-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanaphora_engine.py
224 lines (213 loc) · 8.48 KB
/
anaphora_engine.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
#!/usr/bin/python
#coding: utf-8
# An@phora resolution
import codecs,sys
from lemmatizer import lemmatizer
from lemmatizer import GetGroups
from lemmatizer import GetConjunctions
import datetime,os
def printbrat(antecedent,anaphora,filename):
(token,offset,length) = antecedent
offset_end = offset+length
(an_token,an_offset,an_length,an_type,an_count) = anaphora
if an_token == u'её':
an_token = u'ее'
an_offset_end = an_offset+an_length
ent_number_antecedent = an_count+(an_count-1)
ent_number_anaphora = ent_number_antecedent+1
output = codecs.open('/var/www/brat/data/anaphora/%s' % filename+'.ann','a','utf-8')
output.write('T%d\tantecedent %d %d\t%s\n' % (ent_number_antecedent,offset,offset_end,token))
output.write('T%d\t%s %d %d\t%s\n' % (ent_number_anaphora,an_type,an_offset,an_offset_end,an_token))
output.write('R%d\tanaphora Arg1:T%d Arg2:T%d\n' % (an_count,ent_number_anaphora,ent_number_antecedent))
def anaphora_res(text,window):
text = text.replace(u' её',u' ее')
os.chdir('/var/www/anaphora')
d = datetime.datetime.now()
filename = d.strftime("%d.%m.%Y%I-%M%S")
output = codecs.open('/var/www/brat/data/anaphora/%s' % filename+'.txt','w','utf-8')
output.write(text)
output.close()
open('/var/www/brat/data/anaphora/%s' % filename+'.ann', 'a').close()
pronouns = []
reflexives = []
relatives = []
for word in codecs.open('prons.txt','r','utf-8'):
pronouns.append(word.strip())
for word in codecs.open('reflexives.txt','r','utf-8'):
reflexives.append(word.strip())
for word in codecs.open('relatives.txt','r','utf-8'):
relatives.append(word.strip())
anaphora_count = 0
curOffset = 0
words = []
res = text
processed, curOffset = lemmatizer(res, startOffset = curOffset)
for i in processed:
found = False
(token,lemma,tag,prob,offset) = i
words.append(i)
if len(words) > window:
dif = len(words) - window
words = words[dif:]
if lemma in pronouns:
ab = GetGroups(words)
previous_nouns = [word for word in ab if word[2].startswith('N') and not '.' in word[0]]
#print 'Pronoun',token+'\t'+tag+'\t'+lemma
if lemma == u"его" and tag.startswith('R'):
for w in reversed(previous_nouns):
if w[2][4] != "F" and w[2][3] == tag[2]:
#print w[0]+'\t'+w[1]+'\t'+str(w[2])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
continue
elif lemma == u"он" or lemma == u"она" or lemma == u'они' or lemma == u'их' or lemma == u'оно':
if token == u"Ним":
continue
if tag[3] == "F":
for w in reversed(previous_nouns):
if w[2][4] == "F":
if w[2][2] == "N" and w[2][5] == "A" and w[2][3] == tag[2]:
#print w[0]+'\t'+w[2]+'\t'+str(w[2])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
found = True
break
if found == False:
for w in reversed(previous_nouns):
if w[2][4] == "F" and w[2][3] == tag[2]:
#print w[0]+'\t'+w[2]+'\t'+str(w[2])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
if tag[3] != "F":
for w in reversed(previous_nouns):
if w[2][2] == "N" and w[2][5] == "A" and w[2][3] == tag[2] and w[2][4] != "F":
if tag[1] == "N" and tag[2] == "S" and w[2][4] != tag[3] and w[2][4] != "C":
continue
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
found = True
break
if found == False:
for w in reversed(previous_nouns):
if w[2][3] == tag[2]:
if tag[2] == 'S' and w[2][4] == "F":
continue
if tag[1] == "N" and tag[2] == "S" and w[2][4] != tag[3] and w[2][4] != "C":
continue
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
continue
elif lemma == u"мой":
previous_pronouns = [word for word in ab if word[2].startswith('E') and word[2][5] == "1" and not '.' in word[0]]
for w in reversed(previous_pronouns):
if tag[2] == "S" and w[2][2] == "P":
continue
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
continue
else:
for w in reversed(previous_nouns):
if w[2][3] == 'P' and tag[2] == 'P':
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
if w[2][3] == 'S' and tag[2] == 'S':
if w[2][4] == "F" and tag[3] == "F":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
if w[2][4] != "F" and tag[3] != "F":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'pronoun',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
elif lemma in reflexives:
ab = GetGroups(words)
previous_nouns = [word for word in ab if word[2].startswith('N') or word[2].startswith('E') and not '.' in word[0]]
#print 'Reflexive',token+'\t'+tag+'\t'+lemma
if lemma == u"себе":
if words[-2][1] == u'сам':
continue
previous_nouns = previous_nouns[:-1]
for w in reversed(previous_nouns):
if w[0] != u"что" and w[0] != u"Что":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'reflexive',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
elif lemma == u"свой":
for w in reversed(previous_nouns):
if w[2][2] == "N" and w[2][0] == "N" or w[2][0] == "E" and w[2][1] == "N":
if w[0] != u"что" and w[0] != u"Что":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'reflexive',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
elif lemma in relatives:
ab = GetGroups(words)
previous_nouns = [word for word in ab if word[2].startswith('N') or word[2].startswith('Fc')]
#print 'Relatives',token+'\t'+tag+'\t'+lemma
#for i in ab:
# print i[0],i[2]
comma = 0
for w in reversed(previous_nouns):
if w[0] == ',':
comma = 1
continue
if comma == 1:
if w[2].startswith('N'):
if w[2][3] == 'P':
if tag[2] == 'P' or token == u"которым":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'relative',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
if w[2][3] == 'S' and tag[2] == 'S':
if w[2][4] == "F" and tag[3] == "F":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'relative',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
if w[2][4] != "F" and tag[3] != "F":
#print w[0]+'\t'+w[2]+'\t'+str(w[4])+'\t<---\t'+token+'\t'+str(offset)
anaphora_count += 1
antecedent = (w[0],w[4],w[5])
anaphora = (token,offset,len(token),'relative',anaphora_count)
printbrat(antecedent,anaphora,filename)
break
return filename