Hello, readers! In this article, we will be focusing on Python Wonderwords module in detail.
So, let us begin! 🙂
Also read: How to generate random quotes in Python?
Python Wonderwords module – Quick Overview
With Natural Language processing, we require different words and sentences to work for the processing of the model at times.
For the same, Python has introduced the Wonderwords module. This module enables us to have words and sentences generated at random.
With the Wonderwords module, we can create and use user-defined and customized regular expressions at random. Not only words and sentences at random, but it can also generate random words belonging to different string categories.
Let us now focus on the features offered by the Wonderwords module.
1. Generation of random words
As we have discussed above, with Wonderwords module, we can generate words.
For the same, initially, we would need to import the module. Post which, we generate an object of the module to be further used by functions.
To generate N number of words at random, we make use of the word() function to run a for loop and generate words at random.
In the below example, we have generated 4 words at random using the word() function and a for loop for iteration over until the N numbers are generated.
We have made use of the RandomWord sub-module from wonderwords module to generate words at random in a customized manner.
from wonderwords import RandomWord rm = RandomWord() for i in range(4): wrd = rm.word() print(wrd)
cycle browsing leash berserk
Now, apart from just generating words at random, we can also generate random words according to some categories such as –
- Words starting, ending with a specific alphabet
- starting/ending with some specific group of alphabets
- Words with specific customized length
- enacting as an adjective
- Words enacting as a verb, etc
In this example, we have created 3 random words that starts with t and ends t. Also, we have generated 2 words starting with d and ending with s.
from wonderwords import RandomWord R_word_obj = RandomWord() print("Words starting with 't' and end with 't'") for i in range(3): op = R_word_obj.word(starts_with="t",ends_with="t") print(op) print("Words starting with 'd' and end with 's'") for i in range(3): op1 = R_word_obj.word(starts_with="d",ends_with="s") print(op1)
Words starting with 't' and end with 't' trot talent transit Words starting with 'd' and end with 's' dynamics dangerous decorous
Example 3:: Generation of random adjectives
In the below example, we have generated three adjectives and verbs at random using include_parts_of_speech attribute within the word() function. As a part of speech, we have passed adjectives as a parameter to the function to generate 3 random adjectives.
from wonderwords import RandomWord R_word_obj = RandomWord() print("3 Random Adjectives") for i in range(3): op = R_word_obj.word(include_parts_of_speech=["adjectives"]) print(op)
dead salty easy
2. Generation of sentences at random
Apart from words, even the sentences can be generated at random using the RandomSentence sub-module within the Wonderwords library.
We follow the same process to import the submodule. Then, we generate an object of the submodule. To which, we call the sentence() function to generate N sentences at random.
In this example, we have generated 3 sentences at random using the sentence() function.
from wonderwords import RandomSentence R_sent_obj = RandomSentence() print("Random Sentences:") for i in range(3): op = R_sent_obj.sentence() print(op)
Random Sentences: The panicky peach breeds plea. The glib shoot distributes hydrant. The wry wrapping crushes collection.
By this, we have come to the end of this topic. Feel free to comment below, in case of any questions.
For more such posts related to Python programming, Stay tuned with us.
Till then, Happy Learning! 🙂