/
research_algorithm.py
364 lines (248 loc) · 11.4 KB
/
research_algorithm.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
# -*- coding: utf-8 -*-
# Copyright © 2017 Gianluca D'Amico
# University La Sapienza Rome, Latina, Italy
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from predicate_class import predicate
from predicate_node_class import predicateNode
from supertoken_class import predicateSuperToken
# define method to find predicates in a description
# - superTokenList is the tokenize description
# - predicateDictionary is dictionay which contain predicates trees
def findPredicates( superTokenList , predicateDictionary ):
# initialize description which is returned
newDescription = ""
# initialize mapping which is returned
mappingTag = []
# initialize ambiguity list which is returned
ambiguityList = []
# initialize predicate founded list which is returned
foundedList = {}
# extract length of the list
listLen = len(superTokenList)
# index to parse the list
indexList = 0
# start loop into the list
while indexList < listLen :
# extract current superToken
currentSuperToken = superTokenList[ indexList ]
# check if current superToken is a stopWord
if currentSuperToken.isaStopWord() :
# add to description current token
newDescription = newDescription + currentSuperToken.getToken() + " "
# go on next superToken in list
indexList = indexList+1
else:
# otherwise start the research
# check if lemma of superToken is not a predicateDicionary's key
if currentSuperToken.getLemma() not in predicateDictionary:
# add current token to description
newDescription = newDescription + currentSuperToken.getToken() + " "
# go on next superToken in list
indexList = indexList+1
else:
# otherwise keep on searching into the tree
# initialize candidate
candidate = None
# initialize candidate ambiguity
candidateAmbiguos = False
# initialize index from which candidate start
indexCandidateStart = superTokenList.index(currentSuperToken)
# initialize variable which define how many token the candidate has
offSet = 0
# extract root node from which start candidate research
currentNode = predicateDictionary[currentSuperToken.getLemma()]
# initialize variable to know when stop the research
stopFind = False
# initialize variable to parse internal tree of dictionary
analyzedSuperToken = currentSuperToken
# loop into tree untill one of this event happend
# - currentNode have no child
# - analyzedSuperToken is not a key of ChildrenDictionary of currentNode
# - analyzedSuperToken is NULL ( end of list ) or a punctuation mark
while not stopFind:
# check if node is a tail ( last token of a predicate )
if currentNode.isaTail():
# save possile candidate
candidate = currentNode.getSuperToken().getPredicate()
# save current offset
offSetCandidate = offSet
# check if node is ambiguos
if currentNode.ambiguity():
# save ambiguity
candidateAmbiguos = True
# save ambiguos list
ambiguosList = currentNode.getAmbiguityList()
# extract index of analyzedSuperToken
analyzedIndex = superTokenList.index(analyzedSuperToken)
# check if currentNode have children and if next superToken of analyzedSuperToken is not NULL and if is not a punctuation mark
if currentNode.haveChildren() and not analyzedSuperToken.nextIsaPunctuationMarkOrNull( superTokenList ) :
# extract next superToken
nextAnalyzedSuperToken = superTokenList[analyzedIndex+1]
# check if nextAnalyzedSuperToken is not a stopWord
if not nextAnalyzedSuperToken.isaStopWord() :
# go on nextAnalyzedSuperToken
analyzedSuperToken = nextAnalyzedSuperToken
# check if analyzedSuperToken is not a key of childrenDictionary of current node
if not currentNode.containNode(analyzedSuperToken.getLemma()) :
# if it is stop research
stopFind = True
else:
# otherwise go on next one superToken
# increase offSet
offSet=offSet+1
# go on node which have lemma of analyzedSuperToken as a key
currentNode = currentNode.getChild(analyzedSuperToken.getLemma())
# go on research
else:
# otherwise ignore nextAnalyzedSuperToken extracting it without changing node
analyzedSuperToken = nextAnalyzedSuperToken
# increase offSet
offSet=offSet+1
# go on research
else:
# otherwise stop research because current node have no children or list is finshed or next superToken is a punctuation mark
stopFind = True
# check if a candidate was found
if candidate != None:
# check if candidate is ambiguos
if candidateAmbiguos:
# manage ambiguity
betterCandidate = manageAmbiguity( candidate,offSetCandidate,indexCandidateStart,superTokenList,ambiguosList )
# check if better candidate is not None
if betterCandidate != None:
supportList = ambiguosList[:]
supportList.append(candidate)
# append reference to token sequence , ambiguity list with slice so it can be changed without changing ambiguous list, and the candidate choosen
ambiguityList.append( { "indexStart":indexCandidateStart , "offSet":offSetCandidate , "candidateList":supportList , "candidate":betterCandidate } )
# change candidateand save the ambiguity to return it
candidate = betterCandidate
else:
# otherwise
supportList = ambiguosList[:]
supportList.append(candidate)
# append reference to token sequence , ambiguity list with slice so it can be changed without changing ambiguous list, and the candidate choosen
ambiguityList.append( { "indexStart":indexCandidateStart , "offSet":offSetCandidate , "candidateList":supportList , "candidate":candidate } )
# check if candidate was already founded
if candidate.getName() in foundedList:
# number of already founded predicate with this name
numFounded = foundedList[candidate.getName()]
# increase nuber of occurancy
# foundedList[candidate.getName()] = numFounded + 1
# go on research from token over the offset
indexList = indexCandidateStart + offSetCandidate + 1
tokenSequence = ""
# add token to description
for i in range(0,offSetCandidate):
# add token
tokenSequence = tokenSequence+superTokenList[indexCandidateStart + i].getToken()+" "
# add last token
tokenSequence = tokenSequence+superTokenList[indexCandidateStart + offSetCandidate].getToken()
# insert string in description
newDescription = newDescription+tokenSequence+" "
else:
# create a new key of the list
foundedList[candidate.getName()] = 1
# save new description
if superTokenList[indexCandidateStart+ offSetCandidate].isaStopWord():
offSetCandidate = offSetCandidate - 1
# create string to insert in description
stringList = extractStringOfPredicate( candidate,offSetCandidate,indexCandidateStart,superTokenList )
# insert string in description
newDescription = newDescription+stringList[0]
# add change to mappingList
mappingTag.append( [candidate.getMapString(stringList[1]) ,indexCandidateStart,indexCandidateStart+offSetCandidate ] )
# go on research from token over the offset
indexList = indexCandidateStart + offSetCandidate + 1
else:
# otherwise go on research from next token
# insert current token in description
newDescription = newDescription+currentSuperToken.getToken()+" "
# go on next superToken
indexList = indexList+1
return [newDescription,mappingTag,ambiguityList,foundedList]
def extractStringOfPredicate( predicato,offset,startIndex,superList ):
stringList = []
stringList.append('[['+predicato.getType()+'|'+predicato.getName()+'|')
tokenSequence = ""
for i in range(0,offset):
# add token
tokenSequence = tokenSequence+superList[startIndex + i].getToken()+" "
# add last token
tokenSequence = tokenSequence+superList[startIndex + offset].getToken()
stringList[0] = stringList[0]+tokenSequence+']]'+' '
stringList.append(tokenSequence)
return stringList
def manageAmbiguity( candidate,offSetCandidate,indexCandidateStart,superTokenList,ambiguosList ):
ambiguosSuperTokenList = []
# extract predicateSuperTokenList from ambiguosList
for predicate in ambiguosList:
ambiguosSuperTokenList.append( predicate.getSuperTokenList() )
# define list of verbal tense
presentTense = ["VER:pres","VER:cond","VER:cpre","VER:geru","VER:infi","VER:impe","VER:ppre","VER:pper"]
pastTense = ["VER:impf","VER:cimp","VER:remo","VER:ppre"]
futureTense = ["VER:futu"]
differenceTense = []
differenceTense.append({})
# variable to know when stop while loop
differenceFounded = False
# for all ambiguos superToken, search why they are different in term of verbal tense
for superToken in candidate.getSuperTokenList():
# variable to parse all super token
indexToken = 0
# check if current superToken is a verb
if "VER" in superToken.getchosenGroup():
# for each superToken ambiguos
while not differenceFounded:
# variable to parse all super token
indexAmbiguos = 0
# for each ambiguosSuperTokenList in ambiguos list
for tokenList in ambiguosSuperTokenList:
# check if verbal tense is different
if superToken.getchosenGroup() != tokenList[indexToken].getchosenGroup():
# if first elemet in list is empty
if differenceTense[0] == {}:
# save tense of candidate
differenceTense[0] = {"tense":superToken.getchosenGroup()}
differenceTense.append( {"tense":tokenList[indexToken].getchosenGroup(),"index":indexAmbiguos } )
differenceFounded = True
indexAmbiguos = indexAmbiguos+1
indexToken = indexToken+1
betterCandidate = None
# if it found some difference
if differenceFounded:
index = 0
# solve the difference
# for each different probably candidate
for tense in differenceTense:
# check what is the tense of superToken in superTokenList
if superTokenList[indexCandidateStart + indexToken-1].getchosenGroup() in presentTense and tense["tense"] in presentTense:
# if is not first element
if index != 0:
# save better candidate
betterCandidate = ambiguosList[tense["index"]]
# check what is the tense of superToken in superTokenList
if superTokenList[indexCandidateStart + indexToken-1].getchosenGroup() in pastTense and tense["tense"] in pastTense:
# if is not first element
if index != 0:
# save better candidate
betterCandidate = ambiguosList[tense["index"]]
# check what is the tense of superToken in superTokenList
if superTokenList[indexCandidateStart + indexToken-1].getchosenGroup() in futureTense and tense["tense"] in futureTense:
# if is not first element
if index != 0:
# save better candidate
betterCandidate = ambiguosList[tense["index"]]
index = index + 1
if betterCandidate != None:
return betterCandidate
else:
return None