-
Notifications
You must be signed in to change notification settings - Fork 18
/
wer.py
165 lines (161 loc) · 5.07 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
#-*- coding: utf-8 -*-
#!/usr/bin/env python
import sys
import numpy
def wer(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 = 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):
for j in range(len(h)+1):
if i == 0: d[0][j] = j
elif j == 0: d[i][0] = i
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)
result = float(d[len(r)][len(h)]) / len(r) * 100
#find out the manipulation steps
# x = len(r)
# y = len(h)
# list = []
# while True:
# if x == 0 and y == 0:
# break
# else:
# if 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 d[x][y] == d[x][y-1]+1:
# list.append("i")
# x = x
# y = y-1
# elif 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
# list = list[::-1]
# #print the result in aligned way
# print "REF:",
# 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])),
# 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])),
# else:
# print r[index1],
# else:
# count = 0
# for j in range(i):
# if list[j] == "i":
# count += 1;
# index = i - count
# print r[index],
# print
# print "HYP:",
# 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])),
# 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])),
# else:
# print h[index2],
# else:
# count = 0
# for j in range(i):
# if list[j] == "d":
# count += 1;
# index = i - count
# print h[index],
# print
# print "EVA:",
# 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),
# 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),
# 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),
# else:
# print "S"+" "*(len(h[index2])-1),
# else:
# count = 0
# for j in range(i):
# if list[j] == "i":
# count += 1;
# index = i - count
# print " "*(len(r[index])),
# print
return result
if __name__ == '__main__':
filename1 = sys.argv[1]
filename2 = sys.argv[2]
r = file(filename1).read().split()
h = file(filename2).read().split()
wer(r, h)