-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_antonyms.py
297 lines (252 loc) · 9.38 KB
/
get_antonyms.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
#!/usr/bin/env python3
import inspect
import json
import pickle
import re
import time
from collections import defaultdict
from operator import itemgetter
import nltk
import requests
from bs4 import BeautifulSoup
from nltk.corpus import wordnet
'''
Antonym Dict = {
WORD_1: {
ORIGIN_1: [ANTONYM_1, ..., ANTONYM_n],
...,
ORIGIN_j: [ANTONYM_1, ..., ANTONYM_n]
},
...,
WORD_i: {
ORIGIN_1: [ANTONYM_1, ..., ANTONYM_n],
...,
ORIGIN_j: [ANTONYM_1, ..., ANTONYM_n]
},
}
ORIGINs:
- WN (Antonyms found from WordNet)
- TS (Antonyms found from Thesaurus)
- PTS (Antonyms found from PowerThesaurus)
- SYN (Synonyms found from WordNet, Thesaurus and PowerThesaurus)
'''
def word_net(word):
'''
Treat synsets of words as separate
'''
for syn in wordnet.synsets(word):
synonyms, antonyms = [], []
for l in syn.lemmas():
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
# word_dict[syn]["synonyms"] = synonyms
# word_dict[syn]["antonyms"] = antonyms
# Choose which synsets pass through
def word_net_collapsed(word):
'''
Collapse synsets of the same word
'''
synonyms, antonyms = [], []
for idx, syn in enumerate(wordnet.synsets(word)):
if idx > 3:
break
for l in syn.lemmas():
if l.name() not in synonyms:
synonyms.append(l.name())
if l.antonyms():
if len(l.antonyms()) > 1:
for ant in l.antonyms():
antonyms.append(ant.name())
elif l.antonyms()[0].name() not in antonyms:
antonyms.append(l.antonyms()[0].name())
return antonyms
def power_thesaurus(word, relation="antonyms", inspect=False):
'''
Scrape https://www.powerthesaurus.org for antonyms
Relations:
synonyms :: Not-implemented
antonyms :: Works
definitions :: Not-implemented
examples :: Not-implemented
'''
url = "https://www.powerthesaurus.org/%s/%s" % (word, relation)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
try:
soup = BeautifulSoup(requests.get(url, headers=headers).text, 'html.parser')
except Exception as e:
print(f"The following error ({e}) occured for {url}, sleeping for 10 minutes ...")
time.sleep(600)
power_thesaurus(word)
antonyms = []
# class of antonym seems to change for PowerTheSaurus occasionally, therefore us regex to get the correct antonyms
regex = r"^/\w*/antonyms$"
for link in soup.find_all('a', href=True):
if len(antonyms) > 9:
break
match = re.match(regex, link.attrs['href'])
if match:
ant = link.text if (link.text != word and link.text != "antonyms") else False
if ant:
antonyms.append(ant)
if inspect:
print(antonyms)
return antonyms
def thesaurus(word, inspect=False):
'''
Scrape https://www.thesaurus.com/ for antonyms
'''
url = "https://www.thesaurus.com/browse/%s?s=t" % (word)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
try:
soup = BeautifulSoup(requests.get(url, headers=headers).text, 'html.parser')
except Exception as e:
print(f"The following error ({e}) occured for {url}, sleeping for 10 minutes ...")
time.sleep(600)
thesaurus(word)
antonyms = []
for idx, link in enumerate(soup.find_all('a', attrs={"css-lqr09m-ItemAnchor etbu2a31"}, href=True)):
if len(antonyms) > 9:
break
antonyms.append(link.text)
if inspect:
print(antonyms)
return antonyms
def inspect_antonym_dict(all_words=False):
'''
Inspect the antonym dict
See if it is created
See how many words it contains
See the individual words and the amount of antonyms from each source
See if any words lack antonyms from either thesauri
'''
try:
with open("antonym_dict.pickle", "rb") as handle:
antonym_dict = pickle.load(handle)
print(f"Succesfully loaded antonym dict (contains {len(antonym_dict)} words!)")
except FileNotFoundError as e:
print("Could not find antonym dict, created empty dict!")
antonym_dict = defaultdict(dd_module)
if all_words:
print("Word\tWordNet\t\tTheSaurus\tPowerTheSaurus")
for word, antonym_sources in antonym_dict.items():
l1 = []
for source, antonyms in antonym_dict[word].items():
l1.append((source, len(antonyms)))
l1 = "\t".join(str(i) for i in l1)
print(f"{word}\t{l1}")
else:
# Check if there are any words with 0 antonyms from TS or PTS
ts_list, pts_list = [], [] # Save list of these words to retry antonym extraction process
for word, antonym_sources in antonym_dict.items():
for source, antonyms in antonym_dict[word].items():
if (source == "TS" or source == "PTS") and len(antonyms) == 0:
print(f"{word}\t{source}\t{len(antonyms)}")
if source == "TS":
ts_list.append(word)
elif source == "PTS":
pts_list.append(word)
return ts_list, pts_list
def dd_module():
'''
Module-level function (substitute to lambda function) for pickling
'''
return defaultdict(list)
def save_dict(antonym_dict):
'''
Save the antonym dict
'''
with open("antonym_dict.pickle", "wb") as handle:
pickle.dump(antonym_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
def calculate_antonym_score(antonyms, source, calculations, modifier=1):
'''
Calculate which antonym is the final best fitting antonym for a word
'''
if antonyms[source] != []:
for idx, ant in enumerate(antonyms[source]):
# WordNet antonyms are sparse, ensure they weigh similiar to the thesauri
if source == "WN":
modifier = (len(antonyms["TS"])+len(antonyms["PTS"]))//2
score = (len(antonyms[source]) - idx)*modifier
calculations[ant] += score
return calculations
def get_antonym(word, inspect=False):
'''
Substitute a word with its antonym
'''
# Open or create antonym dict
try:
with open("antonym_dict.pickle", "rb") as handle:
antonym_dict = pickle.load(handle)
except FileNotFoundError as e:
print("Could not find antonym dict, created empty dict!")
antonym_dict = defaultdict(dd_module)
# Check if antonyms already found for this word
if antonym_dict[word] and antonym_dict[word]!={}:
antonyms = antonym_dict[word]
if antonym_dict[word]["WN"] == []:
pass
if antonym_dict[word]["TS"] == []:
ts = thesaurus(word)
antonym_dict[word]["TS"] += ts
antonyms = antonym_dict[word]
save_dict(antonym_dict)
if antonym_dict[word]["PTS"] == []:
pts = thesaurus(word)
antonym_dict[word]["PTS"] += pts
antonyms = antonym_dict[word]
save_dict(antonym_dict)
else:
wn = word_net_collapsed(word)
ts = thesaurus(word)
pts = power_thesaurus(word)
antonym_dict[word]["WN"] += wn
antonym_dict[word]["TS"] += ts
antonym_dict[word]["PTS"] += pts
# Save the new antonym dict
save_dict(antonym_dict)
# Retrieve the antonyms
antonyms = antonym_dict[word]
# Logic to define the final antonym
antonym = ""
calculations = defaultdict(lambda: 0) # set all possible antonyms to have a sore of zero
sources = [("WN", 1), ("TS", 1), ("PTS", 1)] # list of sources and how much they weigh
for src in sources:
calculations = calculate_antonym_score(antonyms, src[0], calculations, modifier=src[1])
top_antonyms = sorted(calculations.items(), key=itemgetter(1), reverse=True) # [(ant_1, freq_1), ..., (ant_i, freq_i)]
if top_antonyms == []:
antonym = f"not {word}"
if inspect:
print(f"Couldn't find any antonyms for '{word}', defaulted to '{antonym}'")
else:
antonym = top_antonyms[0][0]
# print(f"Word = {word}\tAntonym = {antonym}")
return antonym
def words_to_add():
'''
Pre-add some words to the antonym dict
'''
with open("progress_dict.pickle", "rb") as handle:
d = pickle.load(handle)
# Tags: {'NUM', 'PART', 'PRON', 'AUX', 'NOUN', 'CCONJ',
# 'ADJ', 'ADV', 'DET', 'INTJ', 'PROPN', 'ADP',
# 'VERB', 'X', 'SYM', 'PUNCT'}
to_add = set()
for k, v in d.items():
words, pos_tags = k.split(), v.split()
for word, tag in zip(words, pos_tags):
if tag == "ADV":
to_add.add(word)
return to_add
def main():
# word_net("warm")
# word_net_collapsed("warm")
# power_thesaurus("warm", inspect=True)
# thesaurus("warm", inspect=True)
# inspect_antonym_dict(all_words=False)
# words = words_to_add()
# for w in words::
# get_antonym(w.lower())
pass
if __name__ == '__main__':
main()