Every day, we interact with numerous applications, from messaging platforms like Messenger and Telegram to shopping on Amazon and Flipkart, or checking the weather, and more. These websites often use software programs to initiate conversations with users, either through rules or artificial intelligence. Users engage with these programs via a conversational interface using written or spoken text, commonly known as an assistant.
What do you need to know?
Creating an assistant can be achieved using Natural Language Processing (NLP), a rapidly evolving field of artificial intelligence that enables machines to understand and interact with human language.
NLP primarily employs two approaches:
- Rule-based methods: These follow predefined rules to generate responses.
- Statistical methods: These involve machine learning techniques that adapt and improve based on user interactions.
This article focuses on building an assistant using statistical methods.
To develop this assistant, we’ll use Python due to its rich set of libraries and ease of use, even for those with limited programming experience.
Let’s get started Before diving into the code, note that this assistant will be voice-based, requiring the following Python modules:
- pyttsx3: A text-to-speech conversion library that works across Mac OS, Windows, and Linux.
- wikipedia: A library for retrieving and parsing data from Wikipedia.
To install these modules, use the following command:
pip install pyttsx3
pip install wikipedia
Below is the implementation.
import pyttsx3
import wikipedia
import speech_recognition as sr
def search_wikipedia(query):
try:
# Initialize the text-to-speech engine
voice = pyttsx3.init()
# Fetch the summary of the search query from Wikipedia
result = wikipedia.summary(query, sentences=3)
# Print the result
print(result)
# Use the text-to-speech engine to speak the result
voice.say(result)
voice.runAndWait()
except wikipedia.exceptions.DisambiguationError as e:
# Handle disambiguation error
print("The search query is too ambiguous. Please provide a more specific search query.")
voice.say("The search query is too ambiguous. Please provide a more specific search query.")
voice.runAndWait()
except wikipedia.exceptions.PageError as e:
# Handle page error
print("The page you requested does not exist.")
voice.say("The page you requested does not exist.")
voice.runAndWait()
except Exception as e:
# Handle any other exceptions
print("An error occurred while searching Wikipedia.")
voice.say("An error occurred while searching Wikipedia.")
voice.runAndWait()
def recognize_speech():
# Initialize recognizer class (for recognizing the speech)
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Please say something to search on Wikipedia...")
# Adjust the recognizer sensitivity to ambient noise
recognizer.adjust_for_ambient_noise(source, duration=2)
# Listen to the user's input
audio = recognizer.listen(source)
try:
# Recognize speech using Google Web Speech API
query = recognizer.recognize_google(audio)
print("You said: " + query)
return query
except sr.UnknownValueError:
print("Sorry, I did not understand the audio.")
return None
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
return None
def main():
query = recognize_speech()
if query:
search_wikipedia(query)
else:
print("No valid input received.")
if __name__ == "__main__":
main()
Output:

Explanation of the above code:
import pyttsx3
import wikipedia
import speech_recognition as sr
These lines import the necessary libraries for text-to-speech conversion, fetching Wikipedia articles, and recognizing speech input.
def search_wikipedia(query):
This function takes a query, searches Wikipedia, and uses text-to-speech to read the summary.
def recognize_speech():
This function uses the speech_recognition library to capture and recognize speech input.
def main():
This function orchestrates capturing the voice input and searching Wikipedia.
if __name__ == "__main__":
main()
This ensures the main() function is called when the script is executed.
Note: Speak into the microphone when prompted. The program will recognize your speech, search for the query on Wikipedia, and read the summary aloud.
Code Without Voice Assistant For Searching Through Wiki/Google
import pyttsx3
import wikipedia
def search_wikipedia():
try:
voice = pyttsx3.init()
search_query = input("Searching wikipedia/google: ")
result = wikipedia.summary(search_query)
print(result)
voice.say(result)
voice.runAndWait()
except wikipedia.exceptions.DisambiguationError as e:
print("Please provide a more specific search query.")
except wikipedia.exceptions.PageError as e:
print("The page you requested does not exist.")
except Exception as e:
print("An error occurred while searching Wikipedia.")
search_wikipedia()
Output:
Explanation of the Code:
import pyttsx3
This line imports the pyttsx3 library, which is used for text-to-speech conversion. This library allows the program to convert text into spoken words.
import wikipedia
This line imports the wikipedia library, which allows the program to fetch information from Wikipedia. It can be used to search for and summarize Wikipedia articles.
def search_wikipedia():
This line defines a function named search_wikipedia. Functions in Python are blocks of reusable code that perform a specific task when called.
try:
This line starts a try block. The try block lets you test a block of code for errors. If an error occurs, it is handled by the corresponding except blocks.
voice = pyttsx3.init()
This line initializes the pyttsx3 text-to-speech engine and assigns it to the variable voice. The init() method sets up the text-to-speech engine.
search_query = input("Searching wikipedia/google: ")
This line prompts the user to enter a search query. The user’s input is stored in the variable search_query.
result = wikipedia.summary(search_query)
This line searches Wikipedia for a summary of the article matching the user’s query. The summary function fetches a brief summary of the article. The result is stored in the variable result.
print(result)
This line prints the summary fetched from Wikipedia to the console.
voice.say(result)
This line converts the text summary into speech using the pyttsx3 engine. The say method adds the text to the speech queue.
voice.runAndWait()
This line processes the speech queue and waits until all the speech has been spoken. The runAndWait method blocks the program until the speaking is finished.
except wikipedia.exceptions.DisambiguationError as e:
This line starts an except block to handle DisambiguationError exceptions. This error occurs when Wikipedia finds multiple articles matching the search query and needs a more specific query.
print("Please provide a more specific search query.")
If a DisambiguationError occurs, this line prints a message asking the user to provide a more specific search query.
except wikipedia.exceptions.PageError as e:
This line starts another except block to handle PageError exceptions. This error occurs when the requested Wikipedia page does not exist.
print("The page you requested does not exist.")
If a PageError occurs, this line prints a message indicating that the requested page does not exist.
except Exception as e:
This line starts a general except block to catch any other exceptions that might occur. The variable e contains information about the exception.
print("An error occurred while searching Wikipedia.")
If any other error occurs, this line prints a generic error message.
search_wikipedia()
This line calls the search_wikipedia function, which executes the code inside the function. The program prompts the user for a search query, fetches the summary from Wikipedia, and reads it aloud using text-to-speech.

