-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluating_utils.py
417 lines (357 loc) · 13.7 KB
/
evaluating_utils.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# ****************************************************************
#
# evaluate.py - the evaluation program.
#
# Author: Yue Zhang
#
# Computing lab, University of Oxford. 2006.11
#
# ****************************************************************
# ================================================================
#
# Import modules.
#
# ================================================================
import sys
import os
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "libs")))
import getopt
# ----------------------------------------------------------------
#
# addTuples - add two tuples element by element.
#
# Inputs: tuple1 - operand1
# tuple2 - operand2
#
# Returns: tuple
#
# ----------------------------------------------------------------
def addTuples(tuple1, tuple2):
return tuple([tuple1[i]+tuple2[i] for i in range(len(tuple1))])
# ----------------------------------------------------------------
#
# addListToList - add the second list to the first list.
#
# Inputs: list1 - operand1, the one modified list
# list2 - operand2, the list added
#
# ----------------------------------------------------------------
def addListToList(list1, list2):
for i in range(len(list1)):
list1[i] += list2[i]
# ----------------------------------------------------------------
#
# subtractListFromList - subtract the second list from the first list.
#
# Inputs: list1 - operand1, the one modified list
# list2 - operand2, the list added
#
# ----------------------------------------------------------------
def subtractListFromList(list1, list2):
for i in range(len(list1)):
list1[i] -= list2[i]
# ----------------------------------------------------------------
#
# dotProduct - compute the dot-product for lists/tuples.
#
# Inputs: list1 - operand1
# list2 - operand2
#
# Returns: int
#
# ----------------------------------------------------------------
def dotProduct(list1, list2):
nReturn = 0
for i in range(len(list1)):
nReturn += list1[i] * list2[i]
return nReturn
# ----------------------------------------------------------------
#
# addDictToDict - add a dictionary with int value to another dictionary
#
# Input: dict1 - operand1, the one that is added to
# dict2 - operand2, the one to add
#
# Example:
# addDictToDict({'a':1,'b':2}, {'b':1,'c':2}) = {'a':1,'b':4,'c':2}
#
# ----------------------------------------------------------------
def addDictToDict(dict1, dict2):
for key in dict2:
if key in dict1:
dict1[key] += dict2[key]
else:
dict1[key] = dict2[key]
# ----------------------------------------------------------------
#
# subtractDictFromDict - subtract a dictionary with int value from another dictionary
#
# Input: dict1 - operand1, the one that is modified to
# dict2 - operand2, the one to substract
#
# Example:
# subtractDictFromDict({'a':1,'b':2}, {'b':1,'c':2}) = {'a':1,'b':1,'c':-2}
#
# ----------------------------------------------------------------
def subtractDictFromDict(dict1, dict2):
for key in dict2:
if key in dict1:
dict1[key] -= dict2[key]
else:
dict1[key] = -dict2[key]
# ================================================================
#
# CRawSentenceReader - the raw sentence reader
#
# This reader is aimed for Chinese.
#
# ================================================================
class CRawSentenceReader(object):
# ----------------------------------------------------------------
#
# __init__ - initialisation
#
# Inputs: sPath - the file for reading
#
# ----------------------------------------------------------------
def __init__(self, sPath, sEncoding="utf-8"):
self.m_sPath = sPath
self.m_oFile = open(sPath)
self.m_sEncoding = sEncoding
# ----------------------------------------------------------------
#
# __del__ - destruction
#
# ----------------------------------------------------------------
def __del__(self):
self.m_oFile.close()
# ----------------------------------------------------------------
#
# readNonEmptySentence - read the next sentence
#
# Returns: list of characters or None if the EOF symbol met.
#
# ----------------------------------------------------------------
def readNonEmptySentence(self):
# 1. read one line
sLine = "\n" # use a pseudo \n to start
while sLine: # while there is a line
sLine = sLine.strip() # strip the line
if sLine: # if the line isn't empty
break # break
sLine = self.m_oFile.readline() # read next line
if not sLine: # if eof symbol met
return None # return
# 2. analyse this line
uLine = sLine.decode(self.m_sEncoding) # find unicode
lLine = [sCharacter.encode(self.m_sEncoding) for sCharacter in uLine]
return lLine
# ----------------------------------------------------------------
#
# readSentence - read the next sentence
#
# Returns: list of characters or None if the EOF symbol met.
#
# ----------------------------------------------------------------
def readSentence(self):
# 1. read one line
sLine = self.m_oFile.readline() # read next line
if not sLine: # if eof symbol met
return None # return
# 2. analyse this line
uLine = sLine.strip().decode(self.m_sEncoding) # find unicode
lLine = [sCharacter.encode(self.m_sEncoding) for sCharacter in uLine]
return lLine
# ================================================================
#
# CPennTaggedSentenceReader - the tagged sentence reader
#
# ================================================================
class CPennTaggedSentenceReader(object):
# ----------------------------------------------------------------
#
# __init__ - initialisation
#
# Inputs: sPath - the file for reading
#
# ----------------------------------------------------------------
def __init__(self, sPath):
self.m_sPath = sPath
self.m_oFile = open(sPath, encoding='utf-8')
# ----------------------------------------------------------------
#
# __del__ - destruction
#
# ----------------------------------------------------------------
def __del__(self):
self.m_oFile.close()
# ----------------------------------------------------------------
#
# readNonEmptySentence - read the next sentence
#
# Input: bIgnoreNoneTag - ignore _-NONE- tagged word?
#
# Returns: list of word, tag pairs or None if the EOF symbol met.
#
# ----------------------------------------------------------------
def readNonEmptySentence(self, bIgnoreNoneTag):
# 1. read one line
sLine = "\n" # use a pseudo \n to start
while sLine: # while there is a line
sLine = sLine.strip() # strip the line
if sLine: # if the line isn't empty
break # break
sLine = self.m_oFile.readline() # read next line
if not sLine: # if eof symbol met
return None # return
# 2. analyse this line
lLine = sLine.strip().split(" ")
lNewLine = []
for nIndex in range(len(lLine)):
tTagged = tuple(lLine[nIndex].split("_"))
assert(len(tTagged) < 3)
if len(tTagged) == 1:
tTagged = (tTagged[0], "-NONE-")
# if we take -NONE- tag, or if we find that the tag is not -NONE-
if (bIgnoreNoneTag == False) or (tTagged[0]):
lNewLine.append(tTagged)
return lNewLine
# ----------------------------------------------------------------
#
# readNonEmptySentence - read the next sentence
#
# Input: bIgnoreNoneTag - ignore _-NONE- tagged word?
#
# Returns: list of word, tag pairs or None if the EOF symbol met.
#
# ----------------------------------------------------------------
def readSentence(self, bIgnoreNoneTag):
# 1. read one line
sLine = self.m_oFile.readline() # read next line
if not sLine: # if eof symbol met
return None # return
# 2. analyse this line
lLine = sLine.strip().split(" ")
lNewLine = []
for nIndex in range(len(lLine)):
tTagged = tuple(lLine[nIndex].split("_"))
assert(len(tTagged) < 3)
if len(tTagged) == 1:
tTagged = (tTagged[0], "-NONE-")
# if we take -NONE- tag, or if we find that the tag is not -NONE-
if (bIgnoreNoneTag == False) or (tTagged[0]):
lNewLine.append(tTagged)
return lNewLine
# ================================================================
#
# Global.
#
# ================================================================
g_sInformation = "\nThe evaluation program for Chinese Tagger. \n\n\
Yue Zhang 2006\n\
Computing laboratory, Oxford\n\n\
evaluate.py candidate_text reference_text\n\n\
The candidate and reference text need to be files with tagged sentences. Each sentence takes one line, and each word is in the format of Word_Tag.\n\n\
"
# ----------------------------------------------------------------
#
# evaluateSentence - evaluate one sentence
#
# Input: tCandidate - candidate sentence
# tReference
#
# Return: int for correct words
#
# ----------------------------------------------------------------
def evaluateSentence(lCandidate, lReference):
nCorrectWords = 0
nCorrectTags = 0
nChar = 0
indexCandidate = 0
indexReference = 0
while lCandidate and lReference:
if lCandidate[0][0] == lReference[0][0]: # words right
nCorrectWords += 1
if lCandidate[0][1] == lReference[0][1]: # tags
nCorrectTags += 1
indexCandidate += len(lCandidate[0][0]) # move
indexReference += len(lReference[0][0])
lCandidate.pop(0)
lReference.pop(0)
else:
if indexCandidate == indexReference:
indexCandidate += len(lCandidate[0][0]) # move
indexReference += len(lReference[0][0])
lCandidate.pop(0)
lReference.pop(0)
elif indexCandidate < indexReference:
indexCandidate += len(lCandidate[0][0])
lCandidate.pop(0)
elif indexCandidate > indexReference:
indexReference += len(lReference[0][0]) # move
lReference.pop(0)
return nCorrectWords, nCorrectTags
# ----------------------------------------------------------------
#
# evaluateSentence - evaluate all sentences
#
# Input: output_path - output path
# reference_path - reference path
#
# ----------------------------------------------------------------
def evaluate(output_path, reference_path):
import logging
sCandidate = output_path
sReference = reference_path
if not os.path.exists(sCandidate):
print("Candidate file %s does not exist.", sCandidate)
sys.exit(1)
if not os.path.exists(sCandidate):
print("Reference file %s does not exist.", sReference)
sys.exit(1)
#
# Compare candidate and reference
#
nTotalCorrectWords = 0
nTotalCorrectTags = 0
nCandidateWords = 0
nReferenceWords = 0
fReference = CPennTaggedSentenceReader(sReference)
fCandidate = CPennTaggedSentenceReader(sCandidate)
lReference = fReference.readNonEmptySentence(bIgnoreNoneTag=True)
lCandidate = fCandidate.readNonEmptySentence(bIgnoreNoneTag=True)
while lReference and lCandidate:
n = len(lCandidate)
nCandidateWords += len(lCandidate)
nReferenceWords += len(lReference)
nCorrectWords, nCorrectTags = evaluateSentence(lCandidate, lReference)
nTotalCorrectWords += nCorrectWords
nTotalCorrectTags += nCorrectTags
lReference = fReference.readNonEmptySentence(bIgnoreNoneTag=True)
lCandidate = fCandidate.readNonEmptySentence(bIgnoreNoneTag=True)
if (lReference and not lCandidate) or (lCandidate and not lReference):
print("Warning: the reference and the candidate consists of different number of lines!")
word_precision = float(nTotalCorrectWords) / float(nCandidateWords)
word_recall = float(nTotalCorrectWords) / float(nReferenceWords)
tag_precision = float(nTotalCorrectTags) / float(nCandidateWords)
tag_recall = float(nTotalCorrectTags) / float(nReferenceWords)
word_fmeasure = (2 * word_precision * word_recall) / \
(word_precision + word_recall)
if tag_precision + tag_recall == 0:
tag_fmeasure = 0.0
else:
tag_fmeasure = (2 * tag_precision * tag_recall) / \
(tag_precision + tag_recall)
print("precision: %.3f" % word_precision)
print("recall: %.3f" % word_recall)
print("F1: %.3f" % word_fmeasure)
print("-------------------------------------------------------------")
return word_precision, word_recall, word_fmeasure
# ================================================================
#
# Main.
#
# ================================================================
# if __name__ == '__main__':
# evaluate('test_seg_data/avg-test-seg-data_beam-size-16.txt',
# 'data/filter_test.txt')