-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
36 lines (30 loc) · 882 Bytes
/
dictionary.py
File metadata and controls
36 lines (30 loc) · 882 Bytes
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
import json
from difflib import get_close_matches
def display(definitions):
"""
Display the definitions
:param definitions:
:return:
"""
for definition, index in zip(definitions, range(1, len(definitions) + 1)):
print(str(index) + ': ' + definition)
def lookup(word):
"""
Look up a word in the dictionary
:param word:
:return:
"""
# Load data
data = json.load(open('data.json'))
if word in data:
display(data[word])
elif word.lower() in data:
display(data[word])
elif len(get_close_matches(word, data.keys())) > 0:
closest_word = get_close_matches(word, data.keys())[0]
answer = input("Did you mean '%s' instead? [y/n]" % closest_word)
if answer == 'y':
display(data[closest_word])
return
else:
print("No definition available.")