File size: 2,528 Bytes
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
'''
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()