-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdictionary.py
58 lines (48 loc) · 1.93 KB
/
dictionary.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
import requests
"""
I know it's spaghetti code but cut me some slack...
I'm now just learning OOP techniques
"""
error_message = "Sorry , try searching the web for that."
class Dictionary:
def __init__(self):
return
"""
Enclose everything in a try-except block since planning to fail is good practice
"""
# In case the word is a noun
def noun(self, word):
content = requests.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}").json()
noun_def = content[0]["meaning"]["noun"][0]["definition"]
if not noun_def:
return error_message
return noun_def
# In case the word is a verb
def verb(self, word):
content = requests.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}").json()
verb_def = content[0]["meaning"]["verb"][0]["definition"]
if not verb_def:
return error_message
return verb_def
# In case the word is an adjective
def adjective(self, word):
content = requests.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}").json()
adj_def = content[0]["meaning"]["verb"][0]["definition"]
if not adj_def:
return error_message
return adj_def
# In case the word is a transitive verb
def trans_verb(self, word):
content = requests.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}").json()
trans_verb_def = content[0]["meaning"]["transitive verb"][0]["definition"]
if not trans_verb_def:
return error_message
return trans_verb_def
# Getting the audio pronunciation
def audio(self, word):
content = requests.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}").json()
audio = content[0]["phonetics"][0]["audio"]
if not audio:
message = "Can't seem to find the pronunciation, try searching the web"
return message
return audio