Solved: how to search on wikipedia with and speak the result

In the world of technology, searching for information on the internet has become an indispensable part of our daily lives. With countless websites providing knowledge on a myriad of topics, Wikipedia is one such platform that serves as a vast encyclopedia of knowledge. The question then arises – how can we effectively search on Wikipedia and have the results spoken out loud? In this article, we will explore the solution to this problem, the step-by-step explanation of the Python code, and delve deeper into the related libraries and functions used.

To solve this issue, we will create a Python script that will take a search query, fetch the relevant information from Wikipedia, and then read out the summary of the result. This will be achieved using the Wikipedia and pyttsx3 libraries. Let’s dive into the step-by-step explanation of the code.

The first step is to install the required libraries, which can be done using pip:

pip install wikipedia
pip install pyttsx3

Wikipedia Library

The Wikipedia library is a Python wrapper for the Wikipedia API. It allows us to extract information and summaries from Wikipedia articles, search for articles, and even translate articles. In our script, we will be using the wikipedia.search() and wikipedia.summary() functions to search for the desired topic and fetch its summary.

Pyttsx3 Library

The pyttsx3 library (short for Python Text-to-Speech version 3) is a library that enables text-to-speech functionality in Python. It is platform-independent and works with both Windows and macOS. This library doesn’t require an internet connection and is compatible with both Python 2 and Python 3. In our script, we will use the pyttsx3.init() and pyttsx3.say() functions to initialize the text-to-speech engine and speak the summary from Wikipedia.

Code Explanation

With the required libraries installed, we can now proceed to write our Python script:

import wikipedia
import pyttsx3

# Initialize the text-to-speech engine
engine = pyttsx3.init()

# Take the search query as input and search on Wikipedia
query = input("Enter the topic to search on Wikipedia: ")
results = wikipedia.search(query)

# Print the search results
print("Search results:")
for result in results:
    print(result)

# Choose the desired result, fetch the summary, and speak it
choice = input("Enter the name of the article you want to get the summary for: ")
summary = wikipedia.summary(choice)
engine.say(summary)
engine.runAndWait()

In the script, we first import the required libraries (wikipedia and pyttsx3) and initialize the text-to-speech engine. We then ask the user for their search query, use the wikipedia.search() function to search for the topic on Wikipedia, and display the results. The user can then choose the desired result, and we fetch the summary using the wikipedia.summary() function. Finally, we use the pyttsx3.say() and pyttsx3.runAndWait() functions to speak the summary.

With this script, you can now search for any topic on Wikipedia and have the summary spoken out loud using Python, Wikipedia library, and pyttsx3. Happy searching!

Related posts:

Leave a Comment