当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python PyDictionary用法及代码示例


PyDictionary 是 Python2 和 Python3 的字典(如英语词典)模块。 PyDictionary为单词提供以下服务:

  • meanings
  • translations

安装

要安装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’]}

如果一个单词既是名词又是动词,那么该方法将返回一个包含键“Noun”和“Verb”的字典。

例子:


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


相关用法


注:本文由纯净天空筛选整理自Yash_R大神的英文原创作品 PyDictionary module in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。