當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。