-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
60 lines (50 loc) · 1.95 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
import Levenshtein as Lev
class CharacterErrorRate(object):
"""
Computes the Character Error Rate, defined as the edit distance between the
two provided sentences after tokenizing to characters.
"""
def __init__(self, vocab) -> None:
self.total_dist = 0.0
self.total_length = 0.0
def __call__(self, targets, y_hats):
""" Calculating character error rate """
dist, length = self._get_distance(targets, y_hats)
self.total_dist += dist
self.total_length += length
return self.total_dist / self.total_length
def _get_distance(self, targets, y_hats):
"""
Provides total character distance between targets & y_hats
Args:
targets (torch.Tensor): set of ground truth
y_hats (torch.Tensor): predicted y values (y_hat) by the model
Returns: total_dist, total_length
- **total_dist**: total distance between targets & y_hats
- **total_length**: total length of targets sequence
"""
total_dist = 0
total_length = 0
for (target, y_hat) in zip(targets, y_hats):
dist, length = self.metric(target, y_hat)
total_dist += dist
total_length += length
return total_dist, total_length
def metric(self, s1: str, s2: str):
"""
Computes the Character Error Rate, defined as the edit distance between the
two provided sentences after tokenizing to characters.
Arguments:
s1 (string): space-separated sentence
s2 (string): space-separated sentence
"""
s1 = s1.replace(' ', '')
s2 = s2.replace(' ', '')
# if '_' in sentence, means subword-unit, delete '_'
if '_' in s1:
s1 = s1.replace('_', '')
if '_' in s2:
s2 = s2.replace('_', '')
dist = Lev.distance(s2, s1)
length = len(s1.replace(' ', ''))
return dist, length