English dictionary application using Python

Python | English dictionary application: Here, we will see how can we create a python script to develop a vocab dictionary?
Submitted by Abhinav Gangrade, on June 19, 2020

Module used:

In this script, we will use the JSON module because we will use a JSON file and loading requires the JSON module. We will use difflib module.

What is JSON?

JSON (JavaScript Object Notation) is an inbuilt python library which provides us various functions to read and write the file with .json extension and it usually deals with the file which looks like a python dictionary.

What is difflib?

A python inbuilt module that provides us with various functions to compare various sequences in python, from this module we will use the get_close_matches() function which gives us the list of words which are very close to the user's word.

Note: There is a JSON file that you have to download which contains all the words with their meaning but as there are too many words we are not able to access them that's why we will make this script.

Creating An English Dictionary In Python

First of all, we will take the users input as now the user can give us input in any way (.lower manner, .upper manner, .title manner) so we have to check for every condition, sometimes what happens the user's input didn't get matched with the words in the JSON file. In this case, we will provide similar words for the user's input with the help of get_close_matches() function.

Note: The JSON file must be in the same directory otherwise you have to specify the path of the file.

Program:

# import modules
import json
from difflib import get_close_matches

# load the data of json file
# (here file name is dict.json)
data=json.load(open("dict.json"))

# create a function to get the the meaning
def Get_meaning(word):
    if word in data:
        # as data will be in the form of dictionary
        # and this condition is to check if the 
        # meaning of the words are more than one 
        # and the value of each key is in the form of list
        if len(data[word])>1:
            # print each meaning
            for i in data[word]:
                print(i)
        # else will print the only element in the list
        else:
            print(data[word][0])
    # we will check the input in all forms
    elif word.lower() in data:
        if len(data[word.lower()])>1:
            # print each meaning
            for i in data[word.lower()]:
                print(i)
        # else will print the only element in the list
        else:
            print(data[word.lower()][0])
    elif word.upper() in data:
        if len(data[word.upper()])>1:
            # print each meaning
            for i in data[word.upper()]:
                print(i)
        # else will print the only element in the list
        else:
            print(data[word.upper()][0])
    elif word.title() in data:
        if len(data[word.title()])>1:
            # print each meaning
            for i in data[word.title()]:
                print(i)
        # else will print the only element in the list
        else:
            print(data[word.title()][0])
    # now if these three doesnt match with the input
    # then we will find the closest match
    else:
        # get close match will return us the list of all
        # close words with the input
        close_match=get_close_matches(word,data.keys())
        # if there are 1 or more elements
        if len(close_match)>0:
            # now we will print the meaning of closest words
            # that is the first element
            decide=input("Enter yes or no to move forward: ")
            if decide=="yes":
                # print the meaning of the closest word
                print(f"The closest word to this is {close_match[0]}")
                for i in data[close_match[0]]:
                    print(i)
            else:
                print("There is no word related to it")
        else:
            print("Cant Found this word")
word=input("Enter the word: ")
Get_meaning(word)

Output:

RUN 1:
Enter the word: Abhi
Enter yes or no to move forward: yes
The closest word to this is Abi
ISO 639-6 entity
A Kwa language spoken by the Abé people primarily 
in the Department of Agboville in Côte d'Ivoire.

RUN 2:
Enter the word: Hello
Expression of greeting used by two or more people who meet each other.
Python | English Dictionary Application Output

The link to Download the JSON dictionary File: https://github.com/abhinav0606/dictionary.py/blob/master/dict.json

Or, download from here. dist.json


Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.