Python中的 PyDictionary 模块
PyDictionary是 Python2 和 Python3 的字典(如英语词典)模块。 PyDictionary 为单词提供以下服务:
- 意义
- 翻译
安装
要安装 PyDictionary,请在终端/命令提示符下运行以下pip
代码:
pip install PyDictionary
入门
现在让我们看看如何使用 PyDictionary 模块。首先我们需要导入模块:
from PyDictionary import PyDictionary
导入模块后,我们需要创建它的一个实例才能使用它:
dict = PyDictionary()
要获取单词的含义,我们需要在meaning()
方法中传递单词。
例子:
from PyDictionary import PyDictionary
dict = PyDictionary()
# meaning of "python"
meaning = dict.meaning("python")
print(meaning)
输出:
{‘Noun’: [‘large Old World boas’, ‘a soothsaying spirit or a person who is possessed by such a spirit’, ‘(Greek mythology’]}
如果一个词既是名词又是动词,则该方法将返回包含键“名词”和“动词”的字典。
例子:
from PyDictionary import PyDictionary
dict = PyDictionary()
# meaning of "test"
meaning = dict.meaning("test")
print(meaning)
输出:
{‘Noun’: [‘trying something to find out about it’, ‘any standardized procedure for measuring sensitivity or memory or intelligence or aptitude or personality etc’, ‘a set of questions or exercises evaluating skill or knowledge’, ‘the act of undergoing testing’, ‘the act of testing something’, ‘a hard outer covering as of some amoebas and sea urchins’], ‘Verb’: [‘put to the test, as for its quality, or give experimental use to’, ‘test or examine for the presence of disease or infection’, “examine someone’s knowledge of something”, ‘show a certain characteristic when tested’, ‘achieve a certain score or rating on a test’, ‘determine the presence or properties of (a substance’, ‘undergo a test’]}
为了得到一个单词的翻译,我们需要在translate()
方法中传递这个单词作为第一个参数,将要翻译成的语言作为第二个参数。语言名称应使用其各自的语言代码。
from PyDictionary import PyDictionary
dict = PyDictionary()
# to translate into German
translation = dict.translate("happy",'de')
print(translation)
输出:
glücklich