PROJECT 1: A DICTIONARY
Step 1: Create a folder where you want to keep all the files of this project.
Step2: Download your dataset for the dictionary
Step 3: Let’s jump into some coding
1. As our dictionary data is in JSON format, we will use JSON Standard Library to retrieve the data.
import json
You can also use help command in the python cmd to know what each method is used for.
help(json.load)
2. Now we will create an object to store the data of the JSON file. Here, my file name is data.json
data = json.load(open("data.json"))
If you want to check the type of the object created, use this command
type(data)
3. Print the data and check if we are on the right path
print(data)
print(data["rain"])
4. Let’s combine the above written code and create a flow
import json
data = json.load(open("data.json"))
def definition(inp): #function to return the desired data
return data[inp]
word = input("Please enter your Word here: ")
#takes an input from the user
word2 = str(definition(word))
#converts the list object to string object
print("It's meaning: " + word2)
# prints the concatenation of both strings
5. It is a good practice to create an application by solving one issue at a time.
ISSUE 1 : The program ends if wrong spelling or incorrect word is entered by the user
SOLUTION : We will include if-else statement which will check if the word exists in the dictionary or not
import json
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
else:
return "The word does not exist"
word = input("Please enter your Word here: ")
word2 = str(definition(word))
print("It's meaning: " + word2)
ISSUE 2: If the input word is in uppercase or a combination of cases, the program throws an error
SOLUTION: As all the data in the JSON file is in lower case, we will convert our input into lowercase before it enters the function
import json
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
else:
return "The word does not exist"
word = input("Please enter your Word here: ")
word2 = str(definition(word.lower()))
print("It's meaning: " + word2)
LET’S MAKE OUR PROGRAM MORE INTELLIGENT!!
ISSUE 3: What if the user doesn’t know the correct spelling and mistyped the word
SOLUTION: we will use a special library “difflib”
To know more about difflib and it’s methods, use the following code in the python cmd
import difflib
from difflib import get_close_matches
help(get_close_matches)
import json
import difflib
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) > 0:
i = input("Did you mean %s ? Press Y for Yes and N for NO " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
if i == "y" or i == "Y":
return data[d]
elif i == "n" or i == "N":
return "Please double check the word and try again"
else:
return "Invalid input!!"
else:
return "The word does not exist"
user_word = input("Please enter your Word here: ")
meaning_word = definition(user_word.lower())
print(meaning_word)
ISSUE 4: If there is more than one meaning of a word, we get a list of all meanings together as the output
SOLUTION: We can use for loop to print each list item in separate lines
import json
import difflib
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:
i = input("Did you mean %s ? Press Y for Yes and N for NO " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
if i == "y" or i == "Y":
return data[d]
elif i == "n" or i == "N":
return "Please double check the word and try again"
else:
return "Invalid input!!"
else:
return "The word does not exist"
user_word = input("Please enter your Word here: ")
meaning_word = definition(user_word.lower())
for mw in meaning_word: #to distinguish each meaning of the word
print(mw)
ISSUE 5: Whenever we press N, return gives us a string. The for loop prints out each letter of that string as the output (vertically). That means sometimes we get a string and sometimes we get a list as the output
SOLUTION: We can use if-else statement to check the type of the output generated
import json
import difflib
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:
i = input("Did you mean %s ? Press Y for Yes and N for NO " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
if i == "y" or i == "Y":
return data[d]
elif i == "n" or i == "N":
return "Please double check the word and try again"
else:
return "Invalid input!!"
else:
return "The word does not exist"
user_word = input("Please enter your Word here: ")
meaning_word = definition(user_word.lower())
if type(meaning_word) == list:
for mw in meaning_word: #to distinguish each meaning of the word
print(mw)
else:
print(meaning_word)
ISSUE 6: If any common noun such as city names are given as input, the program says that the word does not exist.
SOLUTION: This is because the dictionary has saved the city names with first letter capital. Therefore, we will use title() method as another if-else condition to check such kind of words in the dictionary.
import json
import difflib
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
elif inp.title() in data:
return data[inp.title()]
elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:
i = input("Did you mean %s ? Press Y for Yes and N for NO " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
if i == "y" or i == "Y":
return data[d]
elif i == "n" or i == "N":
return "Please double check the word and try again"
else:
return "Invalid input!!"
else:
return "The word does not exist"
user_word = input("Please enter your Word here: ")
meaning_word = definition(user_word.lower())
if type(meaning_word) == list:
for mw in meaning_word:
print(mw)
else:
print(meaning_word)
ISSUE 7: What if acronyms are entered such as USA ? the program cannot identify such inputs
SOLUTION: Similar to the above issue, we will use upper() method to identify such words
import json
import difflib
data = json.load(open("data.json"))
def definition(inp):
if inp in data:
return data[inp]
elif inp.title() in data:
return data[inp.title()]
elif inp.upper() in data:
return data[inp.upper()]
elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:
i = input("Did you mean %s ? Press Y for Yes and N for NO " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
if i == "y" or i == "Y":
return data[d]
elif i == "n" or i == "N":
return "Please double check the word and try again"
else:
return "Invalid input!!"
else:
return "The word does not exist"
user_word = input("Please enter your Word here: ")
meaning_word = definition(user_word.lower())
if type(meaning_word) == list:
for mw in meaning_word:
print(mw)
else:
print(meaning_word)
Andddd…. FINALLY OUR FIRST PROJECT IS COMPLETE!!
CONGRATULATIONS!! YOU DID A GREAT JOB ??