-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary_Search.py
More file actions
74 lines (56 loc) · 2.3 KB
/
Dictionary_Search.py
File metadata and controls
74 lines (56 loc) · 2.3 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
""""
@author: Abu Zayed
@Name: Dictionary search using data.json
""""
import json
import difflib
from difflib import SequenceMatcher
userInput = ""
count = 0
while userInput != "y":
count = count + 1
#ToDo: Find meaning of word from data.json file
data = json.load(open("data.json")) # putting the json data inside variable data
#ToDo: The user enters a word, converts to small letter and then searchs
userInput = input("Enter word: ").lower()
if userInput in data:
sanitizedWord = difflib.get_close_matches(userInput, data.keys())
print(f"{sanitizedWord} is sanitized word")
for word in sanitizedWord:
if userInput == word:
print(data[userInput])
break
# comparing userInput to key inside json
#loop through the keys in the dictionary
for char in data.keys():
if char in userInput: #checking to see if key is similar to user input
sequence = SequenceMatcher(None, userInput, char).ratio()
if sequence == 1.0:
lenVal = len(data[char])
for x in data[char]:
print(str(data[char]).replace(', ',',\n*****\n'))
elif sequence > 0.75 and sequence < 0.9:
clarityInput = input(f"Did you mean {char} instead? Enter Y if yes, or N if no: ")
if clarityInput == "Y".lower():
print(data[char])
elif clarityInput == "N".lower():
print("input inner if not in data")
elif sequence < 0.0:
print("Invalid input")
#ToDo: If word not found print friendly error
# if userInput in data:
# print(data[userInput])
#elif userInput not in data:
# elif userInput not in data and clarityInput:
# print("Invalid input")
#exit check
if userInput == "quit now":
clarityInput = input("Are you sure you want to exit?(Y/N): ").lower()
if clarityInput == "Y".lower():
break
#ToDo: If word found the meaning is shown.
#Word can have more than one meaning. So it should show all the meanings.
#Delimiters between meanings
#ToDo:If the word has a spelling mistake then the dictionary suggests the closest word
# and ask the user if this is what they are looking for
#