-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordData.py
244 lines (205 loc) · 6.75 KB
/
wordData.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
from PyDictionary import PyDictionary
import nltk
from nltk import text
from nltk.corpus.reader.tagged import MacMorphoCorpusReader
import operator
try:
from nltk.corpus import wordnet
except:
nltk.download('wordnet')
from nltk.corpus import wordnet
import pickle
import threading
from tkExtensions import*
dictionary=PyDictionary()
#Get the definition of a word
def getDefinition(word):
definition = dictionary.meaning(word)
if definition is None:
return {}
return definition
#Correct the brackets in a string to make sure they are always closed
def correctBrackets(string):
bracketFlag = False
for char in string:
if char == "(":
bracketFlag = True
if bracketFlag:
string += ")"
return string
#Format the definition to be read
def formatDefinition(definition):
format = ""
if definition is None or definition == {}:
return "No Definition Available"
for key in definition.keys():
format += (key + ":\n")
for i in range(len(definition[key])):
definitionSpecific = definition[key][i]
definitionSpecific = (definitionSpecific[0].upper()) + definitionSpecific[1:]
definitionSpecific = correctBrackets(definitionSpecific)
format += " " + str(i + 1) + "." + " " + definitionSpecific + "\n"
format += "\n"
return format
#Get the synomyms of a word
def getSynonyms(word):
try:
return dictionary.synonym(word)
except:
return []
#Get the antonyms of a word
def getAntonyms(word):
try:
return dictionary.antonym(word)
except:
return []
#Get the min, max and average similarity in meaning of 2 words
def getRelation(word1, word2):
try:
syns1 = wordnet.synsets(word1)
syns2 = wordnet.synsets(word2)
maxRelation = 0.0
minRelation = 1.0
averageRelationSum = 0.0
relationCount = 0.0
for syn1 in syns1:
for syn2 in syns2:
relation = syn1.wup_similarity(syn2)
if relation > maxRelation:
maxRelation = relation
if relation < minRelation:
minRelation = relation
averageRelationSum += relation
relationCount += 1
if relationCount != 0:
averageRelation = averageRelationSum / relationCount
return maxRelation, minRelation, averageRelation
else:
return -1, -1, -1
except:
return -1, -1, -1
THREADS = 100 #Number of threads used to generate the word data
#The data about a set of words
class WordData:
def __init__(self, words):
self.words = words
self.wordRelations = {}
self.definitions = {}
self.synonyms = {}
self.antonyms = {}
self.nextWord = 0
#Save the data
def save(self):
with open("Data\\wordData.bin", 'wb') as file:
pickle.dump(self, file)
#Get a definition of a word in the set
def definition(self, word):
try:
return self.definitions[word]
except:
return {}
#Get the synoyms of a word in the set
def synonym(self, word):
try:
return self.synonyms[word]
except:
return None
#Get the antonyms of a word in the set
def antonym(self, word):
try:
return self.antonyms[word]
except:
return None
#Check if a word in the set is an adjective
def isAdjective(self, word):
try:
return "Adjective" in self.definition(word).keys()
except:
return False
##############FUNCTIONS BELOW ARE FOR UPLOADING NEW WORD SETS ONLY AND ARENT CALLED IN DAY TO DAY USE. DON'T CALL THEM###############
def populate(self):
try:
self.getDefs()
except KeyboardInterrupt:
print("\nExiting...")
self.save()
def getAnts(self):
count = 0
threads = []
def get(start, end):
for i in range(start, end):
self.antonyms[self.words[i]] = getAntonyms(self.words[i])
print("Words " + str(start) + " to " + str(end) + " done")
return 0
end = 0
sizeOfCat = len(self.words)//THREADS
for i in range(THREADS):
start = end
end = start + sizeOfCat
t = threading.Thread(target=get, args=[start, end])
t.start()
threads.append(t)
t = threading.Thread(target=get, args=[end, len(self.words)])
t.start()
threads.append(t)
for t in threads:
t.join()
print("Saving...")
self.save()
print("Finished Antonyms")
def getDefs(self):
count = 0
threads = []
def get(start, end):
for i in range(start, end):
self.definitions[self.words[i]] = getDefinition(self.words[i])
print("Words " + str(start) + " to " + str(end) + " done")
end = 0
sizeOfCat = len(self.words)//THREADS
for i in range(THREADS):
start = end
end = start + sizeOfCat
t = threading.Thread(target=get, args=[start, end])
t.start()
threads.append(t)
t = threading.Thread(target=get, args=[end, len(self.words)])
t.start()
threads.append(t)
threadsDone = True
for t in threads:
t.join()
print("Saving...")
self.save()
print("Finished Defs")
def getSyns(self):
count = 0
threads = []
def get(start, end):
for i in range(start, end):
self.synonyms[self.words[i]] = getSynonyms(self.words[i])
print("Words " + str(start) + " to " + str(end) + " done")
end = 0
sizeOfCat = len(self.words)//THREADS
for i in range(THREADS):
start = end
end = start + sizeOfCat
t = threading.Thread(target=get, args=[start, end])
t.start()
threads.append(t)
t = threading.Thread(target=get, args=[end, len(self.words)])
t.start()
threads.append(t)
threadsDone = True
for t in threads:
t.join()
print("Saving...")
self.save()
print("Finished Synonyms")
#Get the word data in a file
def getWords(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
#Get the word data from the default file
def loadWordData():
with open(resourcePath("Data\\wordData.bin"), 'rb') as file:
return pickle.load(file)