-
Notifications
You must be signed in to change notification settings - Fork 0
/
wer.py
198 lines (184 loc) · 7.06 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
# !/usr/bin/env python
import numpy
class WER_Engine:
def calculateEditDistance(self, r, h):
'''
This function is to calculate the edit distance of reference sentence and the hypothesis sentence.
Main algorithm used is dynamic programming.
Attributes:
r -> the list of words produced by splitting reference sentence.
h -> the list of words produced by splitting hypothesis sentence.
'''
d = numpy.zeros((len(r) + 1) * (len(h) + 1), dtype=numpy.uint8).reshape((len(r) + 1, len(h) + 1))
for i in range(len(r) + 1):
d[i][0] = i
for j in range(len(h) + 1):
d[0][j] = j
for i in range(1, len(r) + 1):
for j in range(1, len(h) + 1):
if r[i - 1] == h[j - 1]:
d[i][j] = d[i - 1][j - 1]
else:
substitute = d[i - 1][j - 1] + 1
insert = d[i][j - 1] + 1
delete = d[i - 1][j] + 1
d[i][j] = min(substitute, insert, delete)
return d
def getStepList(self, r, h, d):
'''
This function is to get the list of steps in the process of dynamic programming.
Attributes:
r -> the list of words produced by splitting reference sentence.
h -> the list of words produced by splitting hypothesis sentence.
d -> the matrix built when calculating the editting distance of h and r.
'''
x = len(r)
y = len(h)
list = []
while True:
if x == 0 and y == 0:
break
elif x >= 1 and y >= 1 and d[x][y] == d[x - 1][y - 1] and r[x - 1] == h[y - 1]:
list.append("e")
x = x - 1
y = y - 1
elif y >= 1 and d[x][y] == d[x][y - 1] + 1:
list.append("i")
x = x
y = y - 1
elif x >= 1 and y >= 1 and d[x][y] == d[x - 1][y - 1] + 1:
list.append("s")
x = x - 1
y = y - 1
else:
list.append("d")
x = x - 1
y = y
return list[::-1]
def alignedPrint(self, list, r, h, error_rate, accuracy):
'''
This funcition is to print the result of comparing reference and hypothesis sentences in an aligned way.
Attributes:
list -> the list of steps.
r -> the list of words produced by splitting reference sentence.
h -> the list of words produced by splitting hypothesis sentence.
result -> the rate calculated based on edit distance.
'''
print("REFERENCE: ", end=" ")
for i in range(len(list)):
if list[i] == "i":
count = 0
for j in range(i):
if list[j] == "d":
count += 1
index = i - count
print(" " * (len(h[index])), end=" ")
elif list[i] == "s":
count1 = 0
for j in range(i):
if list[j] == "i":
count1 += 1
index1 = i - count1
count2 = 0
for j in range(i):
if list[j] == "d":
count2 += 1
index2 = i - count2
if len(r[index1]) < len(h[index2]):
print(r[index1] + " " * (len(h[index2]) - len(r[index1])), end=" ")
else:
print(r[index1], end=" "),
else:
count = 0
for j in range(i):
if list[j] == "i":
count += 1
index = i - count
print(r[index], end=" "),
print("\nHYPOTHESIS:", end=" ")
for i in range(len(list)):
if list[i] == "d":
count = 0
for j in range(i):
if list[j] == "i":
count += 1
index = i - count
print(" " * (len(r[index])), end=" ")
elif list[i] == "s":
count1 = 0
for j in range(i):
if list[j] == "i":
count1 += 1
index1 = i - count1
count2 = 0
for j in range(i):
if list[j] == "d":
count2 += 1
index2 = i - count2
if len(r[index1]) > len(h[index2]):
print(h[index2] + " " * (len(r[index1]) - len(h[index2])), end=" ")
else:
print(h[index2], end=" ")
else:
count = 0
for j in range(i):
if list[j] == "d":
count += 1
index = i - count
print(h[index], end=" ")
print("\nEVALUATION:", end=" ")
for i in range(len(list)):
if list[i] == "d":
count = 0
for j in range(i):
if list[j] == "i":
count += 1
index = i - count
print("D" + " " * (len(r[index]) - 1), end=" ")
elif list[i] == "i":
count = 0
for j in range(i):
if list[j] == "d":
count += 1
index = i - count
print("I" + " " * (len(h[index]) - 1), end=" ")
elif list[i] == "s":
count1 = 0
for j in range(i):
if list[j] == "i":
count1 += 1
index1 = i - count1
count2 = 0
for j in range(i):
if list[j] == "d":
count2 += 1
index2 = i - count2
if len(r[index1]) > len(h[index2]):
print("S" + " " * (len(r[index1]) - 1), end=" ")
else:
print("S" + " " * (len(h[index2]) - 1), end=" ")
else:
count = 0
for j in range(i):
if list[j] == "i":
count += 1
index = i - count
print(" " * (len(r[index])), end=" ")
print("\nWord Error Rate: " + error_rate)
print("Word Accuracy: " + accuracy)
def wer(self, r, h):
"""
This is a function that calculate the word error rate in ASR.
You can use it like this: wer("what is it".split(), "what is".split())
"""
# build the matrix
d = self.calculateEditDistance(r, h)
# find out the manipulation steps
list = self.getStepList(r, h, d)
# print the result in aligned way
error_rate = float(d[len(r)][len(h)]) / len(r) * 100
accuracy = 100 - error_rate
error_rate = str("%.2f" % error_rate) + "%"
accuracy = str("%.2f" % accuracy) + "%"
self.alignedPrint(list, r, h, error_rate, accuracy)