-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.py
More file actions
63 lines (48 loc) · 1.69 KB
/
dict.py
File metadata and controls
63 lines (48 loc) · 1.69 KB
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
import sys
from bs4 import BeautifulSoup as bs
import requests as r
def main():
"""
Stage 1
----------------------------------------------------------------------------
Check if The User Has Input The Word Or Not
"""
if len(sys.argv)<2:
print "Usage : python dict.py word_to_search"
sys.exit()
"""
Stage 2
----------------------------------------------------------------------------
Open Dictionary.com And Parse it Using BeautifulSoup
"""
content = r.get('http://dictionary.reference.com/browse/'+str(sys.argv[1]))
soup = bs(content.text)
"""
Stage 3
----------------------------------------------------------------------------
Check if The Word Is Misspelled or Spelled Correctly
"""
if soup.find(class_="closest-result"):
print "\n\t\t\tMisspelled."
print soup.find(class_="closest-result").get_text().replace("\n\n\n","")
print soup.find(class_="more-suggestions").get_text().replace("\n\n\n","\t")
"""
Stage 4
------------------------------------------------------------------------------
If The Word is Correctly Spelled Then Search It On Dictionary.com And Print The Results
"""
else:
i = 0
for dictionary in soup.findAll("div",{'class':'def-list'}):
if not i:
print "\n\t\tWelcome To Dictionary.com\n"
elif i == 1 : #For British Dictionary
print "\t\tBritish Dictionary definitions for " + str(sys.argv[1])
elif i == 2 : #For History And Origin of Word
print "\t\tWord Origin and History for " + str(sys.argv[1])
if dictionary.find({'class':'luna-data-header'}):# if Dictionary Has A Title
print dictionary.find({'class':'luna-data-header'})
print dictionary.get_text()
i += 1
if __name__ == "__main__":
main()