-
Notifications
You must be signed in to change notification settings - Fork 4
/
para_wn.py
510 lines (434 loc) · 17.9 KB
/
para_wn.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# Useful WordNet-related functions for paraquery
# Author: Lili Kotlerman, [email protected], June 2012
from nltk.corpus import wordnet as wn
# define a hash that maps relation names to IDs and another one that maps IDs to names
_relation_names = ['not in WN', 'derivation', 'synonym', 'antonym', 'hypernym', 'hyponym', 'co-hyponym', 'undefined relation', 'pertainym', 'holonym', 'meronym']
_relation_ids_to_names = dict(enumerate(_relation_names))
_relation_names_to_ids = dict([(name, idx) for (idx, name) in enumerate(_relation_names)])
def internal_form(word):
return word.replace(' ', '_').lower()
def external_form(word):
return word.replace('_', ' ').lower()
def get_path_length(synsetA, synsetB):
## taken from http://blog.typeslashcode.com/voxpop/2009/10/returning-wordnet-shortest-path-distance-with-nltk/
if synsetA == synsetB:
return 0
path_distance = -1
dist_list1 = synsetA.hypernym_distances(0)
dist_dict1 = {}
dist_list2 = synsetB.hypernym_distances(0)
dist_dict2 = {}
# Transform each distance list into a dictionary. In cases where
# there are duplicate nodes in the list (due to there being multiple
# paths to the root) the duplicate with the shortest distance from
# the original node is entered.
for (l, d) in [(dist_list1, dist_dict1), (dist_list2, dist_dict2)]:
for (key, value) in l:
if key in d:
if value < d[key]:
d[key] = value
else:
d[key] = value
# For each ancestor synset common to both subject synsets, find the
# connecting path length. Return the shortest of these.
for synset1 in dist_dict1.keys():
for synset2 in dist_dict2.keys():
if synset1 == synset2:
new_distance = dist_dict1[synset1] + dist_dict2[synset2]
if path_distance < 0 or new_distance < path_distance:
path_distance = new_distance
return path_distance
def get_shortest_path(word_a, word_b):
path_distance = -1
word_a = internal_form(word_a)
word_b = internal_form(word_b)
if (is_pair_included(word_a, word_b)):
for x in wn.synsets(word_a):
for y in wn.synsets(word_b):
dist = get_path_length(x, y)
if (dist > 0):
if (path_distance < 0 or dist < path_distance):
path_distance = dist
return path_distance
def get_phrase_lemma(words):
a_lemma = ""
for word in words:
lemmas = get_lemmas(word)
if (len(lemmas) > 0):
#take the 1st lemma from the list
a_lemma += lemmas[0] + ' '
else:
#use the word as is, just convert to the external_form
a_lemma += external_form(word) + ' '
return a_lemma.strip()
def get_lemmas(word):
word = internal_form(word)
lemmas = []
if (is_word_included(word)):
for x in wn.synsets(word):
lemmatized_word = lemmatize(word, x.pos)
if (lemmatized_word not in lemmas):
lemmas.append(lemmatized_word)
else:
# if word is not in WN
words = word.split('_')
if (len(words) > 1):
lemmas.append(get_phrase_lemma(words))
else:
lemmas.append(word)
return lemmas
def lemmatize(word, pos):
word = internal_form(word)
if (pos == 's'):
#change ADJ_SAT to ADJ for wn.morphy()
pos = 'a'
lemma = wn.morphy(word, pos)
if not lemma:
lemma = word
return external_form(lemma)
def is_word_included(word):
word = internal_form(word)
return len(wn.synsets(word)) > 0
def is_pair_included(word_a, word_b):
return (is_word_included(word_a) and is_word_included(word_b))
def is_same_pos(word_a, word_b):
# if the 2 words have a common possible pos - then true
if not is_pair_included(word_a, word_b):
return -1
return int(len(get_common_pos(word_a, word_b)) > 0)
def get_common_pos(word_a, word_b):
a_pos = set()
b_pos = set()
common_pos = set()
for x in wn.synsets(word_a):
a_pos.add(x.pos)
for x in wn.synsets(word_b):
b_pos.add(x.pos)
common_pos = a_pos.intersection(b_pos)
return common_pos
def get_derivations(word):
# get all the derivations of the word
ders = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for lemma in x.lemmas:
# check whether the current lemma is the word's lemma (discard lemmas of other words in the synset)
if external_form(lemma.name) != lemmatize(word, x.pos):
continue
for der in lemma.derivationally_related_forms():
ders.add(external_form(der.name))
# # add pertainyms of the word's lemma as it's derivations
# for per in lemma.pertainyms():
# ders.add(external_form(per.name))
return ders
def get_pertainyms(word):
# get all the derivations of the word
pers = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for lemma in x.lemmas:
# add pertainyms of the word and its synonyms
for per in lemma.pertainyms():
pers.add(external_form(per.name))
return pers
def get_synonyms(word):
# get all the synonyms + synonyms' derivations
syns = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for lemma in x.lemmas:
# exclude self lemma from the list of synonyms
if external_form(lemma.name) == lemmatize(word, x.pos):
continue
syns.add(external_form(lemma.name))
#add derivations of a synonym as synonyms
for der in lemma.derivationally_related_forms():
syns.add(external_form(der.name))
# add pertainyms of a synonym as synonyms
# for per in lemma.pertainyms():
# syns.add(external_form(per.name))
return syns
def get_hypernyms(word):
# get all the hypernyms + their derivations
hyps = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for hyp in x.hypernyms():
for hlemma in hyp.lemmas:
hyps.add(external_form(hlemma.name))
#add derivations
for der in hlemma.derivationally_related_forms():
hyps.add(external_form(der.name))
# for per in hlemma.pertainyms():
# hyps.add(per.name.replace('_',' '))
return hyps
def get_hyponyms(word):
# get all the hyponyms + their derivations
hyps = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for hyp in x.hyponyms():
for hlemma in hyp.lemmas:
hyps.add(external_form(hlemma.name))
#add derivations
for der in hlemma.derivationally_related_forms():
hyps.add(external_form(der.name))
# for per in hlemma.pertainyms():
# hyps.add(per.name.replace('_',' '))
return hyps
def get_holonyms(word):
# get all the hypernyms + their derivations
holo = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for hol in x.member_holonyms():
for hlemma in hol.lemmas:
holo.add(external_form(hlemma.name))
# #add derivations
# for der in hlemma.derivationally_related_forms():
# holo.add(external_form(der.name))
for hol in x.substance_holonyms():
for hlemma in hol.lemmas:
holo.add(external_form(hlemma.name))
# #add derivations
# for der in hlemma.derivationally_related_forms():
# holo.add(external_form(der.name))
for hol in x.part_holonyms():
for hlemma in hol.lemmas:
holo.add(external_form(hlemma.name))
# #add derivations
# for der in hlemma.derivationally_related_forms():
# holo.add(external_form(der.name))
return holo
def get_meronyms(word):
# get all the hypernyms + their derivations
mers = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for mer in x.member_meronyms():
for mlemma in mer.lemmas:
mers.add(external_form(mlemma.name))
# #add derivations
# for der in mlemma.derivationally_related_forms():
# mers.add(external_form(der.name))
for mer in x.substance_meronyms():
for mlemma in mer.lemmas:
mers.add(external_form(mlemma.name))
# #add derivations
# for der in mlemma.derivationally_related_forms():
# mers.add(external_form(der.name))
for mer in x.part_meronyms():
for mlemma in mer.lemmas:
mers.add(external_form(mlemma.name))
# #add derivations
# for der in mlemma.derivationally_related_forms():
# mers.add(external_form(der.name))
return mers
def get_hyp(word):
# get all the hypo/hypernyms + their derivations
return get_hypernyms(word).union(get_hyponyms(word))
def get_antonyms(word):
# get antonyms of (the word + all its synonyms) + derivations of each antonym
self_derivations = get_derivations(word)
ants = set()
word = internal_form(word)
if (is_word_included(word)):
for x in wn.synsets(word):
for l in x.lemmas:
for ant in l.antonyms():
ants.add(external_form(ant.name))
#add derivations
for der in ant.derivationally_related_forms():
derivation = external_form(der.name)
# don't include derivations of the word to the list of antonyms. E.g. 'employer' and 'employee' are antonyms,
# but have the same derivationally related form 'employ'
if (derivation not in self_derivations):
ants.add(derivation)
# for per in ant.pertainyms():
# ants.add(per.name.replace('_',' '))
return ants
def is_derivation(word_a, word_b):
word_a = internal_form(word_a)
word_b = internal_form(word_b)
# if the 2 words have the same lemmas - return true
for a_syn in wn.synsets(word_a):
for bSyn in wn.synsets(word_b):
if (lemmatize(word_a, a_syn.pos) == lemmatize(word_b, bSyn.pos)):
return True
if (is_pair_included(word_a, word_b)):
# if word_b's lemma is among word_a's derivations - return True
aDers = get_derivations(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in aDers):
return True
# if word_a's lemma is among word_b's synonyms - return True
# e.g. get_derivations('amendment') returns set(['amend']), while get_derivations('amending') returns set(['amendment', 'amendable', 'amendatory'])
bDers = get_derivations(word_b)
for syn in wn.synsets(word_a):
aLemma = lemmatize(word_a, syn.pos)
if (aLemma in bDers):
return True
return False
def is_synonym(word_a, word_b):
word_a = internal_form(word_a)
word_b = internal_form(word_b)
if (is_pair_included(word_a, word_b)):
# if word_b's lemma is among word_a's synonyms - return True
a_syns = get_synonyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_syns):
return True
# if word_a's lemma is among word_b's synonyms - return True (e.g. 'good' is among the synonyms of 'better', but not vice versa)
b_syns = get_synonyms(word_b)
for syn in wn.synsets(word_a):
aLemma = lemmatize(word_a, syn.pos)
if (aLemma in b_syns):
return True
return False
def is_antonym(word_a, word_b):
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's antonyms - return True
aAnts = get_antonyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in aAnts):
return True
# if word_a's lemma is among word_b's antonyms - return True (doing the same as for is_synonym() and is_derivation(), although encountered no examples)
bAnts = get_antonyms(word_b)
for syn in wn.synsets(word_a):
aLemma = lemmatize(word_a, syn.pos)
if (aLemma in bAnts):
return True
return False
def is_pertainym(word_a, word_b):
word_a = internal_form(word_a)
word_b = internal_form(word_b)
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's pertainyms - return True
a_pers = get_pertainyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_pers):
return True
# if word_a's lemma is among word_b's pertainyms - return True (doing the same as for is_synonym() and is_derivation(), although encountered no examples)
bPers = get_pertainyms(word_b)
for syn in wn.synsets(word_a):
aLemma = lemmatize(word_a, syn.pos)
if (aLemma in bPers):
return True
return False
def is_hyponym(word_a, word_b):
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's hyponyms - return True
a_hypo = get_hyponyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_hypo):
return True
return False
def is_hypernym(word_a, word_b):
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's hypernyms - return True
a_hyper = get_hypernyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_hyper):
return True
return False
def is_hyp(word_a, word_b):
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's hypernyms or among word_a's hyponyms - return True
a_hyp = get_hypernyms(word_a).union(get_hyponyms(word_a))
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_hyp):
return True
return False
def is_cohyponym(word_a, word_b):
if is_derivation(word_a, word_b):
return False
if is_synonym(word_a, word_b):
return False
common_hypernyms = set(get_hypernyms(word_a)).intersection(get_hypernyms(word_b))
return len(common_hypernyms) > 0
def is_holonym(word_a, word_b):
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's hyponyms - return True
a_holo = get_holonyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_holo):
return True
return False
def is_meronym(word_a, word_b):
if is_pair_included(word_a, word_b):
# if word_b's lemma is among word_a's hyponyms - return True
a_mero = get_meronyms(word_a)
for syn in wn.synsets(word_b):
b_lemma = lemmatize(word_b, syn.pos)
if (b_lemma in a_mero):
return True
return False
def get_relation_id(rel):
"""
0 - Pair not found in WN. Can add "relation > 0" condition to queries in order to retrieve only pairs, found in WN (i.e. both src and tgt are in WN)
1 - Derivation
2 - Synonym
3 - Antonym
4 - Hypernym (tgt is a hypernym of src)
5 - Hyponym (tgt is a hyponym of src)
6 - Co-hyponym (tgt and src share a common hypernym)
8 - Pertainym
9 - Holonym
10 - Meronym
7 - Undefined relation, i.e. both words are found in WN, but their relation is not among the above
"""
return _relation_names_to_ids[rel]
def get_relation_name(rel):
"""
0 - Pair not found in WN. Can add "relation > 0" condition to queries in order to retrieve only pairs, found in WN (i.e. both src and tgt are in WN)
1 - Derivation
2 - Synonym
3 - Antonym
4 - Hypernym (tgt is a hypernym of src)
5 - Hyponym (tgt is a hyponym of src)
6 - Co-hyponym (tgt and src share a common hypernym)
8 - Pertainym
9 - Holonym
10 - Meronym
7 - Undefined relation, i.e. both words are found in WN, but their relation is not among the above
"""
return _relation_ids_to_names[rel] if rel in _relation_ids_to_names else ''
def get_wordnet_relation(word_a, word_b):
rel = 'not in WN'
if is_pair_included(word_a, word_b):
if is_derivation(word_a, word_b):
rel = 'derivation'
elif is_synonym(word_a, word_b):
rel = 'synonym'
elif is_antonym(word_a, word_b):
rel = 'antonym'
elif is_hypernym(word_a, word_b):
rel = 'hypernym'
elif is_hyponym(word_a, word_b):
rel = 'hyponym'
elif is_pertainym(word_a, word_b):
rel = 'pertainym'
elif is_holonym(word_a, word_b):
rel = 'holonym'
elif is_meronym(word_a, word_b):
rel = 'meronym'
elif is_cohyponym(word_a, word_b):
rel = 'co-hyponym'
else:
rel = 'undefined relation'
return (rel, get_relation_id(rel))