|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate src-tauri/src/transcription/t2s_table.rs from OpenCC's |
| 4 | +TSCharacters.txt mapping (Apache-2.0 licensed). |
| 5 | +
|
| 6 | +Usage: |
| 7 | + python3 scripts/gen_t2s_table.py [--source URL_OR_PATH] |
| 8 | +
|
| 9 | +By default fetches the latest data from BYVoid/OpenCC on GitHub. Pass a local |
| 10 | +path to use a cached copy. The resulting Rust file contains a sorted slice of |
| 11 | +`(char, char)` pairs used by `convert_to_simplified()` via binary search. |
| 12 | +
|
| 13 | +This script is the single source of truth for the conversion table — do not |
| 14 | +hand-edit `t2s_table.rs`. Re-run this whenever you want to refresh the mapping. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import argparse |
| 20 | +import os |
| 21 | +import sys |
| 22 | +from urllib.request import urlopen |
| 23 | + |
| 24 | +DEFAULT_SOURCE = ( |
| 25 | + "https://raw.githubusercontent.com/BYVoid/OpenCC/master/data/dictionary/TSCharacters.txt" |
| 26 | +) |
| 27 | +OUTPUT_PATH = os.path.join( |
| 28 | + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
| 29 | + "src-tauri", |
| 30 | + "src", |
| 31 | + "transcription", |
| 32 | + "t2s_table.rs", |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +def read_source(source: str) -> str: |
| 37 | + if source.startswith(("http://", "https://")): |
| 38 | + with urlopen(source) as resp: |
| 39 | + return resp.read().decode("utf-8") |
| 40 | + with open(source, "r", encoding="utf-8") as fh: |
| 41 | + return fh.read() |
| 42 | + |
| 43 | + |
| 44 | +def parse_pairs(text: str) -> list[tuple[int, int]]: |
| 45 | + pairs: list[tuple[int, int]] = [] |
| 46 | + for line in text.splitlines(): |
| 47 | + line = line.rstrip("\n") |
| 48 | + if not line or line.startswith("#"): |
| 49 | + continue |
| 50 | + parts = line.split("\t") |
| 51 | + if len(parts) < 2: |
| 52 | + continue |
| 53 | + key = parts[0] |
| 54 | + targets = parts[1].split(" ") |
| 55 | + target = targets[0] |
| 56 | + if not key or not target: |
| 57 | + continue |
| 58 | + if len(key) != 1 or len(target) != 1: |
| 59 | + # Multi-codepoint sources/targets are rare; keep the first char only |
| 60 | + if len(target) > 1: |
| 61 | + target = target[0] |
| 62 | + if len(key) != 1: |
| 63 | + continue |
| 64 | + if key == target: |
| 65 | + continue |
| 66 | + pairs.append((ord(key), ord(target))) |
| 67 | + |
| 68 | + # Sort and dedupe by source |
| 69 | + pairs.sort(key=lambda p: p[0]) |
| 70 | + seen: set[int] = set() |
| 71 | + unique: list[tuple[int, int]] = [] |
| 72 | + for p in pairs: |
| 73 | + if p[0] in seen: |
| 74 | + continue |
| 75 | + seen.add(p[0]) |
| 76 | + unique.append(p) |
| 77 | + return unique |
| 78 | + |
| 79 | + |
| 80 | +def emit_rust(pairs: list[tuple[int, int]]) -> str: |
| 81 | + out: list[str] = [] |
| 82 | + out.append("// Auto-generated from OpenCC TSCharacters.txt (Apache-2.0 License).") |
| 83 | + out.append( |
| 84 | + "// Source: https://github.com/BYVoid/OpenCC/blob/master/data/dictionary/TSCharacters.txt" |
| 85 | + ) |
| 86 | + out.append("// DO NOT EDIT BY HAND. Regenerate via scripts/gen_t2s_table.py.") |
| 87 | + out.append("//") |
| 88 | + out.append(f"// Total entries: {len(pairs)}") |
| 89 | + out.append("// Sorted by Traditional (source) codepoint to enable binary search.") |
| 90 | + out.append("") |
| 91 | + out.append("pub(super) static T2S_TABLE: &[(char, char)] = &[") |
| 92 | + |
| 93 | + per_line = 6 |
| 94 | + buf: list[str] = [] |
| 95 | + for src, dst in pairs: |
| 96 | + buf.append(f"('\\u{{{src:X}}}', '\\u{{{dst:X}}}')") |
| 97 | + if len(buf) == per_line: |
| 98 | + out.append(" " + ", ".join(buf) + ",") |
| 99 | + buf = [] |
| 100 | + if buf: |
| 101 | + out.append(" " + ", ".join(buf) + ",") |
| 102 | + out.append("];") |
| 103 | + out.append("") |
| 104 | + return "\n".join(out) |
| 105 | + |
| 106 | + |
| 107 | +def main() -> int: |
| 108 | + ap = argparse.ArgumentParser(description=__doc__) |
| 109 | + ap.add_argument( |
| 110 | + "--source", |
| 111 | + default=DEFAULT_SOURCE, |
| 112 | + help="URL or local path to OpenCC TSCharacters.txt", |
| 113 | + ) |
| 114 | + ap.add_argument("--output", default=OUTPUT_PATH) |
| 115 | + args = ap.parse_args() |
| 116 | + |
| 117 | + print(f"Reading source: {args.source}", file=sys.stderr) |
| 118 | + raw = read_source(args.source) |
| 119 | + pairs = parse_pairs(raw) |
| 120 | + if not pairs: |
| 121 | + print("No pairs parsed — aborting", file=sys.stderr) |
| 122 | + return 1 |
| 123 | + |
| 124 | + rust = emit_rust(pairs) |
| 125 | + with open(args.output, "w", encoding="utf-8") as fh: |
| 126 | + fh.write(rust) |
| 127 | + print( |
| 128 | + f"Wrote {len(pairs)} entries to {args.output}", |
| 129 | + file=sys.stderr, |
| 130 | + ) |
| 131 | + return 0 |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + raise SystemExit(main()) |
0 commit comments