-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_dict.py
47 lines (39 loc) · 1.66 KB
/
my_dict.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
import json
import os
from difflib import SequenceMatcher as sm # to get the ratio of matched words
from difflib import get_close_matches # get closely releted words
folder = os.path.dirname(os.path.abspath(__file__))
datafile = os.path.join(folder,'data.json')
data = json.load(open(datafile))
def descrip(key):
if not key in data: # if search key isn't found in file
mesg = get_close_matches(key, data.keys(), n=5) # get most close 5 words
if len(mesg) > 0:
yesOrNo = input('did u mean "%s" instead, press y/n or Y/N:' % get_close_matches(
key, data.keys())[0])
if yesOrNo == 'y' or yesOrNo == 'Y':
result = ''
value = data[get_close_matches(key, data.keys())[0]]
for i in value:
result += i
result += '\n\n suggested words: '
for i in mesg:
result += i+', '
result += result[:-2]
result += '\n\n' + '"' + key + '"' + ' matched with suggested words: '
for i in mesg:
matchPercen = str(sm(None, key, i).ratio())
result += matchPercen + ' , '
return result[:-2]
elif yesOrNo == 'n' or yesOrNo == 'N':
return 'word isn\'t found'
else:
return 'y/n or Y/N input error'
else: # if search key is found in file
valus = data[key]
result = ''
for i in valus:
result += i
return result
userInput = input()
print(descrip(userInput.lower()))