File size: 3,231 Bytes
c266841
 
 
 
1a67df9
 
 
 
 
 
 
 
 
 
 
c266841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a67df9
c266841
 
 
1a67df9
 
 
 
 
 
c266841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
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()