Learning how to code is one thing, using your code for a real-world application is another. While this small project is for those with basic Python skills, anyone who is interested in it can also take it up.
Learning a programming language can seem boring if you don’t apply it to something that interests you, and this is one of those fun projects where you get to test your basic Python skills and trust me, you will enjoy it.
Building an interactive English dictionary might sound complicated, but it isn’t. With just a text editor and basic Python skills, you will complete this project in less than half an hour. You will not only learn how to build a word dictionary using the code I provide here, but also understand the logic behind it all, and then you can start building your own, custom projects.
What is a Dictionary?
The English language is not the only language with dictionaries. Python also has a dictionary, and it is important for us to know the similarities between them before we move on to building our application.
Just as the English dictionary has a “word-meaning” pair (a word and its corresponding meaning), the Python dictionary also has a “key:value” pair. But unlike the English dictionary, the Python dictionary is an unordered collection of data and each key is separated from its value by a colon (:).
Now that you know what a Python dictionary looks like and its similarities with the English dictionary, let’s go ahead and build our application.
Starting from Scratch
Before building anything, you need to make sure you have Python installed on your computer (preferable version 3 because that is what we are going to work with) and a text editor alongside it to write your codes. If you don’t have Python installed, head over to their website, and follow the instructions there to get started.
Now that we have got that out of the way, let’s start building.
Download a JSON file containing English words in a Python dictionary format (remember the “key:value” pair we talked about?).
Create a file and call it “dictionary.py” and rename the just downloaded JSON file to “words.json”. Move the words.json and dictionary.py files to a folder to keep everything organized.
Open the dictionary.py file and write in these codes
Note: There are two important modules we imported for our application namely: json and difflib
difflib is also a built-in module in Python that provides classes and functions like get_close_matches for comparing sequences of data.
We load the dictionary (words.json) into the application
“Open” is used to open a file in “read mode” (by default) and json.load is used to deserialize a file path (like the words.json file) to a Python object (a dictionary in our case).
Next, we define and call a function that fetches the word from the words.json file. Type in this code into the dictionary.py file.
Line 1: We defined a function – fetch to fetch words from our dictionary (words.py)
Line 2: We changed the word entered into a small letter because our dictionary is case sensitive.
Line 3-6: We write a conditional statement to check if the word is in the dictionary, If it is, it should return the word (data[word]). If it does not exist, It should return a custom error saying the word does not exist.
Line 7: The input keyword is used to prompt a user to input something. In our case, we told the user to enter a word.
Line 8: We call the function – fetch and print the output.
At this stage, the dictionary is working perfectly but to make it more interactive, we need to add more codes to it. The application code now looks like this:
from difflib import get_close_matchesdata = json.load(open(‘words.json’))def fetch(word):
if word in data:
elif len(get_close_matches(word, data.keys()))>0:
n= input(“Did you mean %s instead? Enter y if Yes, or n if No: “% get_close_matches(word,data.keys()))
return “The word doesn’t exist.Please double check it.”
return “Sorry, we did not understand your entry.”
return “The word cannot be found.”
Word = input (“Enter a word to check: ”)result = fetch(word)if type(result) == list: for value in result: print(value)
Line 5-7: We added an elif (else if) statement. If the word entered could not be found, the get_close_matches function is called and if the get_close_matches array is greater than 0 (if it is not empty), a custom message is sent to the user suggesting other related words.
Line 9-14: If the user’s reply is yes, return the value of the most related word. If the reply is no, return a custom error, and finally, if the reply is neither yes nor no, return a custom error.
LIne 15: If the word could not be found at all, return “The word cannot be found”.
Line 17: We call the function and store it in a variable
Line 18-20: If the result is a list, we loop over the result and print them line by line
Line 21-22: If the result is not a list, we just print the result.
And there you have it, an interactive dictionary in less than an hour using only basic python skills. You can always decide to take this project to the next level and add a graphical interface to it.