-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractive_dictionary.py
55 lines (41 loc) · 1.34 KB
/
Interactive_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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 16:24:13 2018
@author: Vineet
"""
#required libraris import
import json
import difflib
#loading json dataset file
try:
data = json.load(open("data.json"))
print("Data set loaded")
except:
print("Data set failed to load")
def translate(word):
word = word.lower()
if word in data:
return data[word]
elif word.title() in data:
return data[word.title()]
elif word.upper() in data: #conditional for Acronyms
return data[word.upper()]
elif len(difflib.get_close_matches(word, data.keys())) > 0:
suggestion = input("Did you mean %s instead! Enter Y for yes, N for no: " % difflib.get_close_matches(word, data.keys())[0])
if suggestion.lower().upper() == "Y":
return data[difflib.get_close_matches(word, data.keys()) [0]]
elif suggestion.lower().upper() == "N":
return "word dosen't exist"
else:
return "Try again"
else:
print("Word dosen't exist! Try again")
word_definations = input("Enter word: ")
output = translate(word_definations)
if type(output) == list:
counter = 1
for word_meaning in output:
print("%s. %s"%(str(counter),word_meaning))
counter +=1
else:
print(output)