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

Using the random module: another program.
A Python module is just a file that contains reusable code like functions. If a program is going to use functions that are stored in another module, it needs to first import that module.The random module...

Two lists using Loops - Python Program 1
To generate a list of all possible combinations of two lists using loops, you can use nested loops to iterate through both lists and create a new list that contains all the combinations. list1 = list2...

Benefits of Design Patterns in Application Development
Application developments is a tedious job. Programmers write code and test it again and again to make sure their software works fine. So designing complex software is a hard nut to crack. Skilled programmers...

Python Programming or R- Programming
Most of the students usually ask me this question before they join the classes, whether to go with Python or R. Here is my short analysis on this very common topic. If you have interest/or having a job...

Python Important Operation Formats and Sytnax.
Hello Python Enthusiast, Though Python is considered as one of the easisest language to learn in the market, many freshers and beginners will find great difficulty in understansing the syntax and also...
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