Python’s PyDictionary Module

Python’s PyDictionary module/package can be used for getting meaning, translation, synonyms and antonyms of words in English language. Do not that PyDictionary is a Module while Dictionary is just a Object Type. Let’s see How does PyDictionary Module works?
Under the hood functions inside PyDictionary make API requests to different websites passing English Word as parameter.

  • For getting meaning of a word PyDictionary send API request to wordnet.princeton.edu which is a database of words and their meanings, this is developed by Princeton University
  • For doing translation of English Words to other languages, PyDictionary send API request to Google’s Translator and show whatever is retuned by it
  • For getting synonyms/antonyms of a Word, PyDictionary send API request to synonyms.com which is a database of words and their respective synonyms or antonyms

Let’s use PyDictionary Module for getting information about some English words

Setup/Install PyDictionary Module

Install PyDictionary Module using python3 -m pip install PyDictionary on terminal/command line. Entering this will take a few sec and download/install PyDictionary Module. On terminal on MAC this process of installing will like.

Installing PyDictionary Module

Get Meaning of a Word using Python

For getting definition/meaning of word PyDictionary Module can be used.

  • From PyDictionary import PyDictionary() Class using from PyDictionary import PyDictionary
  • Call meaning(parameter) function of PyDictionary() Class passing English word as parameter

Let’s put together all of these steps to Get Meaning of Word

# Getting meaning of a word using Python
from PyDictionary import PyDictionary
dictionary = PyDictionary()
word = "Programming"                  # Word whose meaning you want to find using Python
print(dictionary.meaning(word))
# Below is output of this code
{
'Noun':['setting an order and time for planned events', 
         'creating a sequence of instructions to enable the computer to do something'], 
'Verb':['arrange a program of or for', 'write a computer program', 
        'write a computer program', 'arrange a program of or for']
}

Translating a Word using Python

PyDictionary Module can be used for translating Words into different languages, under the hood PyDictionary translate(word, language Code) function send request to Google Translate API passing language Code parameter stating which language word need to be converted.
Below is Python Code for doing translation of Words using Python Programming Language.

# Getting meaning of a word using Python
from PyDictionary import PyDictionary
dictionary = PyDictionary()
word = "Programming"                  # Word which you want to convert to other language

word_in_hindi = dictionary.translate(word, "hi")    # Language Code for Hindi is hi
word_in_serbian = dictionary.translate(word, "sr")  # Language Code for Serbian is sr
word_in_french = dictionary.translate(word, "fi")   # Language Code for French fi

print(word, " in hindi is => " + word_in_hindi)
print(word, " in serbian is => " + word_in_serbian)
print(word, " in French is => " + word_in_french)

Above Python Code will print out =>

Programming  in hindi is => प्रोग्रामिंग
Programming  in serbian is => Програмирање
Programming  in French is => Ohjelmointi

For know what would be language code for a specific language, please see this article Language Name <=> Code table put together by Google

Getting Synonyms of a Word using Python

Python’s PyDictionary Module can be used for getting a list of synonyms of a word, just pass word to synonym() function and it will return a list containing all of synonyms of word.

Just note that its synonym() not synonyms(), having synonym() will return list of all synonyms of word rather using function synonyms() will return a AttributeError: ‘PyDictionary’ object has no attribute ‘synonyms’.

Below is Python Code for finding out what are synonyms of a Word =>

# Getting meaning of a word using Python
from PyDictionary import PyDictionary
dictionary = PyDictionary()
word = "Computer"                  # Word whose synonym you want to find using Python

list_of_synonyms = dictionary.synonym(word)   # Pass word to synonym function as a parameter
print(list_of_synonyms)

Above Python Code will print out, a list of synonyms of word “Computer” =>

['digital computer', 'busbar', 'website', 'number cruncher', 'home computer', 
'totaliser', 'totalizator', 'microprocessor chip', 'computer circuit', 'micro chip', 
'information processing system', 'monitor', 'totalizer', 'computer memory', 'chip', 
'totalisator', 'Turing machine', 'peripheral', 'web site', 'memory board', 'server', 
'processor', 'central processor', 'floppy disk', 'analog computer', 'computer hardware', 
'data processor', 'diskette', 'C.P.U.', 'mainframe', 'data converter', 'internet site', 
'storage', 'microchip', 'electronic computer', 'disk cache', 'machine', 
'central processing unit', 'monitoring device', 'guest', 'CRT', 'platform', 
'floppy', 'node', 'cathode-ray tube', 'host', 'computer storage', 'predictor', 
'store', 'peripheral device', 'silicon chip', 'computing machine', 
'pari-mutuel machine', 'client', 'hardware', 'memory', 'keyboard', 
'site', 'CPU', 'computing device', 'computer accessory', 'bus', 
'analogue computer', 'computer peripheral']

Getting Antonyms of a Word using Python

Similar to finding out Synonyms of a Word, Python can also be used for finding antonyms of a Word using PyDictionary Module. Just pass word as parameter to antonym() function and this will return a list of antonyms for word.

Just note that its antonym() not antonyms(), having antonym() will return list of all antonyms of word rather using function antonyms() will return a AttributeError: ‘PyDictionary’ object has no attribute ‘antonyms’.

Below is Python Code for finding out what are antonyms of a Word =>

# Getting meaning of a word using Python
from PyDictionary import PyDictionary
dictionary = PyDictionary()
word = "night"                  # Word whose antonym you want to find using Python

list_of_synonyms = dictionary.antonym(word)   # Pass word to antonym function as a parameter
print(list_of_antonyms)

Above Python Code will print out, a list of antonyms of word “night” =>

['day', 'comprehensible', 'blond', 'enlightenment', 'enlightened']

Final Thoughts

PyDictionary is a quite useful Python module for doing a lot of stuff with English Language Words. Specifically I’ve used this in building an APP for showing text in different languages for users accessing the app from different language speaking countries. I think that writing an article about that would be a great, probably I should find some time to write it down here on ComputerScienceHub.
Anyway, If your interested in know about internals/source code of PyDictionary then check out its Github Repo -> PyDictionary Github Repo.

Happy Coding 🥳 🥳

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts