-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththesaurus.py
executable file
·54 lines (45 loc) · 1.69 KB
/
thesaurus.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
#!/usr/bin/env python3
import sys
import urllib.request
from bs4 import BeautifulSoup
def main():
# Check if arguments were supplied
if len(sys.argv) < 2:
print('Please include a search term.')
exit()
search_term=sys.argv[1]
search_term = search_term.replace(' ', '%20')
# Check for invalid word resulting in HTTPError
try:
fp = urllib.request.urlopen('http://www.thesaurus.com/browse/'+search_term)
except urllib.error.HTTPError:
print('The word '+search_term+' could not be found')
exit()
html_bytes = fp.read()
html_doc = html_bytes.decode('utf-8')
fp.close()
soup = BeautifulSoup(html_doc, 'html.parser')
synonyms = soup.find_all("div",{"class":"synonyms"})
# Check again for an invalid word which may have returned a valid HTTP response
if(len(synonyms)==0):
print('The word '+search_term+' could not be found')
exit()
thesarus={}
for synonym in synonyms:
description = synonym.find("div",{"class":"synonym-description"}).find(
"strong",{"class":"ttl"}).text
thesarus[description]=[]
for word in synonym.find("div",{"class":"relevancy-list"}).find_all(
"span",{"class":"text"}):
thesarus[description].append(word.text)
print('Definitions for '+search_term+' are the following:')
choices={}
for idx,word in enumerate(thesarus.keys()):
print(' '+str(idx)+') '+word)
choices[idx]=word
selection = input('Select a definition: ')
print('Synonyms for '+choices[int(selection)]+':')
for word in thesarus[choices[int(selection)]]:
print(word)
if __name__ == '__main__':
main()