|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Builds corpus.json and glossary.json from the python/python-docs-fa repository. |
| 4 | +
|
| 5 | +corpus.json shape (consumed by the glossary searcher site): |
| 6 | + [ { "msgid": "...", "msgstr": "...", "file": "library/functions.po", "line": 123 }, ... ] |
| 7 | +
|
| 8 | +glossary.json shape: |
| 9 | + [ { "en": "decorator", "fa": "دکوراتور، آراینده" }, ... ] |
| 10 | +
|
| 11 | +Usage: |
| 12 | + python build_corpus.py --repo-dir ./python-docs-fa --glossary-tsv ./glossary.tsv --out-dir ./data |
| 13 | +""" |
| 14 | +import argparse |
| 15 | +import csv |
| 16 | +import json |
| 17 | +import os |
| 18 | +import sys |
| 19 | + |
| 20 | +try: |
| 21 | + import polib |
| 22 | +except ImportError: |
| 23 | + print("ERROR: polib is required. Install with: pip install polib", file=sys.stderr) |
| 24 | + sys.exit(1) |
| 25 | + |
| 26 | + |
| 27 | +def find_po_files(repo_dir): |
| 28 | + po_files = [] |
| 29 | + for root, _dirs, files in os.walk(repo_dir): |
| 30 | + # skip VCS/meta directories |
| 31 | + if "/.git" in root or root.endswith("/.git"): |
| 32 | + continue |
| 33 | + for fname in files: |
| 34 | + if fname.endswith(".po"): |
| 35 | + full_path = os.path.join(root, fname) |
| 36 | + rel_path = os.path.relpath(full_path, repo_dir) |
| 37 | + po_files.append((full_path, rel_path)) |
| 38 | + return sorted(po_files, key=lambda x: x[1]) |
| 39 | + |
| 40 | + |
| 41 | +def parse_po_files(repo_dir): |
| 42 | + """Parse every .po file into flattened msgid/msgstr corpus entries.""" |
| 43 | + entries = [] |
| 44 | + skipped = 0 |
| 45 | + po_files = find_po_files(repo_dir) |
| 46 | + |
| 47 | + if not po_files: |
| 48 | + print(f"WARNING: no .po files found under {repo_dir}", file=sys.stderr) |
| 49 | + |
| 50 | + for full_path, rel_path in po_files: |
| 51 | + try: |
| 52 | + po = polib.pofile(full_path) |
| 53 | + except Exception as e: |
| 54 | + print(f"WARNING: failed to parse {rel_path}: {e}", file=sys.stderr) |
| 55 | + skipped += 1 |
| 56 | + continue |
| 57 | + |
| 58 | + for entry in po: |
| 59 | + # Skip obsolete, fuzzy, or empty-translation entries -- they |
| 60 | + # aren't useful corpus results and fuzzy ones are unreviewed. |
| 61 | + if entry.obsolete: |
| 62 | + continue |
| 63 | + if "fuzzy" in entry.flags: |
| 64 | + continue |
| 65 | + if not entry.msgid or not entry.msgstr: |
| 66 | + continue |
| 67 | + |
| 68 | + entries.append( |
| 69 | + { |
| 70 | + "msgid": entry.msgid, |
| 71 | + "msgstr": entry.msgstr, |
| 72 | + "file": rel_path, |
| 73 | + "line": entry.linenum if hasattr(entry, "linenum") else 0, |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + print( |
| 78 | + f"Parsed {len(po_files)} .po files ({skipped} skipped), " |
| 79 | + f"{len(entries)} translated entries", |
| 80 | + file=sys.stderr, |
| 81 | + ) |
| 82 | + return entries |
| 83 | + |
| 84 | + |
| 85 | +def parse_glossary_tsv(tsv_path): |
| 86 | + """Parse the glossary TSV (English<TAB>Persian) into glossary.json entries.""" |
| 87 | + entries = [] |
| 88 | + with open(tsv_path, "r", encoding="utf-8") as f: |
| 89 | + reader = csv.reader(f, delimiter="\t") |
| 90 | + rows = list(reader) |
| 91 | + |
| 92 | + if not rows: |
| 93 | + return entries |
| 94 | + |
| 95 | + # Skip header row if it looks like one |
| 96 | + start_idx = 1 if rows[0][:2] == ["English", "Persian"] else 0 |
| 97 | + |
| 98 | + for row in rows[start_idx:]: |
| 99 | + if len(row) < 2: |
| 100 | + continue |
| 101 | + en, fa = row[0].strip(), row[1].strip() |
| 102 | + if en and fa: |
| 103 | + entries.append({"en": en, "fa": fa}) |
| 104 | + |
| 105 | + print(f"Parsed {len(entries)} glossary entries", file=sys.stderr) |
| 106 | + return entries |
| 107 | + |
| 108 | + |
| 109 | +def main(): |
| 110 | + parser = argparse.ArgumentParser(description=__doc__) |
| 111 | + parser.add_argument( |
| 112 | + "--repo-dir", required=True, help="Path to the cloned python-docs-fa checkout" |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + "--glossary-tsv", |
| 116 | + required=True, |
| 117 | + help="Path to the glossary TSV file (English<TAB>Persian)", |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "--out-dir", |
| 121 | + required=True, |
| 122 | + help="Directory to write corpus.json and glossary.json into", |
| 123 | + ) |
| 124 | + args = parser.parse_args() |
| 125 | + |
| 126 | + os.makedirs(args.out_dir, exist_ok=True) |
| 127 | + |
| 128 | + corpus = parse_po_files(args.repo_dir) |
| 129 | + glossary = parse_glossary_tsv(args.glossary_tsv) |
| 130 | + |
| 131 | + corpus_path = os.path.join(args.out_dir, "corpus.json") |
| 132 | + glossary_path = os.path.join(args.out_dir, "glossary.json") |
| 133 | + |
| 134 | + with open(corpus_path, "w", encoding="utf-8") as f: |
| 135 | + json.dump(corpus, f, ensure_ascii=False, separators=(",", ":")) |
| 136 | + |
| 137 | + with open(glossary_path, "w", encoding="utf-8") as f: |
| 138 | + json.dump(glossary, f, ensure_ascii=False, separators=(",", ":")) |
| 139 | + |
| 140 | + print( |
| 141 | + f"Wrote {corpus_path} ({os.path.getsize(corpus_path):,} bytes)", file=sys.stderr |
| 142 | + ) |
| 143 | + print( |
| 144 | + f"Wrote {glossary_path} ({os.path.getsize(glossary_path):,} bytes)", |
| 145 | + file=sys.stderr, |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + main() |
0 commit comments