/
wnQuery_dev.py
148 lines (118 loc) · 3.94 KB
/
wnQuery_dev.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
#wordnet functions - what data do I want to extract?
#lemmas - names, antonyms, derivationally_related_forms, pertainyms (adj)
import random, nltk
nltk.data.path.append('./nltk_data/')
from nltk.corpus import wordnet as wn
class Syns(object):
def __init__(self, word, pos):
self.word = word
self.pos = pos
self.number = 0
self.structure = self.generateStructure(word, pos)
def generateStructure(self, word, pos):
structure = dict()
synsets = [synset for synset in wn.synsets(word) if synset.pos == pos]
for synset in synsets:
if synset.pos == pos:
if synset.name not in structure:
self.number += 1
structure[synset.name] = synsets[0].path_similarity(synset)
return structure
def ontoList(self, synset):
# things to pick from
if self.pos == 'v':
ln = wn.synset(synset).lexname.split('.')[1]
hyper = self.lemmatize(self.getHypernyms(synset))
definition = self.getDefinition(synset)
lemmas = self.lemmatize(self.getLemmas(synset))
examples = self.getExamples(synset)
strings = [string.replace("_", " ") for string in self.getFrameStrings(synset)]
hypo = self.lemmatize(self.getHyponyms(synset))
ontologyList = [strings, ln, lemmas, examples, hypo, definition, hyper]
else:
ln = wn.synset(synset).lexname.split('.')[1]
hyper = self.lemmatize(self.getHypernyms(synset))
definition = self.getDefinition(synset)
lemmas = self.lemmatize(self.getLemmas(synset))
examples = self.getExamples(synset)
hypo = self.lemmatize(self.getHyponyms(synset))
ontologyList = [ln, lemmas, examples, hypo, definition, hyper]
returnList = list()
for o in ontologyList:
if o:
returnList.append(o)
return returnList
def chooser(self, l, i):
if type(l[i]) == list:
# print "this is o[i]: " + str(o[i])
j = random.choice(l[i])
return j
else:
return l[i]
def findCaps(self, l):
templist = list()
for i in l:
templist.append(" ".join([word for word in i.split(" ") if not word.isupper()]))
return templist
def ontologies(self, synset, sd):
o = self.ontoList(synset)
inBounds = ""
if o:
inBounds = self.chooser(o, random.randint(0,len(o)-1))
# for i in range(-2, 2):
# if sd<=i and sd>=i-1:
# inBounds.append(self.chooser(o, i+2))
return inBounds
def lexStringFilter(self, synset):
compareList = list()
compareList.extend(self.lemmatize(self.getLemmas(synset)))
compareList.extend(self.lemmatize(self.getHypernyms(synset)))
compareList.extend(self.lemmatize(self.getHyponyms(synset)))
return compareList
def getSynset(self, word):
return [synset.name for synset in wn.synsets(word) if synset.pos == self.pos]
def getDefinition(self, synset):
return wn.synset(synset).definition
def getExamples(self, synset):
return wn.synset(synset).examples
def getLemmas(self, synset):
return wn.synset(synset).lemmas
def getFrameStrings(self, synset):
frameStrings = list()
synset = wn.synset(synset)
for lemma in synset.lemmas:
if(lemma != synset.lemmas[0]):
lfs_less = self.findCaps(lemma.frame_strings)
frameStrings.extend(lfs_less)
return frameStrings
def getLexname(self, synset):
return wn.synset(synset).lexname
#less specific
def getHypernyms(self, synset):
root = wn.synset(synset).root_hypernyms()
hyper = wn.synset(synset).hypernyms()
root.extend(hyper)
return root
#more specific
def getHyponyms(self, synset):
hyponyms = list()
for h in self.getHypernyms(synset):
hyponyms.extend(h.hyponyms())
return hyponyms
def lemmatize(self, thingList):
try:
return [t.name.replace("_", " ") for tl in thingList for t in tl.lemmas]
except:
return [t.name.replace("_", " ") for t in thingList]
class Pos(object):
def __init__(self, word):
self.pos = self.getPos(word)
def getPos(self, word):
return random.choice(wn.synsets(word)).pos
#decode('ascii', errors='replace') this might be necessary
if __name__ == "__main__":
import sys
word = sys.argv[1]
p = Pos(word)
syn = Syns(word, p.pos)
print syn.structure