Python Wonderwords module – A brief Introduction

Featured Img Wonderwords

Hello, there fellow learner! Today we are going to learn about a new less known available in Python known as Wonderwords module!

Introduction to Wonderwords module

Wonderwords is a Python library that contains various functions which are used for generating random words and sentences. Features of the library include the following:

  • Random word and sentence generation in various categories
  • Get your own custom regular expression
  • Comes with an amazing command-line interface to implement the library
  • It is Open source as well!

Implementation of Wonderwords Library

Let’s get right into the implementation of the wonderwords module now.

1. Generating Random Words

Firstly, we need to import the wonderwords library, and to get random words we will import the sub-module RandomWord. And the next step is to create a random word object. The code for the same is shown below.

from wonderwords import RandomWord
R_word_obj = RandomWord()

To generate random word, we are required to use the word function on the random word object created. The code below generates 5 random words with the help of a loop.

for i in range(5):
    x = R_word_obj.word()
    print("Word "+str(i+1)+" : ",x)

The output of the code generates five random words which are shown below.

Word 1 :  irrigation
Word 2 :  porcupine
Word 3 :  lightning
Word 4 :  award
Word 5 :  small

We can also generate words in a particular category or generate words having a particular start or end or even both. Let’s generate all those kinds of words in a single code block.

The code below displays the random words in a different category using the same R_word_obj. The output of the same is displayed right below the code.

print("Words starting with 'w' and end with 'er'")
for i in range(5):
    x = R_word_obj.word(starts_with="w",ends_with="er")
    print("Word "+str(i+1)+" : ",x)
print("\nGenerate random Adjectives")
for i in range(5):
    x = R_word_obj.word(include_parts_of_speech=["adjectives"])
    print("Word "+str(i+1)+" : ",x)

print("\nGenerate random Verbs")
for i in range(5):
    x = R_word_obj.word(include_parts_of_speech=["verbs"])
    print("Word "+str(i+1)+" : ",x)    

print("\nGenerate random words having length between 10 and 20")
for i in range(5):
    x = R_word_obj.word(word_min_length=10,word_max_length=20)
    print("Word "+str(i+1)+" : ",x)
Words starting with 'w' and end with 'er'
Word 1 :  winter
Word 2 :  wrestler
Word 3 :  wafer
Word 4 :  wrestler
Word 5 :  winter

Generate random Adjectives
Word 1 :  beautiful
Word 2 :  orange
Word 3 :  old-fashioned
Word 4 :  ruthless
Word 5 :  lopsided

Generate random Verbs
Word 1 :  enlist
Word 2 :  tickle
Word 3 :  study
Word 4 :  delight
Word 5 :  whine

Generate random words having length between 10 and 20
Word 1 :  sensitivity
Word 2 :  precedence
Word 3 :  recapitulation
Word 4 :  co-producer
Word 5 :  willingness

We can also generate a bunch of words without using the for loop every time by making use of the random_words function and mention the number of words as a parameter. The code for the same is shown below.

l1 = R_word_obj.random_words(10,include_parts_of_speech=["verbs"])
print("Random Verbs: ",l1)
l2 = R_word_obj.random_words(30,include_parts_of_speech=["adjectives"])
print("Random Adjectives: ",l2)
Random Verbs:  ['manipulate', 'dive', 'shave', 'talk', 'design', 'obtain', 'wreck', 'juggle', 'challenge', 'spill']

Random Adjectives:  ['enchanting', 'berserk', 'tight', 'utter', 'staking', 'calm', 'wakeful', 'nostalgic', 'juicy', 'bumpy', 'unbiased', 'shiny', 'small', 'verdant', 'wanting', 'telling', 'famous', 'orange', 'quack', 'absent', 'devilish', 'overconfident', 'boundless', 'faded', 'cloudy', 'goofy', 'encouraging', 'guarded', 'vigorous', 'null']

2. Generate Random Sentences

In order to generate random sentences, we need to import the RandomSentence submodule from the Wonderwords library. Then we create a random sentence object to generate random sentences. The code is displayed below.

from wonderwords import RandomSentence
R_sent_obj = RandomSentence()
for i in range(5):
    x = R_sent_obj.sentence()
    print("Sentence "+str(i+1)+" : ",x)

The code above would generate 5 plain random sentences, the output of which is shown below.

Sentence 1 :  The onerous dogwood twists invoice.
Sentence 2 :  The erect chauvinist kills mail.
Sentence 3 :  The noxious meet ties terminology.
Sentence 4 :  The accurate trail suggests bustle.
Sentence 5 :  The racial theism accomplishes hostel.

We can also generate sentences which includes adjectives using the code below. The output is also displayed along with the code.

print("Generate sentences with adjectives")
for i in range(5):
    x = R_sent_obj.bare_bone_with_adjective()
    print("Sentence "+str(i+1)+" : ",x)
Generate sentences with adjectives
Sentence 1 :  The ritzy sunroom mixes.
Sentence 2 :  The goofy back assembles.
Sentence 3 :  The abusive tiara offends.
Sentence 4 :  The wakeful mix mixes.
Sentence 5 :  The itchy submitter bids.


Congratulations! Today you learned about a brand new library available in Python known as Wonderworld. Stay tuned to learn more! Thank you for reading!