python 英语词典
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.
在此脚本中,我们将使用JSON模块,因为我们将使用JSON文件并且加载需要JSON模块。 我们将使用difflib模块。
What is JSON?
什么是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.
JSON (JavaScript对象表示法)是一个内置的python库,它为我们提供了多种功能来读写具有.json扩展名的文件,并且通常处理类似于python字典的文件。
What is difflib?
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.
一个python内置模块,为我们提供了各种功能来比较python中的各种序列,在该模块中,我们将使用get_close_matches()函数,该函数为我们提供了与用户单词非常接近的单词列表。
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.
注意:您必须下载一个JSON文件,其中包含所有带有其含义的单词,但是由于单词太多,我们无法访问它们,因此我们将制作此脚本。
用Python创建英语词典 (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.
首先,我们将以用户输入为准 ,因为现在用户可以以任何方式( .lower方式 , .upper方式 , .title方式 )给我们输入,因此我们必须检查每个条件,有时用户输入发生了什么情况与JSON文件中的单词不匹配。 在这种情况下,我们将在get_close_matches()函数的帮助下为用户输入提供类似的单词。
Note: The JSON file must be in the same directory otherwise you have to specify the path of the file.
注意: JSON文件必须位于同一目录中,否则您必须指定文件的路径。
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.
The link to Download the JSON dictionary File: https://github.com/abhinav0606/dictionary.py/blob/master/dict.json
下载JSON字典文件的链接:https://github.com/abhinav0606/dictionary.py/blob/master/dict.json
Or, download from here. dist.json
或者,从此处下载。 dist.json
翻译自: https://www.includehelp.com/python/english-dictionary-application-using-python.aspx
python 英语词典