-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscore.py
More file actions
68 lines (59 loc) · 1.93 KB
/
Copy pathscore.py
File metadata and controls
68 lines (59 loc) · 1.93 KB
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
import conlangs.conlangs as conlangs
import jsonlines
from absl import app
from absl import flags
from Levenshtein import distance
TEST = flags.DEFINE_string("test", None, "Path to test file")
VERBOSE = flags.DEFINE_bool("verbose", False, "Show errors")
GRAMMARS = {
"ferulian": conlangs.CARDINALS_FERULIAN,
"neoferulian": conlangs.CARDINALS_NEOFERULIAN,
"archiferulian": conlangs.CARDINALS_ARCHIFERULIAN,
"continental_ferulian": conlangs.CARDINALS_CONTINENTAL_FERULIAN,
"lurefian": conlangs.CARDINALS_LUREFIAN,
"archilurefian": conlangs.CARDINALS_ARCHILUREFIAN,
}
def score_output(path, out):
language = path.split("/")[-1].split(".")[0]
print(f"Scoring {language}")
grammar = GRAMMARS[language]
total_names = 0
total_words = 0
total_chars = 0
err_names = 0
err_words = 0
err_chars = 0
with jsonlines.open(path) as reader:
for elt in reader:
hyp = elt["reading"].strip()
ref = grammar.verbalize(elt["number"])[0]
total_names += 1
total_words += len(ref.split())
total_chars += len(ref)
if hyp != ref:
err_names += 1
err_words += distance(ref.split(), hyp.split())
err_chars += distance(ref, hyp)
if VERBOSE.value:
out.write("-" * 80 + "\n")
out.write(f"Num:\t{elt['number']}\n")
out.write(f"Ref:\t{ref}\n")
out.write(f"Hyp:\t{hyp}\n")
out.write("*" * 80 + "\n")
out.write(f"______\tTOTAL\tERROR\tERROR RATE\n")
out.write(
f"Names:\t{total_names}\t{err_names}\t{err_names / total_names:.02f}\n"
)
out.write(
f"Words:\t{total_words}\t{err_words}\t{err_words / total_words:.02f}\n"
)
out.write(
f"Chars:\t{total_chars}\t{err_chars}\t{err_chars / total_chars:.02f}\n"
)
def main(unused_argv):
output_file = TEST.value.replace(".jsonl", ".txt")
with open(output_file, "w") as out:
score_output(TEST.value, out)
if __name__ == "__main__":
flags.mark_flag_as_required("test")
app.run(main)