Conclusion
Creating a Wikipedia Voice Assistant using Python demonstrates the power and versatility of integrating multiple libraries to create a cohesive and functional application. In this project, we leveraged the capabilities of pyttsx3 for text-to-speech conversion and the wikipedia library to fetch and summarize articles. This combination allows us to build an assistant that not only searches for information but also reads it aloud, enhancing accessibility and user experience.
The process began with setting up the necessary libraries, which are easy to install and use thanks to Python’s rich ecosystem. The core functionality was implemented through a straightforward function that handles user input, searches Wikipedia for the relevant article, and then uses text-to-speech to read the summary aloud. By including error handling for common issues such as ambiguous search terms or missing articles, we ensured that the application is robust and user-friendly.
Key Steps in Creating the Wikipedia Voice Assistant
- Importing Libraries: We imported
pyttsx3for text-to-speech conversion andwikipediafor fetching and summarizing Wikipedia articles. These libraries are essential for the core functionality of the assistant. - Defining the Function: We defined the
search_wikipediafunction, which encapsulates the main logic of the assistant. This function prompts the user for a search query, fetches the summary of the article, and reads it aloud. - Handling User Input: We used the
input()function to get the search query from the user. This makes the assistant interactive and user-driven. - Fetching Wikipedia Summary: The
wikipedia.summary()function was used to fetch a brief summary of the article. This provides concise and relevant information to the user. - Text-to-Speech Conversion: We used
pyttsx3to convert the text summary into speech. Thesay()andrunAndWait()methods ofpyttsx3were used to add the text to the speech queue and process it. - Error Handling: We included specific error handling for
DisambiguationErrorandPageErrorto manage common issues that might arise during the search process. Additionally, a general exception handler ensures that unexpected errors are caught and reported.
Benefits of the Wikipedia Voice Assistant
- Accessibility: By converting text into speech, the assistant makes information accessible to users who may have visual impairments or prefer auditory learning.
- Convenience: Users can quickly get information on various topics without needing to read through long articles. This is particularly useful for multitasking or for users on the go.
- Educational Tool: The assistant can be a valuable educational tool, helping students and learners quickly access information and learn new topics through auditory input.
- Customization: The project can be easily customized and extended. For instance, you could add support for more languages, integrate with other APIs, or improve the speech recognition capabilities.
Future Enhancements
To further enhance the functionality and user experience of the Wikipedia Voice Assistant, several improvements can be considered:
- Voice Recognition: Integrating a speech recognition library like
speech_recognitionto allow voice input instead of typing. - Improved Error Handling: Adding more detailed error messages and suggestions for better search queries.
- User Preferences: Allowing users to customize the voice, speed, and volume of the text-to-speech output.
- Integration with Other APIs: Combining the assistant with other APIs (e.g., news, weather) to provide a broader range of information.
In conclusion, building a Wikipedia Voice Assistant in Python is a rewarding project that showcases the practical applications of natural language processing and text-to-speech technologies. By following the steps outlined in this guide, you can create a useful tool that enhances information accessibility and user interaction.
4o





Leave a Reply