#!/usr/bin/env python3 """Upstream-compatible tools/normalize_caps.py for hyperdope-gematria.""" import re import sys from pathlib import Path ROMAN = re.compile(r"M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})", re.I) def load_dict(): for p in ("/usr/share/dict/words", "/etc/dictionaries-common/words"): if Path(p).exists(): return {w.strip().lower() for w in Path(p).read_text(encoding="utf-8", errors="ignore").splitlines() if w.strip()} return set() DICT = load_dict() INGGLISH_NON_ROMAN = {"di", "li", "mi", "vi", "iv", "ci", "xi"} def is_roman(s): return bool(s) and s.lower() not in INGGLISH_NON_ROMAN and ROMAN.fullmatch(s) is not None def xform_word(w): if any(c.isdigit() for c in w) or "[" in w or "]" in w: return w letters = re.sub(r"[^A-Za-z]", "", w) if not letters: return w if w != w.lower() and w != w.upper() and not (w[0].isupper() and w[1:] == w[1:].lower()): return w if w == w.upper() and len(letters) >= 2: return w if len(letters) >= 2 and is_roman(letters) and letters.lower() not in DICT: return w.upper() return w[:1].upper() + w[1:].lower() def xform_line(line): return " ".join(xform_word(w) for w in line.split()) def main(): if len(sys.argv) < 3: sys.exit(0) in_path, out_path = Path(sys.argv[1]), Path(sys.argv[2]) raw_lines = in_path.read_text(encoding="utf-8").splitlines() header = raw_lines[0] if raw_lines and "GEMATRO" in raw_lines[0].upper() else "CREATE_GEMATRO_DB" seen = set() body = [] for line in raw_lines[1:]: s = line.strip() if not s: continue norm = xform_line(s) key = norm.lower() if key not in seen: seen.add(key) body.append(norm) body.sort(key=lambda x: x.lower()) out_path.write_text("\n".join([header] + body) + "\n", encoding="utf-8") if __name__ == "__main__": main()