This function creates a wordcloud of either antonyms, synonyms or both for a given word.
The size of the words in the wordcloud are determined by the strength of the similarity between the given word and its antonym or synonym. The similarity score can be found using the NLTK wordnet package and ranges between 0 and 1 with 1 meaning strong similarity and 0 means weak similarity.
Parameters
Name
Type
Description
Default
word
string
The central word in the wordcloud for which we want to find the synonyms/antonyms.
required
sentence
string
The sentence that contains the word we are interested in.
required
type
string
This determines whether the wordcloud contains antonyms or synonyms or whether the function outputs two wordclouds (one for antonyms and one for synonyms). Either ‘antonym’, ‘synonym’, or ‘both’. Default is ‘both’
'both'
Returns
Name
Type
Description
plt.Figure
A Matplotlib figure of a wordcloud containing antonyms and/or synonyms for a given word.
Raises
Name
Type
Description
LookupError
If Wordnet is not accessible.
TypeError
If word, sentence, or type arguments are not strings.
NameError
If type argument is not ‘synonym’, ‘antonym’, or ‘both’.
Examples
>>> create_wordcloud('car', 'The car is on the road', 'antonym')
>>> create_wordcloud('happy', 'I am glad this trip made you happy')