Last modified: December 14, 2021

How to get the Meaning of a Word in Python

In this article, we'll learn two methods that get the meaning of an English word using python.

The first method is the PyDictionary library.

The second method is the dictionaryapi API.


Let's get started.

Method #1: Get Word meaning using PyDictionary

As I said, PyDictionary is a library which means we need to install it.

PyDictionary installation

To install PyDictionary, choose your preferred command:

Via pip:

pip3 install PyDictionary

via Easy_Install:

easy_install  PyDictionary

How to use PyDictionary

To get the meaning of a word, we need to use the meaning() method.

Syntax:

meaning("word", disable_errors=boolean)

meaning() returns the response as Dict, otherwise print Error with None.

In the following example, we'll use the meaning() method to get the meaning of the word Code.

from PyDictionary import PyDictionary

# Call PyDictionary class
dc = PyDictionary()

# Get meaning of word "Code"
mn = dc.meaning("Code")

# Print Result
print(mn)

Output:

{'Noun': ['a set of rules or principles or laws (especially written ones', 'a coding system used for transmitting messages requiring brevity or secrecy', '(computer science'], 'Verb': ['attach a code to', 'convert ordinary language into code']}

Let's try a word that does not exist to see what will happen.

# Get meaning of word "Codexx"
mn = dc.meaning("Codexx")

# Print Result
print(mn)

Output:

Error: The Following Error occured: list index out of range
None

If you want to hide the error, add disable_errors=True into the parameter:


# Get meaning of word "Codexx"
mn = dc.meaning("Codexx", disable_errors=True)

# Print Result
print(mn)

Output:


None

How to use PyDictionary with multiple words

If you have multiple words, you need to use the getMeanings() method.

Syntax:

PyDictionary("list_of_words").getMeanings()

Let's see an example:

from PyDictionary import PyDictionary

# Words
my_words = ['cat', 'go', 'likewise', 'watch']

dc = PyDictionary(my_words)

# Get meaning of muli words
res = dc.getMeanings()

# Print result
print(res)

Output:

{'cat': {'Noun': ['feline mammal usually having thick soft fur and no ability to roar: domestic cats; wildcats', 'an informal term for a youth or man', 'a spiteful woman gossip', 'the leaves of the shrub Catha edulis which are chewed like tobacco or used to make tea; has the effect of a euphoric stimulant', 'a whip with nine knotted cords', 'a large tracked vehicle that is propelled by two endless metal belts; frequently used for moving earth in construction and farm work', 'any of several large cats typically able to roar and living in the wild', 'a method of examining body organs by scanning them with X rays and using a computer to construct a series of cross-sectional scans along a single axis'], 'Verb': ["beat with a cat-o'-nine-tails", 'eject the contents of the stomach through the mouth']}, 'go': {'Noun': ['a time period for working (after which you will be relieved by someone else', 'street names for methylenedioxymethamphetamine', 'a usually brief attempt', "a board game for two players who place counters on a grid; the object is to surround and so capture the opponent's counters"], 'Verb': ['change location; move, travel, or proceed, also metaphorically', 'follow a procedure or take a course', 'move away from a place into another direction', 'enter or assume a certain state or condition', 'be awarded; be allotted', 'have a particular form', 'stretch out over a distance, space, time, or scope; run or extend between two points or beyond a certain point', 'follow a certain course', 'be abolished or discarded', 'be or continue to be in a certain condition', 'make a certain noise or sound', 'perform as expected when applied', 'to be spent or finished', 'progress by being changed', 'continue to live and avoid dying', 'pass, fare, or elapse; of a certain state of affairs or action', 'pass from physical life and lose all bodily attributes and functions necessary to sustain life', 'be in the right place or situation', 'be ranked or compare', 'begin or set in motion', "have a turn; make one's move in a game", 'be contained in', 'be sounded, played, or expressed', 'blend or harmonize', 'lead, extend, or afford access', 'be the right size or shape; fit correctly or as desired', "go through in search of something; search through someone's belongings in an unauthorized way", 'be spent', 'give support (to', 'stop operating or functioning'], 'Adjective': ['functioning correctly and ready for action']}, 'likewise': {'Adverb': ['in like or similar manner', 'in addition', 'equally']}, 'watch': {'Noun': ['a small portable timepiece', 'a period of time (4 or 2 hours', 'a purposeful surveillance to guard or observe', 'the period during which someone (especially a guard', 'a person employed to keep watch for some anticipated event', 'the rite of staying awake for devotional purposes (especially on the eve of a religious festival'], 'Verb': ['look attentively', 'follow with the eyes or the mind', 'see or watch', 'observe with attention', 'be vigilant, be on the lookout or be careful', 'observe or determine by looking', 'find out, learn, or determine with certainty, usually by making an inquiry or other effort']}}

Method #2: Get Word meaning using dictionaryapi API

dictionaryapi API is a free API that provides us the meaning, audio, synonyms, antonyms, and example of an English words.

However, dictionaryapi API returns the response as JSON. and, we'll use the requests module to make a request.

dictionaryapi API Usage

To get the meaning of a word, we'll send a get request to https://api.dictionaryapi.dev/api/v2/entries/en/<word> URL.


In the following example, we'll get information about the word javascript:

import requests

# Word
my_word = "javascript"

# Get Info
req = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{my_word}")

# Print result
print(req.text)

Output:

[{"word":"JavaScript","phonetic":"ˈdʒɑːvəˌskrɪpt","phonetics":[{"text":"ˈdʒɑːvəˌskrɪpt","audio":"//ssl.gstatic.com/dictionary/static/sounds/20200429/javascript--1_gb_1.mp3"}],"origin":"1990s: from Java2 + script1.","meanings":[{"partOfSpeech":"noun","definitions":[{"definition":"an object-oriented computer programming language commonly used to create interactive effects within web browsers.","synonyms":[],"antonyms":[]}]}]}]

As you can see, we got some info like:

  • Audio URL
  • Meanings
  • Phonetic

If you got the ModuleNotFoundError: No module named 'requests' error, follow this article:
Solution: ModuleNotFoundError: No module named 'requests'

 

Happy codding!