-
Notifications
You must be signed in to change notification settings - Fork 8
/
expanderwordnet.py
119 lines (110 loc) · 2.34 KB
/
expanderwordnet.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
#WORD OBJECT
from nltk.corpus import wordnet as wn
class word:
def __init__(self, text, adj=False, adv=False, noun=False, verb=False, verbose=False):
#First we lookup the word
self.text = text
self.adj = adj
self.adv = adv
self.noun = noun
self.verbose = verbose
if adj:
self.synsets = wn.synsets(text, pos=wn.ADJ)
elif adv:
self.synsets = wn.synsets(text, pos=wn.ADV)
elif noun:
self.synsets = wn.synsets(text, pos=wn.NOUN)
elif verb:
self.synsets = wn.synsets(text, pos = wn.VERB)
elif not adj and not adv and not verb and not noun:
self.synsets = wn.synsets(text)
#######
# PUBLIC FUNCTIONS
#######
#Return the textual definition of the word in question
def definition(self):
if self.verbose == True:
pass
if self.isKnown():
#print self.synsets[0]
res = ""
for i in self.synsets:
res += i.definition()
return res
else:
return None
#This will return an antonym
def antonmys(self):
#Can only have for adjectives
ants = []
if self.isKnown():
for i in self.synsets:
try:
#print i.lemmas()[0].antonyms()
ant = i.lemmas()[0].antonyms()
#print "ant " + ant
if len(ant) > 0:
ants.append(ant)
except:
continue
for i in self.synsets:
try:
ant = i.antonyms()
#print "ant " + ant
if len(ant) > 0:
ants.append(ant)
except:
continue
return ants
else:
return None
def hypernyms(self):
#Can only have for adjectives
hyp = []
if self.isKnown():
for i in self.synsets:
try:
hyper = i.lemmas()[0].hypernyms()
if len(hyper) > 0:
hyp.append(hyper)
except:
continue
for i in self.synsets:
try:
hyper = i.lemmas()[0].hypernyms()
if len(hyper) > 0:
hyp.append(hyper)
except:
continue
return hyp
else:
return None
def hyponyms(self):
#Can only have for adjectives
hyp = []
if self.isKnown():
for i in self.synsets:
#try:
hypo = i.hyponyms()
#print hypo
if len(hypo) > 0:
hyp.append(hypo)
#except:
continue
for i in self.synsets:
#try:
hypo = i.lemmas()[0].hyponyms()
#print hypo
if len(hypo) > 0:
hyp.append(hypo)
#except:
continue
return hyp
else:
return None
#Private internal functions
def isKnown(self):
if len(self.synsets) == 0:
return False
else:
return True