UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Build an interactive Dictionary using Python 3.x

B
Biswanath Banerjee
16/11/2018 0 0

 I have downloaded a 5MB data base consisting of words and their meanings in a json file . I want to write a python progrsm which will ask the user to enter a word and then the program will search through the database and retuen back the meaning of the word . There can be instances when there are two or three different meanings for a word. The program will display all the different meanings of the word.

I wanted to make the program little intelligent. What if the user spells he word incorrectly while entering the word? Suppose he wants to know the meaning of 'crowd' but by mistake he types in 'crowded'. The system will be intelligent enough to suggest few words nearest to the word 'crowdd' which he has typed and let the user choose the right word which he meant from the list of suggesstions.

Also , there are checks for cases - upper case, lower case etc.

Let us see the code now and then run the program to see the outputs:

Here is the python code which you have been eaiting for so long:

import json
from difflib import get_close_matches

data = json.load(open("data.json"))

print(data)

def translate(w):
w = w.lower()
if w in data:
return data[w]
if w in data:
return data[w]
elif w.title() in data: # user entered "maryland" will check for "Maryland" as well
return data[w.title()]
elif w.upper() in data:
return data[w.upper()]
elif len(get_close_matches(w,data.keys()))>0:
yn= input("Did you mean %s instead?. EnterY if yes, or N if no: "% get_close_matches(w,data.keys())[0])
if yn=='Y':
return data[get_close_matches(w,data.keys())[0]]
elif yn =='N':
return("the word does not exist")

else:
return ('We did not understand your entry')
else:
return("the word does not exist")
word = input("Enter word")
output =translate(word)
if type(output) == list:
for item in output:
print(item)
else:
print(output)

Let us test out different scenarios now :

Input: data["crowd"]

Output: ['A large number of people united for some specific purpose.', 'A crowd of people pressed close together in a small space.', '6-string musical instrument of Welsh or Irish origin and played with a bow.', 'A large group of people.', 'To cause to herd, drive, or crowd together.', 'To fill or occupy in a small space to the point of overflowing.', 'To gather together in large numbers.']

Input:

Enter word crowdd
Output:Did you mean crowded instead?. EnterY if yes, or N if no: 
Input: N
Output: the word does not exist

Another scenario. Suppose the User does not type the whole word but abbreviation like 'USA' or 'UK'.
How does our dictionary program find out the meaning in such case?
Input: Let us try out: Enter word UK
Output: A country in Western Europe (comprising Wales, Scotland, England and Northern Ireland) with the capital London.

This is a simple program to show you how to write simple Applications using Python.
Stay tuned to my Lessons. Next Lesson I will show you how to write another interesting interactive application
using Python.
0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Visualize your python code
Hi All, Many developers in the beginning of their career want to visualize how their code is working and the program flow. You can use the following link to visualize your python code. http://www.pythontutor.com/visualize.html#mode=edit ...

Small ML Project on Simple Linear Regression
Here is a small Project on Simple Linear Regression

Debugging Python code
If you are getting an error while executing your python code, the better way of running your python code is by using -i switch. ex: python -i scriptname.py once you run the above command, your execution...

Be A Python Dev
Currently, in the programming world, Python is one of the languages with a high rising demand profile. And this article will explain why that isn’t slowing down in the foreseeable future.Python...

Decorators In Python
A decorator takes in a function, adds some functionality and returns it. Functions and methods are called callable as they can be called. Any object which implements the special method __call__() is...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more