dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jul 15, 2021

Python Wonderwords Library: Random Words and Sentences

One of the greatest features of Python is its vast array of community-supported libraries, which extend the functionality and power of the already formidable programming language. Today, we will look at a handy tool that lets Python developers generate random text, in the form of words and sentences, on the fly. Normally we would have to perform a good deal of work to achieve this, but the Wonderwords package does all the heavy lifting for us.

You can find the most recent version of the Wonderword Python package on Github or the stable version at PyPI. You will need to install the package, which you can do using a package manager for the Python Package Index. You can use pip to install Wonderwords. Simply use the command:

pip install wonderwords

You can then test to make sure the install worked by importing Wonderwords. To import, just use the code:

import wonderwords

If you get an error, then you have not properly installed the Wonderwords package.

If you are new to Python packages and do not know how to install them, then I suggest you visit the official Python Packaging Install Packages tutorial page. There is a plethora of information there that can easily guide you through the process, which will come in handy as you develop your Python developer skills.

How to Generate Random Words in Python

Generating random words in Python using the Wonderwords package is fairly straightforward. To generate a single random word, you would use the following code:

 
# import the RandomWord from the WonderWords library
from wonderwords import RandomWord
 
# Assign a random word to a variable
a = RandomWord()
 

The above code should be self-explanatory. We import RandomWords from the WonderWords module, the create a variable named a, which we then assign a random word to using the RandomWord class that is part of the WonderWords package.

Of course, once we run the program, nothing visible happens. The next step would be to actually use the variable. Check out the code below to see the next step:

# import the RandomWord from the WonderWords library
from wonderwords import RandomWord
 
# Assign a random word to a variable
a = RandomWord()
 
 
print(a.word())

Running this will result in a random word being printed to the screen. Note that your instinct may have been to use

print(a)

instead of

print(a.word())

but doing so would result in you printing a reference to a RandomWord object versus an actual randomized word, such as:

<wonderwords.random_word.RandomWord object at 0x0000019FAB9BBEB0>

So always be certain to use

print(a.word())

instead.

Python RandomWord WonderWords Parameters

The RandomWord class has a number of methods you can use outside of the word() method. For example, If you want to generate a word that behings with a certain letter or string of characters, you can use the starts_with() method. Here is how that looks in code:

# import the RandomWord from the WonderWords library
from wonderwords import RandomWord
 
# Assign a random word to a variable
a = RandomWord()
 
 
print(a.word())
 
# Using the starts_with() method
 
print(a.word(starts_with="b"))

In this code, we print a random word, then print a random word that begins with the letter "b." For me, this printed out the result:

gig
beheading

Your results would be different because the words are randomized.

In addition to creating random words that begin with a given character, you can also create words that end with a character, or even combine the two, so that you can choose words that begin andend with certain letters. Here is how that looks in code:

# import the RandomWord from the WonderWords library
from wonderwords import RandomWord
 
# Assign a random word to a variable
a = RandomWord()
 
 
print(a.word())
 
# Using the starts_with() method
 
print(a.word(starts_with="b"))
 
# Using the ends_with() method
 
print(a.word(ends_with="ing"))
 
# Using starts_with() and ends_with() together
 
print(a.word(starts_with="s", ends_with="ing"))

For me, this printed out the following words:

study
borrowing
patrolling
sling

Again, your results will differ because of the randomization of words.

Creating Random Sentences with Wonderwords Library

It is just as simple to create random sentences using Wonderwords. To do so, you will need to import the RandomSentence class from WonderWords, then use the RandomSentence() class. Here is how that looks in code:

 
 
# Import RandomSentence class from Wonderwords
 
from wonderwords import RandomSentence
 
a = RandomSentence()
 
# Creating a barebone sentence
print(a.bare_bone_sentence())
 
# Creating a sentence with a direct object
 
print(a.simple_sentence())
 
# Add an adjective to the sentence
 
print(a.bare_bone_with_adjective())
 
# Random sentence with subject, predicate, adjective, and object
 
print(a.sentence())

The above Python code sample demonstrates ways to create different types of sentence with the WonderWords package. Again, your results will differ from mine when you run this sample code because of randomization, but my results were:

The comb hooks.
The intuition exists comfort.
The cooing creditor lists.
The vast handful invents hammer.
James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date