Want to know how you can create random combinations of words and sentences with ease? Also, with customization at the same time. The answer to it is by using the Wonderwords library. In this article, we will learn about the given module, its features, and basic utilization. Let’s begin.
Introduction to Wonderwords
Wonderwords is a Python package that simplifies the generation of random English words and structured sentences. It offers effortless word and sentence creation, catering to various applications such as game development, creative writing, and linguistic research. This versatile tool is valuable for users seeking random content generation in Python projects. It can be employed in bots and robots.
Key Features
Some of the specialities of this Wonderwords API are:
- Wonderwords can generate random words.
- We can specify word length, starting and ending characters, categories, and custom regular expressions.
- It allows us to use custom word lists and define custom categories of words
- It offers a user-friendly interface and thorough documentation.
- It features a sleek command line interface and is open source.
Installation
To install the latest version of Wonderwords, run the following command:
pip install wonderwords
To upgrade Wonderwords using pip, use:
pip install --upgrade wonderwords
Usage of the Wonderwords Python Module
Now that we have covered the installation and understand what Wonderwords is, this section will briefly describe its usage.
Using the RandomWord Class & word Method
Here are some examples demonstrating how we can utilize the RandomWord class within the API, using the word() method.
Example 1: To generate a random word.
from wonderwords import RandomWord
data = RandomWord().word()
print(data)
Output: Here the randomly generated word is “methodology“.
Example 2: To generate a random word that starts with s and ends with ng.
from wonderwords import RandomWord
data = RandomWord().word(starts_with="s", ends_with="ng")
print(data)
Output: Here the randomly generated word is “shipping“.
Example 3: To generate a random noun or verb, all parts of speech are included by default.
from wonderwords import RandomWord
data = RandomWord().word(include_parts_of_speech=["nouns", "verbs"])
print(data)
Output: Here the randomly generated word is “fling“.
Example 4: To generate a random word with 4 to 6 characters.
from wonderwords import RandomWord
data = RandomWord().word(word_min_length=2, word_max_length=6)
print(data)
Output: Here, the randomly generated word is “grip,” which has 5 characters, falling within the range of 2 to 6.
Example 5: It also lets you combine multiple filtering options.
from wonderwords import RandomWord
data = RandomWord().word(starts_with="sp", word_max_length=6, include_parts_of_speech=["nouns"])
print(data)
Output: Here, the randomly generated word “spiral” meets all three specified conditions.
Using the filter Method
With the filter method in Python, you can easily create a list of words that fit certain rules. This tool lets you gather and organize data just the way you need it. Its working is pretty similar to the previous method. Below are some of its usage examples.
Example 1: To get a list of all words starting with “gr“.
from wonderwords import RandomWord
data = RandomWord().filter(starts_with="gr")
print(data)
Output: Here, we are provided with a list of all string elements that start with “gr“
Example 2: Using all available options from the word method.
from wonderwords import RandomWord
data = RandomWord().filter(ends_with="u", include_parts_of_speech=["nouns", "adjectives"], word_min_length=5)
print(data)
Output: Here, the randomly generated list of words meets all three specified conditions.
Using the random_words Method
The random_words method helps create a random list of words, similar to a filter. Here, you specify how many words you want, and they’re picked randomly. If there aren’t enough words, it raises a NoWordsToChooseFrom exception. Below are some examples of the same.
Example 1: To get a list of 5 random verbs.
from wonderwords import RandomWord
data = RandomWord().random_words(5, include_parts_of_speech=["verbs"])
print(data)
Output: A list of 5 randomly generated verbs is displayed.
Example 2: We can use all the options found in the word method.
from wonderwords import RandomWord
data = RandomWord().random_words(7, starts_with="sc", word_min_length=6)
print(data)
Output: A list of 7 randomly generated words that satisfy all the given conditions.
Example 3: Example of an NoWordsToChooseFrom exception case.
from wonderwords import RandomWord
data = RandomWord().random_words(50, starts_with="z", word_min_length=8)
print(data)
Output: The number of words we want to retrieve is larger than the available words, hence, a NoWordsToChooseFrom exception is raised.
Solution: You can silence the NoWordsToChooseFrom exception and return all words, even if there are fewer, by setting return_less_if_necessary to True.
from wonderwords import RandomWord
data = RandomWord().random_words(50, starts_with="z", word_min_length=8,return_less_if_necessary=True)
print(data)
Output:
Using the RandomSentence Class
We can generate random sentences with ease using the RandomSentence class. Here are some examples to demonstrate the same.
Example 1: To get a random bare-bone sentence.
from wonderwords import RandomSentence
data = RandomSentence().bare_bone_sentence()
print(data)
Output: It prints a random sentence.
Example 2: To get a random sentence with a direct object
from wonderwords import RandomSentence
data = RandomSentence().simple_sentence()
print(data)
Output: It prints a simple sentence.
Example 3: To get a random bare-bone sentence with an adjective.
from wonderwords import RandomSentence
data = RandomSentence().bare_bone_with_adjective()
print(data)
Output: It prints a simple sentence with the use of an adjective.
Example 4: To get a random sentence with a subject, predicate, direct object and adjective.
from wonderwords import RandomSentence
data = RandomSentence().sentence()
print(data)
Output: It prints a complex sentence.
Conclusion
So that’s it for this article. I hope you had fun creating random words and sentences using the WonderWords library. This module can be really useful when creating puzzles and random content.
If you enjoyed reading this, continue reading –
Reference
https://pypi.org/project/wonderwords/