-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.py
164 lines (135 loc) · 5.67 KB
/
Evaluator.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
import re,os
class Evaluator:
def __init__(self):
self.filename = ""
self.truePos = {}
self.classified = {}
self.trueCount = {}
self.evalDicts = [self.truePos,self.classified,self.trueCount]
self.evalAttr = ["location", "speaker","stime","etime","paragraph","sentence"]
self.unTagged = ""
self.speakers, self.location, self.stime, self.etime, self.para, self.sent = "","","","","",""
self.originalValues = {}
self.newValues = {}
#Initialise dictionaries
for i in self.evalDicts:
for x in self.evalAttr:
i[x] = 0
def getFromTagged(self,email=None):
if email:
contents = email
else:
with open(self.filename,'r') as f:
contents = f.read()
speakers = self.extract("speaker",contents)
location = self.extract("location",contents)
stime = self.extract("stime",contents)
etime = self.extract("etime",contents)
para = self.extract("paragraph",contents)
sent = self.extract("sentence",contents)
allTags = re.compile(r'<([a-z/>]*)>')
self.unTagged = re.sub(allTags,"",contents)
return speakers,location,stime,etime,para,sent
def extract(self,type,data,optReg=None):
if optReg:
regex = re.compile(optReg)
else:
regexString = r'<{}>([\d\D]*?)<\/{}>'.format(type,type)
regex = re.compile(regexString)
regexResults = re.findall(regex,data)
return regexResults
def truePositives(self,type, correctVals, testVals):
self.classified[type] += len(testVals)
self.trueCount[type] += len(correctVals)
if any(isinstance(x, (tuple)) for x in correctVals):
correctVals = [x[0] for x in correctVals]
else:
correctVals = [x.strip(' ') for x in correctVals]
if any(isinstance(x, (tuple)) for x in testVals):
testVals = [x[0] for x in testVals]
else:
testVals = [x.strip(' ') for x in testVals]
counter = 0
if type == "sentence":
allTags = re.compile(r'<([a-z/>]*)>')
correctVals = [re.sub(allTags, "", x.rstrip('\n')) for x in correctVals]
testVals = [re.sub(allTags, "", x.rstrip('\n')[:-1]) for x in testVals]
if type == "paragraph":
allTags = re.compile(r'<([a-z/>]*)>')
correctVals = [re.sub(allTags, "", x.rstrip('\n')) for x in correctVals]
testVals = [re.sub(allTags, "", x.rstrip('\n')) for x in testVals]
#print("Test vals: " + str(testVals))
#print("Correct vals : " + str(correctVals))
if correctVals:
for i in testVals:
if i == correctVals[counter] or i in correctVals or i in correctVals[counter]:
self.truePos[type] += 1
counter+=1
if len(correctVals) == counter:
break
else:
if testVals == correctVals:
self.truePos[type] += 1
self.classified[type] += 1
self.trueCount[type] += 1
def precision(self,type):
truePosNumber = self.truePos[type]
classifiedNum = self.classified[type]
if classifiedNum > 0:
return round(truePosNumber/classifiedNum,2)
else:
return 0
def recall(self,type):
truePosNumber = self.truePos[type]
trueCountNumber = self.trueCount[type]
if trueCountNumber > 0:
return round(truePosNumber/trueCountNumber,2)
else:
return 0
def fMeasure(self,type):
precis = self.precision(type)
recall = self.recall(type)
if (precis+recall) > 0:
divSum = round((precis * recall) / (precis + recall),2)
else:
divSum = 0
return divSum * 2
def evaluate(self,email):
newEmail = email.newHeader + email.newBody
Nspeakers, Nlocation, Nstime, Netime, Npara, Nsent = self.getFromTagged(newEmail)
self.newValues["location"] = Nlocation
self.newValues["speaker"] = Nspeakers
self.newValues["stime"] = Nstime
self.newValues["etime"] = Netime
self.newValues["paragraph"] = Npara
self.newValues["sentence"] = Nsent
for i in self.evalAttr:
originalVal = self.originalValues[i]
generatedVal = self.newValues[i]
self.truePositives(i,originalVal,generatedVal)
def evalResults(self):
for i in self.evalAttr:
print("----" + str(i) + "----")
print("Recall" + ": " + str(self.recall(i)))
print("Precision" + ": " + str(self.precision(i)))
print("F measure" + ": " + str(self.fMeasure(i)))
print()
def getFileToTag(self,filename):
self.filename = filename
self.speakers, self.location, self.stime, self.etime, self.para, self.sent = self.getFromTagged()
self.originalValues["location"] = self.location
self.originalValues["speaker"] = self.speakers
self.originalValues["stime"] = self.stime
self.originalValues["etime"] = self.etime
self.originalValues["paragraph"] = self.para
self.originalValues["sentence"] = self.sent
folder = os.path.dirname(self.filename)
filename = (os.path.basename(os.path.splitext(filename)[0]))
newFolder = folder + "\\untagged\\"
if not os.path.exists(newFolder):
os.makedirs(newFolder)
newFileName = newFolder + filename + "-untagged.txt"
file = open(newFileName, 'w')
file.write(self.unTagged)
file.close()
return newFileName