File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ #import necessary libraries
2
+ import json
3
+ from difflib import get_close_matches
4
+
5
+ #load json file
6
+ data = json .load (open ('data.json' ))
7
+
8
+
9
+ # Creating a function to get meaning of the given word from our dictionary
10
+ def get_meaning (word ):
11
+ # Scenario 1: word is in our dictionary
12
+ if word in data :
13
+ return data [word ]
14
+ # Scenario 2: There is typo in users input, check for closest matching word
15
+ elif len (get_close_matches (word , data .keys (), cutoff = .8 )) > 0 :
16
+ match = get_close_matches (word , data .keys (), cutoff = .8 )[0 ]
17
+ check = input ("Did you mean %s, instead? Enter Y if yes, otherwise N: " % match )
18
+ if check .lower () == 'y' :
19
+ return data [match ]
20
+ elif check .lower () == 'n' :
21
+ return "Word doesn't exist"
22
+ else :
23
+ return "Invalid choice"
24
+ # if word is not not at all available in our vocabulary
25
+ else :
26
+ return "The Word Doesn't exist in the dictionary, sorry!"
27
+
28
+ while True :
29
+ #get a word from user
30
+ word = input ('Enter a word you want to know the meaning of: ' )
31
+ if word == "quit" :
32
+ break
33
+ #print the output
34
+ output = get_meaning (word .lower ())
35
+
36
+ # If certain word has more than one meaning then iterate over them
37
+ if type (output ) == list :
38
+ for item in output :
39
+ print (item )
40
+ else :
41
+ print (output )
42
+
You can’t perform that action at this time.
0 commit comments