Hi folks! Welcome to this brand new tutorial. Today we will create an English dictionary that takes a word as input from user and returns back the meaning of that word. During this tutorial we will learn some powerful aspects of Python. We will learn some new ways in which we can deal with Python strings. So let's latch onto it straight away!
What we are going to build
We are going to build a dictionary that operates on command line. It takes a word as input from user. This word is then searched in a data file stored locally on the computer. It then returns one or more meanings for the word. This program will incorporate a glimpse of artificial intelligence and if the user misspells the word by a little margin then it will suggest the correct word. Sounds exciting right? So lets begin building this.
- Python 3 installed on your computer
- Basic knowledge of Python
Coding the program
To store the collection of thousands of English words and their meanings we will use a JSON file. JSON files are taylor made for such scenarios. A JSON file stores data as key and value pairs, something very similar to a Python data type called dictionary. The keys are immutable (unchangeable) and are used to access the values. Our data file is a list of key and value pairs. The string taken from user will be searched among the keys of the file and if a match is found, the meaning will be returned from the corresponding value. So lets first import JSON library to handle the JSON files
Now lets open our JSON data file using
open() function and load it into a variable called
collection using the
load() method from JSON library.
collection = json.load(open("collection.json"))
Now we will build the function that takes the user’s string as an argument and then searches it inside the
def lookup(word): if word in collection: return collection[word]
This will check the collection for the required word and if the word is there then the corresponding
key:value pair will be returned.
At this point we can print the meaning of word but as a programmer we have to account for a lot of things that can go wrong when the user enters the word. For example Python is a case sensitive programming language and our JSON collection is also in lowercase. So when the user enters a word in uppercase, it will not be found even if it is present in the JSON file. There is a simple solution for this. We simply convert the user input into lowercase using the
lower() function. The
lower() function takes a string and converts it into lowercase. For example if the user enters Guava or GUAVA or GUava or guavA, it will always be converted into guava before being searched.
def lookup(word): word = word.lower() if word in collection: return collection[word]
Now consider some more scenarios
- The user hits enter without entering a word
- The user misspells the word
If the user makes a blank entry then we will simply tell the user that no word was entered.
else: return "No word was entered"
Now we have only one problem to solve, the misspelled word. Manually solving this problem would take a lot of code and hard work but thanks to Python’s modules we can do this by simply calling a function.
get_close_matches() function from
difflib module comes to rescue. Difflib is a library of functions in Python that compares sequences and strings and
get_close_matches() is a function in
difflib that returns close matches to a string, just like the name suggests.
Lets scroll to the top of the script and import
get_close_matches() function from
import from difflib import get_close_matches
Now we will get some close matches if the user enters a word with wrong spellings.
elif len(get_close_matches(word, collection.keys())) > 0:
get_close_matches() is wrapped inside
len() function which returns the length of a string. This length is then compared for a non zero value. This means that the code inside this function will only be executed if the user enters something. If the user enters nothing, this block of code will be skipped and next block will be executed.
Now we want to prompt the user again for input to verify that the close match our program has fetched is the one that user wants. Therefore we ask the user again for an input. Here the user is given two options. Yes or No. If the user opts for Yes then the close match returned is the one user is looking for. If the user enters No then we can conclude that the word user is looking for does not exist in our collection of words.
yn = input("Are you looking for the word %s ? Enter Y for Yes or N for No: " % get_close_matches(word, collection.keys())) if yn == "Y": return collection[get_close_matches(word, collection.keys())] elif yn == "N": return "Word not found. Please try again." ```sh And if the user enters something other than Y or N then we need to account for that also. ```sh else: return "Please enter either Y or N"
And with this line of code our function
lookup() is complete. This function will not only search for the meaning of the word but also handle lot of things that can go wrong during the user input. OK now, all bases covered. Let’s take some input from user.
print('\n***** WELCOME TO THE PYTHON ENGLISH DICTIONARY *****\n') word = input("Enter word: ")
Now we call our
lookup() function to do the job for us.
output = lookup(word)
Now we can simply display the output with print function. If the word has more than one meaning then we print the meanings in seperate lines.
if type(output) == list: for item in output: print(item) else: print(output)
And there we are. Our code is complete. We have now a full fledged dictionary application written in Python with a lot of functionality. Let’s try it now. Let’s run our application using command line. Lets enter a word, say Guava The meaning of word is displayed. Now lets misspell our word. Let’s enter a word that has more than one meanings. Great! So now we have a tried and tested English dictionary application scripted in Python that runs on command line and returns the meanings of entered words. The program is intelligent enough to guide the user in case of a mistake thanks to a bunch of if-else conditionals that account for different scenarios. Remember that such applications tend to have bugs and we fix the bugs when we encounter them. This improves our program step by step untill it is running smooth and error free. So that’s about it. I hope you enjoyed this and learnt some new cool features of Python. See you later.
import json from difflib import get_close_matches collection = json.load(open("collection.json")) def lookup(word): word = word.lower() if word in collection: return collection[word] elif len(get_close_matches(word, collection.keys())) > 0: yn = input("Are you looking for the word %s ? Enter Y for Yes or N for No: " % get_close_matches(word, collection.keys())) if yn == "Y": return collection[get_close_matches(word, collection.keys())] elif yn == "N": return "Word not found. Please try again." else: return "Please enter either Y or N" else: return "No word entered" print('\n***** WELCOME TO THE PYTHON ENGLISH DICTIONARY *****\n') word = input("Enter word: ") output = lookup(word) if type(output) == list: for item in output: print(item) else: print(output)
I have uploaded the collection.json file here if you want to access it: https://www.dropbox.com/s/bnq70f4dh2lmwlw/collection.json