-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.py
More file actions
62 lines (52 loc) · 1.86 KB
/
Copy pathDictionary.py
File metadata and controls
62 lines (52 loc) · 1.86 KB
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
59
60
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 15 18:26:57 2020
@author: Monjur Bin Shams
"""
import json
from difflib import get_close_matches
#saving the dictionary in a variable format from json format
dict = json.load(open("dict.json",'r'))
#function for searching the word in the dictionary
def searchword(word):
word = word.lower()
if word in dict:
return dict[word]
#get_close_match returns a list of close matches. So we check if the length of that list is greater than zero
#if it is greater than zero, then we give the first index to the user to check
elif len(get_close_matches(word, dict.keys()))>0:
c2 = input("Did you mean %s? y/n: " %get_close_matches(word, dict.keys())[0])
c2.lower()
if (c2 == "y"):
return dict[get_close_matches(word, dict.keys())[0]]
elif c2=="n":
return "\nThe word does not exist in the dictionary"
else:
return "\nInvalid choice, better luck next time"
else:
return "\nThe word does not exist in the dictionary"
#function for choosing to search again without closing the program
def choice(c):
if (c=='y'):
word = input("Enter the word:")
inputword(word)
elif c=="n":
print("\nThanks for using the dictionary")
return
else:
print("\nInvalid choice, better luck next time")
return
#function for processing the input word and taking the choice for searching again
def inputword(word):
output = ((searchword(word)))
if type(output)==list:
for item in output:
print(item)
else:
print(output)
c = input("\nDo you want to search again? If yes, press y/n:")
c.lower()
choice(c)
#taking input from the user
word = input("Enter the word:")
inputword(word)