Download rule_based.py from Urdatorn/Makron-code: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/Urdatorn/Makron-code/resolve/main/rule_based.py
- Command line
-
hf download hf://Urdatorn/Makron-code/rule_based.py
-
curl -L -o rule_based.py https://huggingface.co/Urdatorn/Makron-code/resolve/main/rule_based.py
3.23 kB
| ''' | |
| Score the rule-based grc-macronizer on Norma, with the same scorer and output | |
| files as evaluate.py (<out>/rule-based.<split>.metrics.json / .positions.jsonl). | |
| Settings: --settings oga (default) uses those of the OGA silver-data run | |
| (grc-macronizer scripts/macronize_oga_corpus.py, main b68585c): | |
| Macronizer(make_prints=False, doc_from_file=False, no_hypotactic=False, | |
| lowercase=False). The only difference is that OGA's own CoNLL-U annotation | |
| (custom_doc) does not exist for Norma, so odyCy parses the lines. | |
| --settings eval_norma reproduces the first run (results/rule-based, commit | |
| 456c970), as in grc-macronizer/macron_model/eval_norma.py: | |
| Macronizer(no_hypotactic=True, make_prints=False, lowercase=True). | |
| The macronizer is called once per work with its lines joined by "\n", falling | |
| back to one call per line when the output does not split back into the same | |
| number of lines. | |
| ''' | |
| import argparse | |
| import collections | |
| import json | |
| import os | |
| import time | |
| from norma import format_table, load_split, metrics, score | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--splits", nargs="+", default=["test", "validation"]) | |
| ap.add_argument("--norma_repo", default="Urdatorn/norma") | |
| ap.add_argument("--norma_revision", default=None) | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--settings", choices=["oga", "eval_norma"], default="oga") | |
| a = ap.parse_args() | |
| from grc_macronizer import Macronizer | |
| if a.settings == "oga": | |
| kw = dict(make_prints=False, doc_from_file=False, no_hypotactic=False, lowercase=False) | |
| else: | |
| kw = dict(no_hypotactic=True, make_prints=False, lowercase=True) | |
| print(f"Macronizer({kw})", flush=True) | |
| mac = Macronizer(**kw) | |
| os.makedirs(a.out, exist_ok=True) | |
| for s in a.splits: | |
| recs = load_split(s, a.norma_repo, a.norma_revision) | |
| by_src = collections.defaultdict(list) | |
| for i, r in enumerate(recs): | |
| by_src[r.source].append(i) | |
| outputs = [None] * len(recs) | |
| for src, idx in by_src.items(): | |
| t = time.time() | |
| out = mac.macronize("\n".join(recs[i].plain for i in idx)).split("\n") | |
| mode = "joined" | |
| if len(out) != len(idx): | |
| mode = f"per-line fallback ({len(out)} != {len(idx)})" | |
| out = [mac.macronize(recs[i].plain) for i in idx] | |
| for i, o in zip(idx, out): | |
| outputs[i] = o | |
| print(f"[{s}] {src}: {len(idx)} lines, {mode}, {time.time()-t:.1f}s", flush=True) | |
| pos = score(recs, outputs) | |
| met = metrics(pos) | |
| print(f"\n=== rule-based on Norma {s} ({len(recs)} lines, {met['n']} positions) ===") | |
| print(format_table(met), flush=True) | |
| with open(os.path.join(a.out, f"rule-based.{s}.metrics.json"), "w") as f: | |
| json.dump(met, f, indent=2) | |
| with open(os.path.join(a.out, f"rule-based.{s}.positions.jsonl"), "w", encoding="utf-8") as f: | |
| for p in pos: | |
| f.write(json.dumps(p, ensure_ascii=False) + "\n") | |
| with open(os.path.join(a.out, f"rule-based.{s}.outputs.txt"), "w", encoding="utf-8") as f: | |
| f.write("\n".join(outputs) + "\n") | |
| if __name__ == "__main__": | |
| main() | |