How to Generate a keyword at random?
To get quotes from different backgrounds, we will generate a random keyword each time, and the program will return a quote from a specific author centered on the keyword.
We use the random_word module to get any random English word. The random_word module can produce a single random word or a list of random words.
Before going to the code, install the random_word module as shown below
pip install random_word
Output:
Collecting random_word Downloading Random_Word-1.0.7-py3-none-any.whl (8.0 kB) Requirement already satisfied: requests in /usr/local/lib/python3.7/ dist-packages (from random_word) (2.23.0) Collecting nose Downloading nose-1.3.7-py3-none-any.whl (154 kB) |████████████████████████████████| 154 kB 8.6 MB/s Requirement already satisfied: urllib3!=1.25.0,!=1.25.1, <1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests-> random_word) (1.24.3) Requirement already satisfied: idna<3,>=2.5 in /usr/ local/lib/python3.7/dist-packages (from requests->random_word) (2.10) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/ dist-packages (from requests->random_word) (2021.10.8) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->random_word) (3.0.4) Installing collected packages: nose, random-word Successfully installed nose-1.3.7 random-word-1.0.7
pip install quote
Output:
Collecting quote Downloading quote-2.0.4.tar.gz (4.1 kB) Collecting gazpacho>=1.0 Downloading gazpacho-1.1.tar.gz (7.9 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Building wheels for collected packages: quote, gazpacho Building wheel for quote (setup.py) ... done Created wheel for quote: filename=quote-2.0.4-py3-none-any. whl size=5005 sha256=2afb78d9fe4304db45fa2462ec1e37f927bf09c39485859da3b0bb3f6b 7be7bc Stored in directory: /root/.cache/pip/wheels/74/8c/7d/2670c2479ed9ff454b 92c11a0459b9f8dcbc461ede57f8a493 Building wheel for gazpacho (PEP 517) ... done Created wheel for gazpacho: filename=gazpacho-1.1-py3-none-any.whl size=7483 sha256=0dd0461047f3c9b88afa32b3c0411b2a29a503006323d10d30c45544683637f6 Stored in directory: /root/.cache/pip/wheels/db/6b/a2/486f272d5e523b56bd19817c14ef35e c1850644dea78f9dd76 Successfully built quote gazpacho Installing collected packages: gazpacho, quote Successfully installed gazpacho-1.1 quote-2.0.4
Program to Generate Random Quotes from quote Module in Python
Method #1: Using random_word Module (Static Input)
Approach:
- Import RandomWords from the random_word module using the import keyword.
- Import quote from the quote module using the import keyword.
- Create an object for the RandomWords() to extract the words and store it in a variable.
- Apply the get_random_word() function on the above object to get a random word and store it in another variable.
- Print the above word obtained.
- Pass the above word obtained and limit = 1 as the arguments to the quote() function to generate a quote at random. Set a limit to limit the number of quotes generated.
- Store it in another variable.
- The quote function provides a collection of dictionaries, each containing information about a certain quote.
- Loop till the length of the above result dictionary using the for loop.
- Inside the loop, print the corresponding quote.
- The Exit of the Program.
Below is the implementation:
# Import RandomWords from the random_word module using the import keyword from random_word import RandomWords # Import quote from the quote module using the import keyword from quote import quote # Create an object for the RandomWords() to extract the words and store it in # a variable. randobj = RandomWords() # Apply the get_random_word() function on the above object to get a random word # and store it in another variable. wrd = randobj.get_random_word() # Print the above word obtained. print("The result Keyword obtained = ", wrd) # Pass the above word obtained and limit = 1 as the arguments to the quote() # function to generate a quote at random. # Set a limit to limit the number of quotes generated. # Store it in another variable. # The quote function provides a collection of dictionaries, each containing # information about a certain quote. rslt = quote(wrd, limit=1) # Loop till the length of the above result dictionary using the for loop. for itr in range(len(rslt)): # Print the corresponding quote. print("The result Quote obtained = ", rslt[itr]['quote'])
Output:
The result Keyword obtained = yeoman The result Quote obtained = The scene I had just witnessed (a couple making love in the ocean) brought back a lot of memories – not of things I had done but of things I had failed to do, wasted hours and frustrated moments and opportunities forever lost because time had eaten so much of my life and I would never get it back. I envied Yeoman and felt sorry for myself at the same time, because I had seen him in a moment that made all my happiness seem dull.