2022年 11月 9日

python 英语词典_使用Python的英语词典应用程序

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:

程序:

  1. # import modules
  2. import json
  3. from difflib import get_close_matches
  4. # load the data of json file
  5. # (here file name is dict.json)
  6. data=json.load(open("dict.json"))
  7. # create a function to get the the meaning
  8. def Get_meaning(word):
  9. if word in data:
  10. # as data will be in the form of dictionary
  11. # and this condition is to check if the
  12. # meaning of the words are more than one
  13. # and the value of each key is in the form of list
  14. if len(data[word])>1:
  15. # print each meaning
  16. for i in data[word]:
  17. print(i)
  18. # else will print the only element in the list
  19. else:
  20. print(data[word][0])
  21. # we will check the input in all forms
  22. elif word.lower() in data:
  23. if len(data[word.lower()])>1:
  24. # print each meaning
  25. for i in data[word.lower()]:
  26. print(i)
  27. # else will print the only element in the list
  28. else:
  29. print(data[word.lower()][0])
  30. elif word.upper() in data:
  31. if len(data[word.upper()])>1:
  32. # print each meaning
  33. for i in data[word.upper()]:
  34. print(i)
  35. # else will print the only element in the list
  36. else:
  37. print(data[word.upper()][0])
  38. elif word.title() in data:
  39. if len(data[word.title()])>1:
  40. # print each meaning
  41. for i in data[word.title()]:
  42. print(i)
  43. # else will print the only element in the list
  44. else:
  45. print(data[word.title()][0])
  46. # now if these three doesnt match with the input
  47. # then we will find the closest match
  48. else:
  49. # get close match will return us the list of all
  50. # close words with the input
  51. close_match=get_close_matches(word,data.keys())
  52. # if there are 1 or more elements
  53. if len(close_match)>0:
  54. # now we will print the meaning of closest words
  55. # that is the first element
  56. decide=input("Enter yes or no to move forward: ")
  57. if decide=="yes":
  58. # print the meaning of the closest word
  59. print(f"The closest word to this is {close_match[0]}")
  60. for i in data[close_match[0]]:
  61. print(i)
  62. else:
  63. print("There is no word related to it")
  64. else:
  65. print("Cant Found this word")
  66. word=input("Enter the word: ")
  67. Get_meaning(word)

Output:

输出:

  1. RUN 1:
  2. Enter the word: Abhi
  3. Enter yes or no to move forward: yes
  4. The closest word to this is Abi
  5. ISO 639-6 entity
  6. A Kwa language spoken by the Abé people primarily
  7. in the Department of Agboville in Côte d'Ivoire.
  8. RUN 2:
  9. Enter the word: Hello
  10. 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

下载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 英语词典