Download rule_based.py from Urdatorn/Makron-code: direct link, hf CLI and curl.
- Browser
- Download file 2.53 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
2.53 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). | |
| Configuration as in grc-macronizer/macron_model/eval_norma.py (commit 456c970): | |
| Macronizer(no_hypotactic=True, make_prints=False, lowercase=True), 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) | |
| a = ap.parse_args() | |
| from grc_macronizer import Macronizer | |
| mac = Macronizer(no_hypotactic=True, make_prints=False, lowercase=True) | |
| 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() | |