-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwer.py
57 lines (45 loc) · 1.5 KB
/
wer.py
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
#!/usr/bin/env python3
import jiwer
import sys
class ReplaceYo(jiwer.AbstractTransform):
def process_string(self, s: str):
s = s.replace("ё", "е")
return s
def load_file(path):
with open(path) as f:
result = f.read()
return result
def main(ref_path, hyp_path):
ref = load_file(ref_path)
hyp = load_file(hyp_path)
tr = [
jiwer.ToLowerCase(),
jiwer.RemovePunctuation(),
ReplaceYo(),
jiwer.RemoveWhiteSpace(replace_by_space=True),
jiwer.RemoveMultipleSpaces(),
jiwer.ReduceToSingleSentence(),
jiwer.ReduceToListOfListOfWords(word_delimiter=" "),
]
transformation = jiwer.Compose(tr)
measures = jiwer.compute_measures(
ref,
hyp,
truth_transform=transformation,
hypothesis_transform=transformation
)
print(f"WER (Words Error Rate): {measures['wer']}")
print(f"MER (Match Error Rate): {measures['mer']}")
print(f"WIL (Word Information Lost): {measures['wil']}")
print(f"WIP (Word Information Preserved): {measures['wip']}")
print(f"Hits: {measures['hits']}")
print(f"Substitutions: {measures['substitutions']}")
print(f"Deletions: {measures['deletions']}")
print(f"Insertions: {measures['insertions']}")
if __name__ == '__main__':
try:
r, h = sys.argv[1:3]
except TypeError:
print("This program must be run with 2 arguments: 'python wer.py <hypothesys_file_path> <reference_file_path>'")
sys.exit(1)
main(r, h)