Repository navigation
Expand file tree
/
Copy pathdict.py
More file actions
116 lines (99 loc) · 3.79 KB
/
Copy pathdict.py
File metadata and controls
116 lines (99 loc) · 3.79 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
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
import os
import sys
import urllib.request, urllib.parse, urllib.error
import json
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
print(bcolors.OKBLUE + "++++++Terminal Based google-dictionary++++++++++\n" + bcolors.ENDC)
# taking word from user / commandline
if len(sys.argv) != 2:
title = input(bcolors.OKBLUE + "\nPlease input word to search : " + bcolors.ENDC)
else:
title = os.environ["word"]
flag = 0
print(bcolors.OKGREEN + "\n\\\\\\\\\\\\\\\\\\\\\\\\START\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n" + bcolors.ENDC)
print ("Word: ", bcolors.BOLD + title + bcolors.ENDC )
try :
url = 'https://api.dictionaryapi.dev/api/v2/entries/en_US/'+title
result = json.load(urllib.request.urlopen(url))
except urllib.error.HTTPError as e: ResponseData = 'failed'
except :
print('Connect to-- Internet!! ', sys.exc_info()[0])
os._exit(0)
def get_meaning(title):
try :
url = 'https://api.dictionaryapi.dev/api/v2/entries/en_US/'+title
result = json.load(urllib.request.urlopen(url))
except urllib.error.HTTPError as e: ResponseData = 'failed'
except :
print('Connect to Internet!!', sys.exc_info()[0])
os._exit(0)
try :
print('\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ MEANING of {} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n'.format(title))
print(bcolors.UNDERLINE + result[0]["meanings"][0]["definitions"][0]["definition"] + bcolors.ENDC)
print(bcolors.OKGREEN + "\n\\\\\\\\\\\\\\\\\\\\\\\\\END\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n" + bcolors.ENDC)
except :
print('!!Error!! Please Check the Spelling!', sys.exc_info()[0])
os._exit(0)
return
def get_synonyms(title):
try :
url = 'https://api.dictionaryapi.dev/api/v2/entries/en_US/'+title
result = json.load(urllib.request.urlopen(url))
except urllib.error.HTTPError as e: ResponseData = 'failed'
except :
print('Connect to Internet!!', sys.exc_info()[0])
os._exit(0)
try :
print('\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ SYNONYMS of {} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n'.format(title))
for x in range(3):
print(bcolors.UNDERLINE + result[0]["meanings"][0]["definitions"][0]["synonyms"][x] + bcolors.ENDC)
except :
print('!!Error!! ', sys.exc_info()[0])
os._exit(0)
return
def get_example(title):
try :
url = 'https://api.dictionaryapi.dev/api/v2/entries/en_US/'+title
result = json.load(urllib.request.urlopen(url))
except urllib.error.HTTPError as e: ResponseData = 'failed'
except :
print('Connect to Internet!!', sys.exc_info()[0])
os._exit(0)
try:
print('\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ EXAMPLE of {} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n'.format(title))
print(result[0]["meanings"][0]["definitions"][0]["example"])
except :
print('!!Error!! ', sys.exc_info()[0])
os._exit(0)
return
get_meaning(title)
while flag == 0:
print('-----------------------------------')
print(' ``Press 1 to get synonyms`` ')
print(' ``Press 2 to get an example `` ')
print(bcolors.WARNING + ' ``Press 9 to quit``' + bcolors.ENDC)
print('-----------------------------------')
try :
char = int(input())
if char == 1:
get_synonyms(title)
elif char == 2:
get_example(title)
elif char== 9 :
print('See you soon!')
flag = 1
else:
print('Wrong key pressed')
except :
print('INCORRECT KEY', sys.exc_info()[0])
pass
print(bcolors.OKGREEN + "\n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\END\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n" + bcolors.ENDC)