forked from iacercalixto/coreference_resolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
284 lines (229 loc) · 10.1 KB
/
parser.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
import os, sys
from xml.dom.minidom import parse, parseString
from resources import Word, Markable, Paragraph, Sentence, Document
import time
from multiprocessing import Process
class CorefParser:
main_dir_name = 'data'
def __init__(self):
pass
def __handleMarkable(self, xmlMarkable, xmlMarkableParent, s, nextWordIndex, appendString="...."):
# The first element in the slice of this markable
initialWordIndex = nextWordIndex
# For all the possible child nodes inside a markable,
# parse them
if xmlMarkable.hasChildNodes():
for xmlNode in xmlMarkable.childNodes:
# If we've found a word inside this markable, parse it
# an increase the word count only
if xmlNode.nodeName == 'W':
nextWordIndex += 1
w = Word()
self.__handleWord(xmlNode, w)
s.addWord(w)
w.sentence = s
#print '....added word id=' + str(xmlNode.attributes['ID'].nodeValue)
# If we found a nested markable inside this markable
if xmlNode.nodeName == 'MARKABLE':
# Update the nextWordIndex according to the index updated by the
# call of self.__handleMarkable recursively
nextWordIndex = self.__handleMarkable(xmlNode, xmlMarkable, s, nextWordIndex, appendString+"....")
#numWords = finalWordIndex
else:
raise Exception("There should be at least one word inside the markable id="+
xmlMarkable.attributes['ID'].nodeValue)
# Create the markable
m = Markable()
m.idx = xmlMarkable.attributes['ID'].nodeValue
m.comment = xmlMarkable.attributes['COMMENT'].nodeValue
# Create the slice using the words' indexes in the sentence
m.slice = str(initialWordIndex) + ":" + str(nextWordIndex-1)
m.sentence = s
# Every markable, no matter how deeply nested inside another markables,
# will always be added to a sentence. They'll be accessible and identifiable
# by their word slices.
# Add the markable to the sentence
s.addMarkable(m)
# TO-DO: handle CoRef
return nextWordIndex
def __handleWord(self, xmlWord, word):
#w = Word()
w = word
# All attributes exists (defaults behaviour)
if (xmlWord.attributes.length==5):
w.idx = xmlWord.attributes['ID'].nodeValue
w.dep = xmlWord.attributes['DEP'].nodeValue
w.func = xmlWord.attributes['FUNC'].nodeValue
w.lemma = xmlWord.attributes['LEMMA'].nodeValue
w.postag = xmlWord.attributes['POS'].nodeValue
else:
for i in range(xmlWord.attributes.length):
attrName = xmlWord.attributes.item(i).name
if attrName == 'ID':
w.idx = xmlWord.attributes['ID'].nodeValue
if attrName == 'DEP':
w.dep = xmlWord.attributes['DEP'].nodeValue
if attrName == 'FUNC':
w.func = xmlWord.attributes['FUNC'].nodeValue
if attrName == 'LEMMA':
w.lemma = xmlWord.attributes['LEMMA'].nodeValue
if attrName == 'POS':
w.postag = xmlWord.attributes['POS'].nodeValue
#print xmlWord.nodeName
#print xmlWord.hasChildNodes()
#print xmlWord.nodeValue
w.surfaceForm = xmlWord.childNodes[0].data
print w, w.surfaceForm
return w
def __handleSentence(self, xmlSentence, sentence):
#s = Sentence()
s = sentence
s.idx = xmlSentence.attributes['ID'].nodeValue
numWords = 0
if xmlSentence.hasChildNodes():
for xmlNode in xmlSentence.childNodes:
print 'xmlNode.nodeName: '+xmlNode.nodeName
if xmlNode.nodeName == 'MARKABLE':
finalWordIndex = self.__handleMarkable(xmlNode, s, s, numWords)
# Add +1 to the finalWordIndex because it is decremented by
# one unit to make the slice range from index 0 to n-1
# (being n the number of words in the sentence)
numWords = finalWordIndex
if xmlNode.nodeName == 'W':
numWords += 1
w = Word()
self.__handleWord(xmlNode, w)
s.addWord(w)
w.sentence = s
print s
print 's.words: '+str(s.words)
print 's.markables: '+str(s.markables)
return s
def __handleParagraph(self, xmlParagraph, paragraph):
#p = Paragraph()
p = paragraph
p.idx = xmlParagraph.attributes['ID'].nodeValue
if xmlParagraph.hasChildNodes():
for xmlNode in xmlParagraph.childNodes:
if xmlNode.nodeName == 'S':
s = Sentence()
print 'before handle_sentence'
self.__handleSentence( xmlNode, s )
print 'after handle_sentence'
p.addSentence(s)
s.paragraph = p
print p, p.sentences
#return p
def __handleDocument(self, xmlDocument):
d = Document()
root = xmlDocument.firstChild
maxNumParag = 2
counter = 0
if root.hasChildNodes():
for xmlNode in root.childNodes:
#print 'xmlNode.nodeName '+str(xmlNode.nodeName)
if xmlNode.nodeName == 'DESC' or xmlNode.nodeName == 'TIME':
pass
if xmlNode.nodeName == 'P':
counter += 1
p = Paragraph()
print 'before hangle_paragraph'
self.__handleParagraph( xmlNode, p )
print 'after handle_paragraph'
d.addParagraph(p)
p.document = d
print p.idx
if counter >= maxNumParag:
break
print d, d.paragraphs
"""
# After all the paragraphs, therefore sentences, therefore words
# and markables have been parsed, the markables
# will have its slices relative to the word indexes in the sentence,
# being the first word index 0 and the last index numWords-1
# Transform the indexes to absolute indexes, relative to the
# word objects' ID attributes
for paragraph in d._paragraphs:
for sentence in paragraph._sentences:
for markable in sentence._markables:
sliceIndex = markable._slice
fromIndex, toIndex = sliceIndex.split(":")[0], sliceIndex.split(":")[1]
try:
fromId = sentence._words[int(fromIndex)]._id
toId = sentence._words[int(toIndex)]._id
#print fromId + " " + toId
markable._slice = fromId + ":" + toId
except ValueError:
# Value error here means that the key couldn't be converted to an int
# value, in that case it has already been converted by another
# markable and can be used "as is"
markable._slice = fromIndex + ":" + toIndex
"""
return d
def parse(self, filename='462176newsML-done.xml.xml'):
""" Filename must be an XML 'file.xml'
"""
time.clock()
print 'Parsing file ' + str(filename)
fullname = os.path.join(self.main_dir_name, filename)
self.__dom1 = parse(fullname) # parse an XML file by name
self.__parsedDocument = self.__handleDocument(self.__dom1)
print 'Time elapsed: ' + str(time.clock())
def parseString(self, string):
""" String containing a valid XML
"""
time.clock()
self.__dom1 = parseString(string) # parse an XML file by name
self.__parsedDocument = self.__handleDocument(self.__dom1)
print 'Time elapsed: ' + str(time.clock())
def getParsedDocument(self):
return self.__parsedDocument
# The number of sentences before the sentence with an NP to be search for a coreference
WINDOW_SIZE = 10
if (__name__=='__main__'):
string = """
<D ID="D1">
<P ID="P1">
<S ID="S1">
<W ID="W1">haha</W>
<W ID="W2">hehe</W>
<W ID="W3">hihi</W>
</S>
</P>
<P ID="P2">
<S ID="S2">
<W ID="W4">hoho</W>
</S>
<S ID="S3">
<W ID="W5">huhu</W>
<W ID="W6">baba</W>
</S>
<S ID="S4">
<W ID="W7">bebe</W>
</S>
</P>
</D>
"""
parser = CorefParser()
parser.parseString(string)
sentences = []
for sentence in parser.getParsedDocument().paragraphs[0].sentences:
print sentence.idx
#for paragraph in parser.getParsedDocument()._paragraphs:
# print 'checking paragraph #'+paragraph._id
# for sentence in paragraph._sentences:
# print 'adding sentence #'+sentence._id
# sentences.append(sentence)
"""
# Create a list of process with one parser for each
plist = []
for filename in os.listdir( os.path.join('data') ):
parser = CorefParser()
p = Process( target=parser.parse, args=(filename,) )
plist.append(p)
# Execute them! :)
for p in plist:
p.start()
p.join()
print 'Job done!'
"""