-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_en.py
445 lines (311 loc) · 14.3 KB
/
parse_en.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import spacy
import platform
import os
from collections import Counter
from nltk.corpus import wordnet
import configparser
import time
config = configparser.ConfigParser()
config.read('config.ini')
DIS_ACTIVE = config.getboolean('DISAMBIGUATION', 'DIS_ACTIVE')
DIS_VERB = config.get('DISAMBIGUATION', 'DIS_VERB').split(", ")
DIS_NOUN = config.get('DISAMBIGUATION', 'DIS_NOUN').split(", ")
DIS_ADJ = config.get('DISAMBIGUATION', 'DIS_ADJ').split(", ")
DIS_ADV = config.get('DISAMBIGUATION', 'DIS_ADV').split(", ")
DIS_EXCEPTIONS = config.get('DISAMBIGUATION', 'DIS_EXCEPTIONS').split(", ")
DIS_METRIC_COMPARISON = config.get('DISAMBIGUATION', 'DIS_METRIC_COMPARISON')
GMC_ACTIVE = config.getboolean('GROUNDED_MEANING_CONTEXT', 'GMC_ACTIVE')
GMC_POS = config.get('GROUNDED_MEANING_CONTEXT', 'GMC_POS').split(", ")
OBJ_JJ_TO_NOUN = config.getboolean('POS', 'OBJ_JJ_TO_NOUN')
class Parse(object):
def __init__(self, VERBOSE):
self.FILTER = ['det', 'punct', 'aux', 'auxpass', 'cc', 'case', 'intj', 'dep', 'predet', 'advcl']
self.adv_adj_POS = ['RB', 'UH', 'RP', 'PRP', 'RBS', 'JJ', 'NN', 'RBR', 'DT']
self.POS_FILTER = []
self.VERBOSE = VERBOSE
self.BLACK_LIST_WORDS = ['that', 'which', 'then']
# nlp engine instantiation
print("\nNLP engine initializing. Please wait...")
# python -m spacy download en_core_web_trf/en_core_web_lg
self.nlp = spacy.load('en_core_web_trf')
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
# enable cache usage
self.FLUSH = True
# last dependencies
self.last_deps = []
# last detected entities
self.ner = []
# last processed sentence
self.last_sentence = None
# last processed sentence
self.pending_root_tense_debt = None
# novel deps usage
self.last_enc_deps = []
# offset dictionary
self.offset_dict = {}
# Macro Semantic Table
self.MST = [[], [], [], [], [], []]
# GMC support dictionary
self.GMC_SUPP = {}
# GMC support dictionary reversed
self.GMC_SUPP_REV = {}
# Lemmas correction dictionary
self.LCD = {}
# Beginning Computational time
self.start_time = 0
def set_start_time(self):
self.start_time = time.time()
def get_comp_time(self):
assert_time = time.time() - self.start_time
return assert_time
def feed_MST(self, component, index):
self.MST[index].append(component)
def get_last_MST(self):
return self.MST
def get_pending_root_tense_debt(self):
return self.pending_root_tense_debt
def set_pending_root_tense_debt(self, d):
self.pending_root_tense_debt = d
def get_last_sentence(self):
return self.last_sentence
def get_last_ner(self):
return self.ner
def set_last_deps(self, deps):
self.last_deps = deps
def get_last_deps(self):
return self.last_deps
def get_flush(self):
return self.FLUSH
def flush(self):
self.FLUSH = True
self.last_deps = []
self.ner = []
self.MST = [[], [], [], [], [], []]
def no_flush(self):
self.FLUSH = False
def get_nlp_engine(self):
return self.nlp
def get_pos(self, s):
s_list = s.split(':')
if len(s_list) > 1:
return s_list[1]
else:
return s_list[0]
def get_lemma(self, s):
s_list = s.split('_')
if len(s_list) == 1:
result = s_list[0].split(":")[0]
else:
result = ""
for i in range(len(s_list)):
if i == 0:
result = s_list[i].split(':')[0]
else:
result = result +"_"+s_list[i].split(':')[0]
return result
def get_deps(self, input_text, LEMMATIZED):
nlp = self.get_nlp_engine()
doc = nlp(input_text)
self.last_sentence = input_text
for X in doc.ents:
ent = "("+X.label_ + ", " + X.text + ")"
self.ner.append(ent)
words_list = []
for token in doc:
words_list.append(token.text)
enc_dep = []
enc_dep.append(token.dep_)
enc_dep.append(token.head.idx)
enc_dep.append(token.idx)
counter = Counter(words_list)
offset_dict = {}
offset_dict_lemmatized = {}
for token in reversed(doc):
index = counter[token.text]
print("\nlemma in exam: ", token.lemma_)
# check for presence in Grounded Meaning Context (GMC). In this case the choosen label must be that in GMC, already found
if GMC_ACTIVE is True and token.tag_ in GMC_POS and token.lemma_ in self.GMC_SUPP:
offset_dict[token.idx] = token.text[0].upper() + token.text[1:] + "0" + str(index) + ":" + token.tag_
# offset_dict[token.idx] = token.text + "0" + str(index) + ":" + token.tag_
shrinked_proper_syn = self.GMC_SUPP[token.lemma_]
offset_dict_lemmatized[token.idx] = shrinked_proper_syn + "0" + str(index) + ":" + token.tag_
print("\n<--------------- Getting from GMC: "+token.text+" ("+shrinked_proper_syn+")")
# Otherwise a proper synset must be inferred....
elif DIS_ACTIVE and (token.tag_ in DIS_VERB or token.tag_ in DIS_NOUN or token.tag_ in DIS_ADJ or token.tag_ in DIS_ADV) and token.lemma_ not in DIS_EXCEPTIONS:
if token.tag_ in DIS_VERB:
pos = wordnet.VERB
elif token.tag_ in DIS_NOUN:
pos = wordnet.NOUN
elif token.tag_ in DIS_ADV:
pos = wordnet.ADV
else:
pos = wordnet.ADJ
# pos=VERB, NOUN, ADJ, ADV
syns = wordnet.synsets(token.text, pos=pos, lang="eng")
proper_syn = ""
proper_syn_sim = 0
proper_definition = ""
source = ""
for synset in syns:
#print("\nsynset: ", synset.name())
#print("#synset examples: ", len(synset.examples()))
# Checking vect distance from glosses
if DIS_METRIC_COMPARISON == "GLOSS" or len(synset.examples()) == 0:
doc2 = nlp(synset.definition())
sim = doc.similarity(doc2)
if sim > proper_syn_sim:
proper_syn_sim = sim
proper_syn = synset.name()
proper_definition = synset.definition()
source = "GLOSS"
elif DIS_METRIC_COMPARISON == "EXAMPLES":
# Checking vect distances from examples (wether existing)
for example in synset.examples():
doc2 = nlp(example)
sim = doc.similarity(doc2)
if sim > proper_syn_sim:
proper_syn_sim = sim
proper_syn = synset.name()
proper_definition = synset.definition()
source = "EXAMPLES"
elif DIS_METRIC_COMPARISON == "BEST":
# Checking best vect distances between gloss and examples
for example in synset.examples():
doc2 = nlp(example)
sim1 = doc.similarity(doc2)
if sim1 > proper_syn_sim:
proper_syn_sim = sim1
proper_syn = synset.name()
proper_definition = synset.definition()
source = "BEST-example"
doc2 = nlp(synset.definition())
sim2 = doc.similarity(doc2)
if sim2 > proper_syn_sim:
proper_syn_sim = sim2
proper_syn = synset.name()
proper_definition = synset.definition()
source = "BEST-gloss"
elif DIS_METRIC_COMPARISON == "AVERAGE":
# AVERAGE = average between doc2vect gloss and examples
actual_sim1 = 0
source = "AVERAGE"
for example in synset.examples():
doc2 = nlp(example)
sim1 = doc.similarity(doc2)
if sim1 > actual_sim1:
actual_sim1 = sim1
doc2 = nlp(synset.definition())
sim2 = doc.similarity(doc2)
average = (actual_sim1 + sim2) / 2
if average > proper_syn_sim:
proper_syn_sim = average
proper_syn = synset.name()
proper_definition = synset.definition()
else:
# COMBINED = similarity between doc2vect gloss+examples
source = "COMBINED"
for example in synset.examples():
combined = str(synset.definition())+" "+example
doc2 = nlp(combined)
sim1 = doc.similarity(doc2)
if sim1 > proper_syn_sim:
proper_syn_sim = sim1
proper_syn = synset.name()
proper_definition = synset.definition()
print("\nProper syn: ", proper_syn)
print("Max sim: ", proper_syn_sim)
print("Gloss: ", proper_definition)
print("Source: ", source)
shrinked_proper_syn = self.shrink(proper_syn)
self.GMC_SUPP[token.lemma_] = shrinked_proper_syn
print("\n--------------> Storing in GCM: "+token.lemma_+" ("+shrinked_proper_syn+")")
self.GMC_SUPP_REV[shrinked_proper_syn] = token.lemma_
if OBJ_JJ_TO_NOUN is True:
# taking in account of possible past adj-obj corrections
lemma = str(token.lemma_).lower()
if lemma in self.LCD:
shrinked_proper_syn = self.LCD[lemma]
print("\n<------------- Getting from LCD: "+shrinked_proper_syn+" ("+lemma+")")
offset_dict_lemmatized[token.idx] = shrinked_proper_syn + "0" + str(index) + ":" + token.tag_
offset_dict[token.idx] = token.text[0].upper() + token.text[1:] + "0" + str(index) + ":" + token.tag_
# offset_dict[token.idx] = token.text + "0" + str(index) + ":" + token.tag_
else:
lemma = str(token.lemma_).lower()
# taking in account of possible past adj-obj corrections
if OBJ_JJ_TO_NOUN is True and lemma in self.LCD:
lemma = self.LCD[lemma]
print("\n<------------- Getting from LCD: ", lemma)
# offset_dict[token.idx] = token.text+"0"+str(index)+":"+token.tag_
offset_dict[token.idx] = token.text[0].upper() + token.text[1:] + "0" + str(index) + ":" + token.tag_
offset_dict_lemmatized[token.idx] = lemma+"0"+str(index)+":"+token.tag_
counter[token.text] = index - 1
deps = []
for token in doc:
new_triple = []
new_triple.append(token.dep_)
if token.head.lemma_ == '-PRON-':
new_triple.append(offset_dict[token.head.idx])
else:
if LEMMATIZED:
new_triple.append(offset_dict_lemmatized[token.head.idx])
else:
new_triple.append(offset_dict[token.head.idx])
if token.lemma_ == '-PRON-':
new_triple.append(offset_dict[token.idx])
else:
if LEMMATIZED:
new_triple.append(offset_dict_lemmatized[token.idx])
else:
new_triple.append(offset_dict[token.idx])
deps.append(new_triple)
# query accomodation
if LEMMATIZED:
for d in deps:
if d[2][0:5].lower() == "dummy":
d[2] = "Dummy:DM"
for i in range(len(deps)):
governor = self.get_lemma(deps[i][1]).capitalize() + ":" + self.get_pos(deps[i][1])
dependent = self.get_lemma(deps[i][2]).capitalize() + ":" + self.get_pos(deps[i][2])
deps[i] = [deps[i][0], governor, dependent]
return deps
def morph(self, sent):
sent_changed = ""
for c in sent:
if c in [':', '$', '.']:
sent_changed = sent_changed + "_"
else:
sent_changed = sent_changed + c
final_sent_changed = ""
sent_changed_splitted = sent_changed.split(" ")
for i in range(len(sent_changed_splitted)):
if sent_changed_splitted[i][0] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
if i == 0:
final_sent_changed = "N" + sent_changed_splitted[i]
else:
final_sent_changed = final_sent_changed + " N" + sent_changed_splitted[i]
else:
if i == 0:
final_sent_changed = sent_changed_splitted[i]
else:
final_sent_changed = final_sent_changed + " " + sent_changed_splitted[i]
return final_sent_changed
def shrink(self, word):
chunk_list = word.split("_")
sw = ""
for chunk in chunk_list:
sw = sw + chunk
return sw
def main():
# pos=VERB, NOUN, ADJ, ADV
syns = wordnet.synsets("wise", pos=wordnet.ADJ, lang="eng")
for synset in syns:
print("\nsynset: ", synset.name())
print("gloss: ", synset.definition())
print("lemmas: ", synset.lemmas())
for s in synset.examples():
print("#synset example: ", s)
if __name__ == "__main__":
main()