-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_lookup.py
39 lines (30 loc) · 1.11 KB
/
dictionary_lookup.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
import json
import difflib
from difflib import SequenceMatcher
from difflib import get_close_matches
data = json.load(open("data.json"))
def definition(w):
#w = w.lower()
if w in data:
return data[w]
else: #takes care of most exceptions
if w.title() in data: #capitalised letters
return data[w.title()]
else:
match = get_close_matches(w,data.keys(),cutoff = 0.8)
if len(match) > 0: #similar words
yn = input("Did you mean %s instead? Enter Y if yes, N if no: " % match[0])
if str.islower(yn): yn = str.swapcase(yn)
if yn == "Y":
return data[match[0]]
elif yn != "N":
return "We do not understand"
else:
return "The word doesnt exit. Please enter another word"
word = input("Enter word: ")
output = definition(word)
if type(output) == list:
for item in output:
print(item)
else:
print(output)