-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanaphoramllib.py
214 lines (174 loc) · 6.84 KB
/
anaphoramllib.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
#!/usr/bin/python2.7
# -!- coding: utf-8 -!-
# usage: anaphoramllib.py
import os, sys, codecs, re
import cPickle
import lemmatizer
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn import tree, svm
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline
classifiers = {
'rf': RandomForestClassifier(n_estimators = 150, min_samples_split = 2, n_jobs = -1),
'dt': tree.DecisionTreeClassifier(),
'svm': svm.SVC(),
'part': ExtraTreesClassifier(n_estimators = 30, min_samples_split = 2, n_jobs = -1),
'featureselection': Pipeline([
('feature_selection', LinearSVC(penalty="l1", dual = False)),
('classification', RandomForestClassifier())
])
}
def LoadPronouns(filename):
pronouns = {}
pronounIndex = {}
inpFile = codecs.open(filename, encoding = 'utf-8')
for line in (line_raw.strip('\r\n') for line_raw in inpFile):
pronounType, pronounList = line.split('\t', 1)
pronouns[pronounType] = [item for item in line.split('\t')]
for item in pronouns[pronounType]:
pronounIndex[item] = pronounType
return pronouns
def GetClassNumber(name, labels):
if not name in labels:
labels[name] = len(labels)
return labels[name]
def Intersects(offset1, offset2):
return (offset1[0] <= offset2[0] and not (offset1[0] + offset1[1]) < (offset2[0])) or \
(offset2[0] <= offset1[0] and not (offset2[0] + offset2[1]) < (offset1[0]))
class AnaphoraResolutorML:
def __init__(self):
self.classifier = None
self.anaphWnd = 20
self.cataphWnd = 0
self.labels = {}
def LoadModel(self, model, labels):
with open(model, 'rb') as inpFile:
self.classifier = cPickle.load(inpFile)
with open(labels, 'rb') as inpFile:
self.labels = cPickle.load(inpFile)
def LoadPronouns(self, pronouns):
self.pronouns = pronouns
self.pronounIndex = {}
for pronounType in pronouns:
for pronoun in pronouns[pronounType]:
self.pronounIndex[pronoun] = pronounType
def SetWindow(self, anaphWnd, cataphWnd):
self.anaphWnd = anaphWnd
self.cataphWnd = cataphWnd
def FindAntecedent(self, targetGroup, groups):
threshold = 0.3
if not self.classifier:
return None
if not targetGroup[1] in self.pronounIndex:
return None
candidates = self.GetCandidates(groups, targetGroup)
features = []
for candidate in candidates:
features.append(self.GetFeaturesVector(groups, nCandidate = candidate[0], nPronoun = groups.index(targetGroup)))
results = self.classifier.predict_proba(features)
resultsSimple = self.classifier.predict(features)
# MAGIIIC. Returning three most probable candidates
return sorted((candidates[i] + (results[i][1],) for i in range(len(results)) if results[i][1] >= threshold), key = lambda x: x[-1], reverse = True)[:3]
def Resolute(self, groups):
for group in groups:
if group[1] in pronounIndex:
results = self.FindAntecedent(group, groups)
print results
def TrainModel(self, features, target, classifier = 'rf'):
if not classifier in classifiers:
return False
#print features, target
self.classifier = classifiers[classifier]
self.classifier.fit(features, target)
return True
def SaveModel(self, filename):
if not self.classifier:
return
with open(filename, 'wb') as outFile:
cPickle.dump(self.classifier, outFile)
with open(filename + '.labels', 'wb') as outFile:
cPickle.dump(self.labels, outFile)
def GetCandidates(self, words, group):
candidates = []
prnIndex = words.index(group)
if prnIndex == -1:
prnIndex = len(words) - 1
for i in range(prnIndex - self.anaphWnd, prnIndex + self.cataphWnd):
if 0 <= i < len(words):
if i == prnIndex:
continue
if lemmatizer.posFilters['noun'](words[i]) or lemmatizer.posFilters['pronoun'](words[i]):
candidates.append((i, words[i] + [i - prnIndex]))
return candidates
def GetFeaturesVector(self, groups, nCandidate, nPronoun):
indexFrom = groups[nCandidate][4]
length = groups[nCandidate][5]
indexTo = indexFrom + length
pronounIndexFrom = groups[nPronoun][4]
pronounLength = groups[nPronoun][5]
pronounIndexTo = pronounIndexFrom + pronounLength
nSymbols = length
nWords = len(groups[nCandidate][0].split(' '))
distInSymbols = pronounIndexFrom - indexTo if nPronoun > nCandidate else -(indexFrom - pronounIndexTo)
distInGroups = nPronoun - nCandidate
distInWords = sum(len(group[0].split(' ')) for group in groups[min(nPronoun, nCandidate):max(nPronoun, nCandidate)])
#len(text[indexTo:pronounIndexFrom].split(' ')) if nPronoun > nCandidate else -len(text[pronounIndexTo:indexFrom].split(' '))
numberCandidate = groups[nCandidate][2][3]
numberPronoun = groups[nPronoun][2][2]
caseCandidate = groups[nCandidate][2][2]
casePronoun = groups[nPronoun][2][1]
agrNumber = numberCandidate == numberPronoun
agrCase = caseCandidate == casePronoun
isName = groups[nCandidate][2][6] != '0'
isAnimate = groups[nCandidate][2][5] == 'A'
nGroupsLikeThis = len([item for item in groups if item[1] == groups[nCandidate][1]])
#print nGroupsLikeThis, groups[nCandidate][0]
pronounType = self.pronounIndex[groups[nPronoun][1]] if groups[nPronoun][1] in self.pronounIndex else 'UNK'
pronounText = groups[nPronoun][0]
# nSymbols — length of antecedent candidate in symbols
# nWords — length of antecedent candidate in words (groups)
# distInGroups — distance in groups
# distInSymbols — distance in symbols
# distInWords — distance in words
# numberCandidate — grammatical number of antecedent candidate
# numberPronoun — grammatical number of a pronoun
# BOOL agrNumber — do they agree by number
# caseCandidate — grammatical case of antecedent candidate
# casePronoun — grammatical case of a pronoun
# agrCase — BOOL do they agree by case
# isName
# isAnimate
# BOOL isName — is antecedent candidate a name
# pronounType — type of a prnoun
# pronoun — pronoun wordform
# isRightAnswer — is this candidate a real antecedent for this pronoun
return [nSymbols,
nWords,
distInGroups,
distInSymbols,
distInWords,
GetClassNumber(numberCandidate, self.labels),
GetClassNumber(numberPronoun, self.labels),
int(agrNumber),
GetClassNumber(caseCandidate, self.labels),
GetClassNumber(casePronoun, self.labels),
int(agrCase),
int(isName),
#int(isAnimate),
nGroupsLikeThis,
GetClassNumber(pronounType, self.labels),
GetClassNumber(pronounText, self.labels)]
"""
return (
(
(indexFrom, indexTo),
(pronounIndexFrom, pronounIndexTo)
),
(
'\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%d\t%s\t%s\t%d\t%d\t%s\t%s\t%d' %
(
inputFilename, indexFrom, length, pronounIndexFrom, pronounLength, nSymbols, nWords, distInGroups, distInSymbols, distInWords, numberCandidate, numberPronoun, int(agrNumber), caseCandidate, casePronoun, int(agrCase), int(isName), pronounType, pronounText, int(isRightAnswer)
)
)
)
"""