-
Notifications
You must be signed in to change notification settings - Fork 24
/
metrics.py
148 lines (117 loc) · 5.08 KB
/
metrics.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import jiwer
import editdistance
import diff_match_patch
import numpy as np
from tqdm import tqdm
from nltk import ngrams
from jiwer.transformations import wer_default
def WER(references, hypotheses):
measures = jiwer.compute_measures(references,
hypotheses,
truth_transform=wer_default,
hypothesis_transform=wer_default)
out = {
'WER': round(100*measures['wer'], 2),
'IER': round(100*measures['insertions']/(measures['hits']+measures['substitutions']+measures['deletions']), 2),
'DER': round(100*measures['deletions']/(measures['hits']+measures['substitutions']+measures['deletions']), 2),
'SER': round(100*measures['substitutions']/(measures['hits']+measures['substitutions']+measures['deletions']), 2)
}
return out
def CER(references, hypotheses):
errors = 0
words = 0
for h, r in zip(hypotheses, references):
h_list = list(h)
r_list = list(r)
words += len(r_list)
errors += editdistance.eval(h_list, r_list)
return round(100*errors/words, 2)
def NGramDuplicates(text, ngram_size=5):
all_ngrams = list(ngrams(text.split(), ngram_size))
return len(all_ngrams)-len(set(all_ngrams))
def NGramInsertions(references, hypotheses, ngram_size=5):
repeated_ngrams = 0
for r, h in zip(references, hypotheses):
all_ngrams = list(ngrams(r.split(), ngram_size))
ref_counts = {}
for ngram in all_ngrams:
try:
ref_counts[ngram] += 1
except:
ref_counts[ngram] = 1
all_ngrams = list(ngrams(h.split(), ngram_size))
hyp_counts = {}
for ngram in all_ngrams:
try:
hyp_counts[ngram] += 1
except:
hyp_counts[ngram] = 1
for k, v in hyp_counts.items():
if (v > 1) and (ref_counts.get(k, 1) < v):
repeated_ngrams += (v-ref_counts.get(k, 1))
return repeated_ngrams
def evaluate(references, hypotheses, cer=False, ngram_size=5):
scores = WER(references, hypotheses)
if cer:
scores.update({'CER': CER(references, hypotheses), f'{ngram_size}-GramInsertions': NGramInsertions(references, hypotheses, ngram_size=ngram_size)})
else:
scores.update({f'{ngram_size}-GramInsertions': NGramInsertions(references, hypotheses, ngram_size=ngram_size)})
return scores
def word_alignment_accuracy_single(references, hypotheses, collar=0.2):
# Find diffs between ref and hyp
r_list = [_['word'].replace(" ", "_") for _ in references]
h_list = [_['word'].replace(" ", "_") for _ in hypotheses]
orig_words = '\n'.join(r_list) + '\n'
pred_words = '\n'.join(h_list) + '\n'
diff = diff_match_patch.diff_match_patch()
diff.Diff_Timeout = 0
orig_enc, pred_enc, enc = diff.diff_linesToChars(orig_words, pred_words)
diffs = diff.diff_main(orig_enc, pred_enc, False)
diff.diff_charsToLines(diffs, enc)
diffs_post = [(d[0], d[1].replace('\n', ' ').strip().split()) for d in diffs]
# Find words which got HIT and their matching
r_idx, h_idx = 0, 0
word_idx_match = {}
for case, words in diffs_post:
if case == -1:
r_idx += len(words)
elif case == 1:
h_idx += len(words)
else:
for _ in words:
word_idx_match[r_idx] = h_idx
r_idx += 1
h_idx += 1
# Find words whose alignments overlap with each other
overlapped_words = 0
within_collar_words = 0
for r_idx, h_idx in word_idx_match.items():
if (hypotheses[h_idx]['start']<references[r_idx]['end']) and (hypotheses[h_idx]['end']>references[r_idx]['start']):
overlapped_words += 1
if (hypotheses[h_idx]['start']>=references[r_idx]['start']-collar) and (hypotheses[h_idx]['end']<=references[r_idx]['end']+collar):
within_collar_words += 1
results = {
'acc_overlapped': round(100*overlapped_words/len(word_idx_match), 2),
'acc_within_collar': round(100*within_collar_words/len(word_idx_match), 2),
'overlapped_words': overlapped_words,
'within_collar_words': within_collar_words,
'total_hit_words': len(word_idx_match),
}
return results
def word_alignment_accuracy(references, hypotheses, collar=0.2):
overlapped_words = 0
within_collar_words = 0
total_hit_words = 0
for r, h in tqdm(zip(references, hypotheses), total=len(references)):
res = word_alignment_accuracy_single(r, h, collar=collar)
overlapped_words += res['overlapped_words']
within_collar_words += res['within_collar_words']
total_hit_words += res['total_hit_words']
results = {
'acc_overlapped': round(100*overlapped_words/total_hit_words, 2),
'acc_within_collar': round(100*within_collar_words/total_hit_words, 2),
'overlapped_words': overlapped_words,
'within_collar_words': within_collar_words,
'total_hit_words': total_hit_words,
}
return results