"""Build the release files from the internal v3 lexicon. python build_lexicon.py path/to/gem_lexicon_v3.json ../data The internal file maps word -> {w, f, n, g, p}; each pattern p keeps up to three HornMorpho segs (degem=False), the v3 rules that shaped it, and whether a native speaker's tap put it first. Every pattern that has segs is re-derived here with gemination.py (v3 rules on), and the script stops if any seg disagrees with the stored positions, so the release cannot drift from the extractor it ships with. Patterns that exist only because of a native tap (data/native_taps.json) have no segs. Writes gemination_lexicon_v3.jsonl, gemination_lexicon_v3.csv and stats.json. Field definitions are in the dataset card (README.md). """ import collections import csv import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import gemination as G src, out = Path(sys.argv[1]), Path(sys.argv[2]) out.mkdir(parents=True, exist_ok=True) lex = json.load(open(src, encoding="utf-8")) taps = {t["word"] for t in json.load(open(out / "native_taps.json", encoding="utf-8"))["items"]} VERSION = "v3" rows = [] for w, d in lex.items(): readings = [] for p in d["p"]: derived = [G.reading_gemination(w, {"seg": s}) for s in p["segs"]] bad = [g for g in derived if g["pos"] != p["pos"]] if bad: sys.exit(f"extractor disagrees with stored pattern for {w}: {bad[0]} vs {p['pos']}") if not derived and not p.get("native"): sys.exit(f"pattern without segs that is not a native tap: {w} {p['pos']}") first = derived[0] if derived else {"sources": ["native"] * len(p["pos"])} readings.append({ "positions": p["pos"], "letters": [w[i] for i in p["pos"]], "sources": first["sources"], "marked": G.mark(w, p["pos"]), "root_frequency": p["freq"], "pos_tags": p["pos_tags"], "alignment_ok": p["ok"], "rules": p.get("rules", []), "native": bool(p.get("native")), "segs": p["segs"], }) analyzed = d["n"] > 0 and bool(readings) top = readings[0] if readings else None rows.append({ "word": w, "frequency": d["f"], "analyzed": analyzed, "n_readings": d["n"], "n_patterns": len(readings), "geminates": top["positions"] if top else [], "marked": top["marked"] if top else w, "ambiguous": len(readings) > 1, "alignment_failed": any(not r["alignment_ok"] for r in readings), "no_geminate_any_reading": analyzed and all(not r["positions"] for r in readings), "native_checked": w in taps, "readings": readings, }) rows.sort(key=lambda r: (-r["frequency"], r["word"])) with open(out / f"gemination_lexicon_{VERSION}.jsonl", "w", encoding="utf-8") as f: for r in rows: f.write(json.dumps(r, ensure_ascii=False) + "\n") def join(readings, fn): return "|".join(fn(r) for r in readings) cols = ["word", "frequency", "analyzed", "n_readings", "n_patterns", "ambiguous", "alignment_failed", "no_geminate_any_reading", "top_positions", "top_letters", "top_sources", "top_marked", "all_positions", "all_sources", "all_marked", "all_root_frequency", "all_pos_tags", "top_rules", "native_checked"] with open(out / f"gemination_lexicon_{VERSION}.csv", "w", encoding="utf-8", newline="") as f: wr = csv.writer(f) wr.writerow(cols) for r in rows: rs = r["readings"] t = rs[0] if rs else {"positions": [], "letters": [], "sources": [], "marked": r["word"]} wr.writerow([r["word"], r["frequency"], int(r["analyzed"]), r["n_readings"], r["n_patterns"], int(r["ambiguous"]), int(r["alignment_failed"]), int(r["no_geminate_any_reading"]), " ".join(map(str, t["positions"])), " ".join(t["letters"]), " ".join(t["sources"]), t["marked"], join(rs, lambda x: " ".join(map(str, x["positions"]))), join(rs, lambda x: " ".join(x["sources"])), join(rs, lambda x: x["marked"]), join(rs, lambda x: str(x["root_frequency"])), join(rs, lambda x: " ".join(x["pos_tags"])), " ".join(t.get("rules", [])), int(r["native_checked"])]) T = sum(r["frequency"] for r in rows) A = [r for r in rows if r["analyzed"]] def share(sel): return {"types": len(sel), "tokens": sum(r["frequency"] for r in sel), "token_share": round(sum(r["frequency"] for r in sel) / T, 4)} top_n = collections.defaultdict(list) for r in A: top_n[len(r["geminates"])].append(r) src_types, src_tokens = collections.Counter(), collections.Counter() for r in A: for s in r["readings"][0]["sources"]: src_types[s] += 1 src_tokens[s] += r["frequency"] stats = { "word_types": len(rows), "corpus_tokens": T, "analyzed": share(A), "not_analyzed": share([r for r in rows if not r["analyzed"]]), "ambiguous": share([r for r in rows if r["ambiguous"]]), "alignment_failed": share([r for r in rows if r["alignment_failed"]]), "no_geminate_any_reading": share([r for r in rows if r["no_geminate_any_reading"]]), "geminate_in_top_reading": share([r for r in A if r["geminates"]]), "geminate_in_any_reading": share([r for r in A if any(x["positions"] for x in r["readings"])]), "two_or_more_in_top_reading": share([r for r in A if len(r["geminates"]) >= 2]), "top_reading_geminate_count": {k: share(v) for k, v in sorted(top_n.items())}, "top_reading_sources": {"geminates_over_types": dict(src_types), "geminates_token_weighted": dict(src_tokens)}, "top_reading_shaped_by_rule": {name: share([r for r in rows if r["readings"] and name in r["readings"][0]["rules"]]) for name in ("causative_glottal", "relative_m_light", "relative_exist")}, "native_checked": share([r for r in rows if r["native_checked"]]), } json.dump(stats, open(out / "stats.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1) print(json.dumps(stats, ensure_ascii=False, indent=1))