Repository navigation
Expand file tree
/
Copy pathcheck_consistency.py
More file actions
960 lines (816 loc) · 32.9 KB
/
Copy pathcheck_consistency.py
File metadata and controls
960 lines (816 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
#!/usr/bin/env python3
"""
scripts/check_consistency.py
Audit the whole .po corpus (English msgid -> Farsi msgstr) for translation
consistency. Three independent checks:
1. glossary -- a glossary term occurs in the English msgid but none of
its allowed Farsi equivalents occur in the msgstr.
Matching is markup-aware (terms inside code spans, code
roles, URLs, kept-verbatim emphasis and proper-noun
phrases are ignored) and morphology-tolerant (Persian
plural/ezafe/verb endings, Arabic broken plurals,
hamza/yeh spelling variants all still count).
2. duplicates -- the exact same English string is translated differently
in different places. The strongest consistency signal.
3. drift -- frequent English terms NOT in the glossary whose Farsi
rendering varies across occurrences; these are candidate
terms to add to GLOSSARY.md and then normalize.
Output: a JSON report (default reports/consistency_report.json) plus a
console summary. Findings are for human review -- the heuristics trade a
little noise for recall, and every finding carries file:line + both texts.
Requires: polib (pip install polib)
Usage:
python3 scripts/check_consistency.py
python3 scripts/check_consistency.py --checks glossary,duplicates,drift
python3 scripts/check_consistency.py --min-freq 6 --out report.json
"""
import argparse
import json
import re
import sys
from collections import Counter, defaultdict
from pathlib import Path
import polib
REPO_ROOT = Path(__file__).resolve().parent.parent
EXCLUDE_DIRS = {".git", ".venv", "venv", "node_modules", "__pycache__", ".tox"}
# ---------------------------------------------------------------------------
# Markup handling
# ---------------------------------------------------------------------------
# Sphinx roles whose content is code and is (correctly) left untranslated.
# Terms appearing inside these must NOT trigger glossary checks.
CODE_ROLES = {
"func",
"meth",
"mod",
"class",
"data",
"const",
"attr",
"exc",
"obj",
"command",
"cmdoption",
"envvar",
"file",
"kbd",
"option",
"program",
"regexp",
"makevar",
"dunder",
"module",
"method",
"exception",
"ref",
"doc",
"download",
"env",
"pep",
"rfc",
"issue",
"source",
"mimetype",
"keyword",
"literal",
"token",
"grammar",
"confval",
"setting",
}
ROLE_RE = re.compile(r":([\w+-]+):`([^`]*)`")
DOUBLE_BACKTICK_RE = re.compile(r"``.*?``", re.DOTALL)
SINGLE_BACKTICK_RE = re.compile(r"`([^`]*)`")
SUBREF_RE = re.compile(r"\|[\w.-]+\|")
URL_RE = re.compile(r"https?://\S+")
# asterisk emphasis only: underscores are code in these docs (__init__ etc.)
EMPH_RE = re.compile(r"(\*\*?)(.+?)\1")
TARGET_RE = re.compile(r"^(.*)\s<[^<>]+>$")
EMPH_TOKEN_RE = re.compile(r"\*([A-Za-z_][\w.\-]*)\*")
def strip_markup(text: str, keep_prose_roles: bool = True) -> str:
"""Reduce RST/Sphinx text to prose for term mining.
Code spans, code-role contents and URLs are removed; the display text
of prose roles (:term:, :ref: titles, ...) is kept because translators
translate it.
"""
def role_repl(m: re.Match) -> str:
role, body = m.group(1).lower(), m.group(2)
if role in CODE_ROLES:
return " "
if not keep_prose_roles:
return " "
t = TARGET_RE.match(body)
return t.group(1) if t else body
text = DOUBLE_BACKTICK_RE.sub(" ", text)
text = ROLE_RE.sub(role_repl, text)
text = SINGLE_BACKTICK_RE.sub(r" \1 ", text)
text = SUBREF_RE.sub(" ", text)
text = URL_RE.sub(" ", text)
text = EMPH_RE.sub(r"\2", text)
return text
def looks_like_code_block(msgid: str) -> bool:
"""Whole-entry literal blocks (doctests, code listings)."""
s = msgid.strip()
if "\n" not in s:
return False
lines = [ln for ln in s.splitlines() if ln.strip()]
if not lines:
return True
codeish = sum(
1
for ln in lines
if ln.lstrip().startswith((">>>", "...", "#", "$"))
or ln.startswith((" ", "\t"))
)
return codeish / len(lines) >= 0.8
WORD_RE = re.compile(r"[A-Za-z][A-Za-z\-]*[A-Za-z]|[A-Za-z]")
# quoted spans whose content looks like code ('Content-type', "import foo")
QUOTED_CODE_RE = re.compile(r"'([\w.\-/ ]+)'|\"([\w.\-/ ]+)\"")
def quoted_span_is_code(content: str) -> bool:
words = content.split()
return (
len(words) == 1
or any(c in content for c in ".-_")
or content.startswith(("import ", "from "))
)
def strip_quoted_code(text: str) -> str:
def repl(m: re.Match) -> str:
content = m.group(1) or m.group(2)
return " " if quoted_span_is_code(content) else m.group(0)
return QUOTED_CODE_RE.sub(repl, text)
CODE_LINE_START_RE = re.compile(
r"^(?:import|from)\s" r"|^[a-z_][\w.]*(?:\[[^\]]*\])?\s*[=(]" r"|^[a-z_][\w.]*\("
)
CODE_STMT_RE = re.compile(
r"^(?:if|elif|else|for|while|def|class|try|except|finally|with|return"
r"|raise|break|continue|lambda|async|await)\b.*:\s*(?:#.*)?$"
)
def looks_codeish(line: str) -> bool:
if line.startswith((">>>", "...", "$", "#")):
return True
if line.startswith(("'", '"', "[", "{", "<")) or line[0:1].isdigit():
return True # literal output of a doctest
return bool(CODE_LINE_START_RE.match(line) or CODE_STMT_RE.match(line))
def prose_line(line: str) -> bool:
s = line.strip()
if not s or looks_codeish(s):
return False
return len(s.split()) >= 3 and s[0].isupper()
def standalone_code_entry(msgid: str) -> bool:
"""Entries that are pure code listings (no '::' marker included)."""
lines = [ln for ln in msgid.split("\n") if ln.strip()]
if not lines:
return False
codeish = sum(1 for ln in lines if ln[0] in " \t" or looks_codeish(ln.strip()))
prose = sum(1 for ln in lines if prose_line(ln))
return codeish >= max(1, len(lines) // 2) and prose <= len(lines) // 3
def strip_literal_blocks(msgid: str) -> str:
"""Drop literal-block content (the lines after a '::' marker), which
in mixed prose+code entries is code and must not be term-mined."""
out = []
in_block = False
for ln in msgid.split("\n"):
stripped = ln.strip()
if not in_block:
if stripped == "::":
in_block = True
continue
if stripped.endswith("::"):
out.append(ln[: ln.rindex("::")].rstrip())
in_block = True
continue
out.append(ln)
else:
if not stripped:
continue # blank line inside/after block: keep skipping
if ln[0] in " \t" or looks_codeish(stripped):
continue # still block content
in_block = False
out.append(ln)
return "\n".join(out)
def mostly_preserved_code(msgid: str, msgstr: str) -> bool:
"""Code listings whose only translated parts are comments: nearly all
English tokens of the msgid reappear verbatim in the msgstr."""
toks = [t.lower() for t in WORD_RE.findall(strip_markup(msgid))]
if len(toks) < 6:
return False
kept = sum(1 for t in toks if t in msgstr)
return kept / len(toks) >= 0.8
# ---------------------------------------------------------------------------
# Farsi text normalization
# ---------------------------------------------------------------------------
# Arabic-letter spelling variants used interchangeably in the corpus.
FA_CHAR_MAP = str.maketrans(
{
"\u0622": "\u0627", # آ
"\u0623": "\u0627", # أ
"\u0625": "\u0627", # إ
"\u0671": "\u0627", # ٱ
"\u0626": "\u0621", # ئ (شیئی -> شیءی)
"\u0624": "\u0621", # ؤ
"\u064a": "\u06cc", # ي -> ی
"\u0643": "\u06a9", # ك -> ک
"\u0629": "\u0647", # ة -> ه
}
)
def normalize_fa(text: str) -> str:
return text.translate(FA_CHAR_MAP)
# ---------------------------------------------------------------------------
# Glossary loading + pattern building
# ---------------------------------------------------------------------------
# Arabic *base letters* (not combining marks): a glossary variant followed
# by one of these is a different word, not the variant + a suffix.
AR_LETTERS = (
"\u0620-\u064a" # basic Arabic letters alef..yeh
"\u066e-\u066f" # dotless beh/qaf
"\u0671-\u06d3" # extended letters (peh, cheh, jeh, gaf, yeh barree...)
"\u06ee-\u06fc" # dal/rae with ring, waw/alef variants, ligatures
)
POST_BOUND = f"(?![{AR_LETTERS}A-Za-z0-9_])"
PRE_BOUND = f"(?<![{AR_LETTERS}A-Za-z])"
# ZWNJ is sometimes inserted inside words by editors/IMEs (شیء for شیء),
# so allow an optional ZWNJ between every pair of letters of a variant word.
def flex_word(word: str) -> str:
return "(?:\u200c)?".join(re.escape(c) for c in word)
# Common Persian clitics/suffixes that legitimately attach to a term,
# with or without ZWNJ: plural ها/های/هایی, indefinite/adjectival ی/یی,
# ezafe-ish ی, possessives (م/ت/ش/...), comparatives.
FA_SUFFIX = (
r"(?:\u200c)?(?:هایی|های|ها|یی|ترین|تر|انه|شان|مان|تان"
r"|یش|یت|یم|اش|ات|ام|ای|ش|ت|م|ی)?"
)
# Productive compound formers: شیءگرا (object-oriented), فهرستسازی, ...
# The term's concept is still present, so these count as a match.
FA_COMPOUND = r"(?:\u200c)?(?:گرایی|گرا|محوری|محور|سازی|ساز|پذیری|پذیر|بندی|پایه|مند)?"
FA_TAIL = FA_COMPOUND + FA_SUFFIX
# Arabic broken plurals the corpus uses for glossary variants. Broken
# plurals are unpredictable, so they're listed explicitly here; extend
# as needed when reviewing violations.
ARABIC_PLURALS = {
"تابع": ("توابع",),
"مقدار": ("مقادیر",),
"شیء": ("اشیاء", "اشیای", "اشیا"),
"نوع": ("انواع",),
"عدد": ("اعداد",),
"عنصر": ("عناصر",),
"عبارت": ("عبارات",),
"استثنا": ("استثنائات",),
"رابط": ("روابط",),
}
# Separable Persian preverbs: می/نمی/ن is inserted AFTER the preverb
# (برگرداندن -> برمیگرداند، برنمیگرداند). بر and باز are largely
# interchangeable in these compound verbs (برگرداندن ~ بازگرداندن).
PREVERB_ALTS = {
"بر": "(?:بر|باز)",
"باز": "(?:باز|بر)",
}
PREVERBS = ("بر", "باز", "در", "فر", "وا", "سر", "پیش", "رو")
# Endings that may follow a verb stem (past and present forms),
# longest first. "دن" covers the infinitive itself (بازگرداندن).
VERB_SUFFIX = r"(?:دیم|دید|دند|یند|دن|ده|دی|د|ید|یم|ند|ی)?"
# Same-concept verb pairs: forms of برگشتن (intransitive "return":
# بازمیگردد، برگشتی) are valid renderings of "return" alongside
# برگرداندن. Irregular present stems are listed in PRESENT_STEM.
RELATED_VERBS = {
"برگرداندن": ("برگشتن",),
}
PRESENT_STEM = {
"گشتن": "گرد",
}
def verb_pattern_for(infinitive: str) -> str:
"""Pattern for one infinitive's conjugated forms (no PRE_BOUND)."""
preverb = ""
rest = infinitive
for pv in PREVERBS:
if infinitive.startswith(pv) and len(infinitive) > len(pv) + 3:
preverb, rest = pv, infinitive[len(pv) :]
break
past = rest[:-1]
stems = [past]
short = rest[:-2] # present stem for causatives (گرداندن -> گردان)
if len(short) >= 3 and short not in stems:
stems.append(short)
if rest in PRESENT_STEM:
stems.append(PRESENT_STEM[rest])
stem_re = "(?:" + "|".join(flex_word(s) for s in stems) + ")"
if preverb:
pv_re = PREVERB_ALTS.get(preverb, re.escape(preverb))
return (
f"{pv_re}" + r"(?:\s|\u200c)?(?:می\u200c|نمی\u200c|ن)?"
f"{stem_re}{VERB_SUFFIX}{POST_BOUND}"
)
return f"(?:می\u200c|نمی\u200c|ب|ن)?{stem_re}{VERB_SUFFIX}{POST_BOUND}"
def verb_pattern(w: str) -> str:
"""Single-word infinitive -> pattern covering its conjugated forms.
پوشاندن -> پوشاند، پوشانده، میپوشاند، بپوشانید، میپوشانند ...
برگرداندن -> برگرداند، برمیگرداند، برگردانید، بازگرداندند،
and same-concept برگشتن forms (بازمیگردد، برگشتی) via RELATED_VERBS.
"""
w = normalize_fa(w)
alts = [verb_pattern_for(w)]
for rel in RELATED_VERBS.get(w, ()):
alts.append(verb_pattern_for(normalize_fa(rel)))
return PRE_BOUND + "(?:" + "|".join(alts) + ")"
def word_alts(word: str) -> str:
"""Regex alternation for a variant word incl. known broken plurals."""
word = normalize_fa(word)
forms = [word] + [
normalize_fa(p) for p in ARABIC_PLURALS.get(word, ()) if p != word
]
return "(?:" + "|".join(flex_word(f) for f in forms) + ")"
def variant_to_regex(variant: str) -> str:
"""Build a tolerant regex for one glossary Farsi variant.
Handles: ZWNJ-attached suffixes, known Arabic broken plurals,
hamza/yeh spelling variants, verb variants in کردن/شدن infinitive
form (conjugated stems still count, including می-insertion after
separable preverbs), and multi-word variants with up to one filler
word or a ZWNJ-plural in between
(آرگومان کلیدواژهای -> آرگومانهای کلیدواژهای).
"""
v = normalize_fa(variant.strip())
words = v.split()
if len(words) == 1:
w = words[0]
if len(w) > 3 and w.endswith("ن") and w[-2] in "دت":
return verb_pattern(w)
return f"{PRE_BOUND}{word_alts(w)}{FA_TAIL}{POST_BOUND}"
# multi-word variant
last = words[-1]
head = words[:-1]
# between parts: plain space (+ up to one filler word), a bare ZWNJ
# join (رشتهمستند), or a ZWNJ-attached plural then a space
# (آرگومانهای کلیدواژهای). A trailing ezafe mark (رشتهٔ) may
# precede the space.
marks = "\u064b-\u065f\u0670"
fill = r"(?:\u200c(?:هایی|های|ها)?\s+" r"|\u200c" rf"|[{marks}]*\s+(?:\S+\s+)?)"
if last == "کردن":
h = fill.join(word_alts(w) for w in head)
# verb stems, plus the deverbal noun (خنثی کردن -> خنثیسازی)
noun = r"(?:\u200c)?(?:سازی|ساز)(?:\u200c(?:های|ها))?"
return (
f"{PRE_BOUND}{h}" + r"(?:\s+\S+){0,2}[\s\u200c]+"
f"(?:می\u200c|نمی\u200c|ن)?"
f"(?:کرد(?:ه|ی|یم|ید|ند|ن)?|کن(?:د|ید|یم|ند)?"
f"|شد(?:ه|ی|یم|ید|ند|ن)?|شو(?:د|ند)?"
f"|{noun}){POST_BOUND}"
)
if last == "شدن":
h = fill.join(word_alts(w) for w in head)
return (
f"{PRE_BOUND}{h}" + r"(?:\s+\S+){0,2}[\s\u200c]+"
f"(?:می\u200c|نمی\u200c|ن)?"
f"(?:شد(?:ه|ی|یم|ید|ند|ن)?|شو(?:د|ند)?)?{POST_BOUND}"
)
parts = [word_alts(w) for w in words]
return PRE_BOUND + fill.join(parts) + FA_TAIL + POST_BOUND
def term_to_regex(term: str) -> str:
"""English glossary term -> regex for locating it in msgid prose.
Spaces and hyphens are interchangeable; an optional English plural
suffix is allowed ("arguments" matches term "argument"). A leading
dot or slash means code (list.index, if/while/def/class), not prose;
for single-word terms a leading hyphen means a compound name
(Content-type, first-class) and is rejected too.
"""
parts = [re.escape(p) for p in re.split(r"[\s\-]+", term) if p]
body = r"[\s\-]+".join(parts)
pre = r"(?<![A-Za-z0-9_.\-])" if len(parts) == 1 else r"(?<![A-Za-z0-9_./])"
return f"{pre}{body}(?:e?s)?(?![A-Za-z0-9_])"
def load_glossary(path: Path):
raw = json.loads(path.read_text(encoding="utf-8"))
glossary = {}
for term, variants_str in raw.items():
variants = [v.strip() for v in re.split(r"[،,]", variants_str) if v.strip()]
glossary[term.lower()] = variants
return glossary
def build_term_index(glossary):
"""One combined alternation (longest-first) -> list of (term) by group."""
terms = sorted(glossary, key=len, reverse=True)
alts = []
index = []
for i, term in enumerate(terms):
alts.append(f"(?P<t{i}>{term_to_regex(term)})")
index.append(term)
combined = re.compile("|".join(alts), re.IGNORECASE)
return combined, index
def build_variant_patterns(glossary):
patterns = {}
for term, variants in glossary.items():
alt = "|".join(f"(?:{variant_to_regex(v)})" for v in variants)
patterns[term] = re.compile(f"(?:{alt})")
return patterns
# ---------------------------------------------------------------------------
# Entry loading
# ---------------------------------------------------------------------------
def iter_po_files(root: Path):
for p in sorted(root.rglob("*.po")):
if not (set(p.parts) & EXCLUDE_DIRS):
yield p
def entry_translations(entry):
"""All msgstr texts of an entry (plural forms included)."""
if entry.msgid_plural:
return [s for s in entry.msgstr_plural.values() if s]
return [entry.msgstr] if entry.msgstr else []
def load_entries(root: Path):
entries = []
n_files = 0
for path in iter_po_files(root):
n_files += 1
try:
po = polib.pofile(str(path))
except Exception as e:
print(f"WARNING: failed to parse {path}: {e}", file=sys.stderr)
continue
rel = str(path.relative_to(root))
for entry in po:
if entry.obsolete or entry.fuzzy or not entry.msgid:
continue
translations = entry_translations(entry)
if not translations:
continue # untranslated
entries.append(
{
"file": rel,
"line": entry.linenum,
"msgid": entry.msgid,
"msgstr": translations[0],
"translations": translations,
"code": looks_like_code_block(entry.msgid),
}
)
return entries, n_files
# ---------------------------------------------------------------------------
# Check 1: glossary violations
# ---------------------------------------------------------------------------
# Idiomatic English uses of glossary terms that don't refer to the concept
# (matched against the prose surrounding the term occurrence).
TERM_SKIP_CONTEXTS = {
"instance": [re.compile(r"for\s+instance\b", re.I)],
"type": [
re.compile(r"\b(?:you|we|to)\s+type\b", re.I),
re.compile(r"\btype\s+(?:the|in|it|this|these|them)\b", re.I),
re.compile(r"\btype\s+of\b", re.I),
],
"types": [re.compile(r"\btypes\s+of\b", re.I)],
"return": [re.compile(r"\bin\s+return\b", re.I)],
"escape": [re.compile(r"\bescape\s+key\b", re.I)],
"function": [
re.compile(r"\bto\s+function\b", re.I),
re.compile(r"\bfunctions?\s+(?:as|correctly|properly|normally)\b", re.I),
re.compile(r"\bfunctioning\b", re.I),
],
"class": [
re.compile(r"\bfirst[\s\-]class\b", re.I),
re.compile(r"\bclass(?:es)?\s+of\b", re.I),
],
}
def term_in_skip_context(term: str, prose: str, m: re.Match) -> bool:
keys = {term, m.group(0).lower()}
for key in keys:
for pat in TERM_SKIP_CONTEXTS.get(key, ()):
for cm in pat.finditer(prose):
if cm.start() <= m.start() < cm.end():
return True
return False
def remove_kept_emphasis(prose: str, raw_msgid: str, msgstr: str) -> str:
"""Drop emphasized code-ish tokens (*encoding*, *op*, ...) that the
translator kept verbatim -- those are parameter names, not prose."""
for tok in set(EMPH_TOKEN_RE.findall(raw_msgid)):
if tok in msgstr:
prose = re.sub(
r"(?<![A-Za-z0-9_.])" + re.escape(tok) + r"(?![A-Za-z0-9_])",
" ",
prose,
flags=re.IGNORECASE,
)
return prose
def is_proper_noun_occurrence(prose: str, m: re.Match) -> bool:
"""Mid-sentence capitalized term followed by a capitalized word
(Object Pascal, Boolean Method...) is a proper noun, not the term."""
text = m.group(0)
if not text[0].isupper():
return False
start = m.start()
if start > 0:
prev = prose[start - 1]
if prev not in " \t\n([{'\"-":
return False # sentence start or after punctuation: keep it
nxt = prose[m.end() : m.end() + 30].split()
return bool(nxt) and nxt[0][0].isupper()
def check_glossary(entries, glossary):
combined, index = build_term_index(glossary)
var_patterns = build_variant_patterns(glossary)
violations = defaultdict(list)
variant_usage = defaultdict(Counter)
term_hits = Counter()
for e in entries:
msgid, msgstr = e["msgid"], e["msgstr"]
if e["code"] or msgstr.strip() == msgid.strip():
continue
if mostly_preserved_code(msgid, msgstr):
continue
if standalone_code_entry(msgid):
continue
prose = strip_literal_blocks(msgid)
prose = strip_markup(prose)
prose = strip_quoted_code(prose)
prose = remove_kept_emphasis(prose, msgid, msgstr)
if not prose.strip():
continue
msgstr_n = normalize_fa(msgstr)
found = set()
for m in combined.finditer(prose):
if is_proper_noun_occurrence(prose, m):
continue
term = index[int(m.lastgroup[1:])]
if term_in_skip_context(term, prose, m):
continue
found.add(term)
for term in found:
term_hits[term] += 1
if var_patterns[term].search(msgstr_n):
for v in glossary[term]:
if re.search(variant_to_regex(v), msgstr_n):
variant_usage[term][v] += 1
else:
violations[term].append(
{
"file": e["file"],
"line": e["line"],
"msgid": msgid,
"msgstr": msgstr,
"allowed": glossary[term],
}
)
return {
"violations": violations,
"variant_usage": {t: dict(c) for t, c in variant_usage.items()},
"term_hits": dict(term_hits),
}
# ---------------------------------------------------------------------------
# Check 2: identical msgid, different msgstr
# ---------------------------------------------------------------------------
def norm_ws(s: str) -> str:
return re.sub(r"\s+", " ", s).strip()
def check_duplicates(entries):
groups = defaultdict(list)
for e in entries:
if e["code"]:
continue
key = norm_ws(e["msgid"])
if not key:
continue
trans = tuple(norm_ws(t) for t in e["translations"])
groups[key].append((trans, e))
dupes = {}
for key, occs in groups.items():
distinct = {trans for trans, _ in occs}
if len(distinct) < 2:
continue
by_variant = defaultdict(list)
for trans, e in occs:
by_variant[trans].append({"file": e["file"], "line": e["line"]})
dupes[key] = {
"occurrences": len(occs),
"variants": [
{
"msgstr": "\n".join(trans),
"count": len(locs),
"locations": locs[:10],
}
for trans, locs in sorted(
by_variant.items(), key=lambda kv: -len(kv[1])
)
],
}
return dict(sorted(dupes.items(), key=lambda kv: -kv[1]["occurrences"]))
# ---------------------------------------------------------------------------
# Check 3: non-glossary terminology drift
# ---------------------------------------------------------------------------
ENGLISH_STOPWORDS = set(
"""
a an the and or but if then else when while for to of in on at by with
from as is are was were be been being this that these those it its it's
you your yours we our ours they their theirs he she his her him not no
nor so than too very can will would should could may might must shall
do does did done have has had having don't doesn't didn't isn't aren't
wasn't weren't won't wouldn't shouldn't couldn't mustn't i me my mine
us them what which who whom whose where why how all any both each few
more most other some such only own same new use used using uses also
into out up down over under again further once here there between see
also note notes example examples e.g one two three first second third
following however many much every well just like even since within
without via etc need needs needed want wants let lets say says take
takes give gives know known find found work works working call called
mean means meaning keep keeps run runs running look looks help helps
""".split()
)
MARKUP_NOISE = set(
"""
py class func meth mod exc data const attr obj term ref doc
versionadded versionchanged deprecated seealso rubric literalinclude
code-block highlight index note warning topic default-domain
""".split()
)
STOPWORDS = ENGLISH_STOPWORDS | MARKUP_NOISE
FARSI_WORD_RE = re.compile(r"[\u0600-\u06FF\u200c]+")
# U+060C ، U+061B ؛ U+061F ؟ U+0640 ـ are punctuation, not letters
FA_PUNCT = "\u060c\u061b\u061f\u0640"
FARSI_STOPWORDS = set(
"""
است را به از در که این با آن یک برای میشود میکند میتوان تا هم نیز
شده شود کرد کند دارد دارند بود بودن باشد باشند ها های هایی و یا اگر
چون زیرا اما ولی هر همه بین روی زیر بالای کنار پس سپس دیگر خود
""".split()
)
def tokenize_en(text: str):
return [w.lower().strip("-") for w in WORD_RE.findall(strip_markup(text))]
def tokenize_fa(text: str):
tokens = FARSI_WORD_RE.findall(strip_markup(text, keep_prose_roles=False))
return [
t
for t in tokens
if t not in FARSI_STOPWORDS and len(t) > 1 and not any(c in FA_PUNCT for c in t)
]
def check_drift(
entries, glossary, min_freq, assoc_ratio=4.0, min_variant_count=3, max_examples=4
):
glossary_terms = set(glossary)
unigrams = Counter()
bigrams = Counter()
occurrences = defaultdict(list)
for e in entries:
if e["code"] or e["msgstr"].strip() == e["msgid"].strip():
continue
if standalone_code_entry(e["msgid"]):
continue
if mostly_preserved_code(e["msgid"], e["msgstr"]):
continue
toks = [
t
for t in tokenize_en(e["msgid"])
if len(t) > 2 and t not in STOPWORDS and t not in glossary_terms
]
seen = set()
for t in toks:
unigrams[t] += 1
seen.add(t)
for i in range(len(toks) - 1):
bg = f"{toks[i]} {toks[i + 1]}"
bigrams[bg] += 1
seen.add(bg)
for t in seen:
occurrences[t].append(e)
candidates = {
t
for t, f in (unigrams | bigrams).items()
if f >= min_freq and t.split()[0] not in glossary_terms
}
# corpus-wide Farsi baseline (document frequency)
doc_freq = Counter()
total = 0
for e in entries:
total += 1
for w in set(tokenize_fa(e["msgstr"])):
doc_freq[w] += 1
drift = {}
for term in candidates:
occs = occurrences[term]
n = len(occs)
local = Counter()
word_idx = defaultdict(list)
for i, e in enumerate(occs):
for w in set(tokenize_fa(e["msgstr"])):
local[w] += 1
word_idx[w].append(i)
assoc = []
for w, cnt in local.items():
if cnt < min_variant_count:
continue
ratio = (cnt / n) / max(doc_freq[w] / total, 1e-6)
if ratio >= assoc_ratio:
assoc.append((w, cnt, ratio))
if len(assoc) < 2:
continue
assoc.sort(key=lambda x: -x[1])
drift[term] = {
"frequency": n,
"distinct_translations": len(assoc),
"variants": [
{
"farsi_word": w,
"count": cnt,
"association_ratio": round(ratio, 2),
"examples": [
{
"file": occs[i]["file"],
"line": occs[i]["line"],
"msgid": occs[i]["msgid"],
"msgstr": occs[i]["msgstr"],
}
for i in word_idx[w][:max_examples]
],
}
for w, cnt, ratio in assoc
],
}
return dict(sorted(drift.items(), key=lambda kv: -kv[1]["frequency"]))
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
ap = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
ap.add_argument(
"po_dir",
nargs="?",
default=str(REPO_ROOT),
help="Directory containing .po files (default: repo root)",
)
ap.add_argument(
"--glossary",
default=str(REPO_ROOT / "glossary.json"),
help="Glossary JSON: term -> 'variant1، variant2'",
)
ap.add_argument(
"--checks",
default="glossary,duplicates,drift",
help="Comma-separated subset of: glossary,duplicates,drift",
)
ap.add_argument(
"--min-freq",
type=int,
default=5,
help="Min occurrences for drift candidates (default: 5)",
)
ap.add_argument(
"--assoc-ratio",
type=float,
default=4.0,
help="Min association ratio for drift (default: 4.0)",
)
ap.add_argument(
"--out",
default=str(REPO_ROOT / "reports" / "consistency_report.json"),
help="Output JSON report path",
)
args = ap.parse_args()
checks = {c.strip() for c in args.checks.split(",") if c.strip()}
root = Path(args.po_dir)
print(f"Loading glossary: {args.glossary}")
glossary = load_glossary(Path(args.glossary))
print(f" {len(glossary)} terms")
print(f"Loading .po files under {root} ...")
entries, n_files = load_entries(root)
print(f" {n_files} files, {len(entries)} translated entries")
report = {
"summary": {
"po_files": n_files,
"translated_entries": len(entries),
"glossary_terms": len(glossary),
}
}
if "glossary" in checks:
print("Check 1/3: glossary violations ...")
res = check_glossary(entries, glossary)
n_viol = sum(len(v) for v in res["violations"].values())
report["summary"]["glossary_terms_with_violations"] = len(res["violations"])
report["summary"]["glossary_violations_total"] = n_viol
report["glossary_violations"] = res["violations"]
report["glossary_variant_usage"] = res["variant_usage"]
print(
f" {len(res['violations'])} terms with violations "
f"({n_viol} flagged entries)"
)
worst = sorted(res["violations"].items(), key=lambda kv: -len(kv[1]))[:10]
for term, v in worst:
print(f" {term}: {len(v)}")
if "duplicates" in checks:
print("Check 2/3: identical msgid with different translations ...")
dupes = check_duplicates(entries)
report["summary"]["duplicate_msgids_with_drift"] = len(dupes)
report["duplicate_drift"] = dupes
print(f" {len(dupes)} distinct msgids translated inconsistently")
for key, data in list(dupes.items())[:10]:
short = key if len(key) <= 60 else key[:57] + "..."
print(
f" {data['occurrences']}x / {len(data['variants'])} variants: {short!r}"
)
if "drift" in checks:
print(f"Check 3/3: non-glossary drift (min-freq={args.min_freq}) ...")
drift = check_drift(
entries, glossary, args.min_freq, assoc_ratio=args.assoc_ratio
)
report["summary"]["non_glossary_drift_terms"] = len(drift)
report["non_glossary_drift"] = drift
print(f" {len(drift)} candidate terms with inconsistent rendering")
for term, data in list(drift.items())[:10]:
words = "، ".join(v["farsi_word"] for v in data["variants"][:4])
print(f" {term} ({data['frequency']}x): {words}")
out = Path(args.out)
out.parent.mkdir(parents=True, exist_ok=True)
with open(out, "w", encoding="utf-8") as f:
json.dump(report, f, ensure_ascii=False, indent=2)
print(f"\nReport written to {out}")
if __name__ == "__main__":
main()