Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Language Translator Using Google API in Python
Next article icon
Every day, we visit so many applications, be it messaging platforms like Messenger, Telegram or ordering products on Amazon, Flipkart, or knowing about weather and the list can go on. And we see that these websites have their own software program for initiating conversations with human beings using rules or artificial intelligence. Users interact with the software program via a conversational interface through written or spoken text. This can be referred to as an assistant.Example:
Input: what is codingOutput: What/If (stylized as WHAT/ IF) is an American thriller web television miniseries, created by Mike Kelley, that premiered on May 24, 2019, on Netflix. The series stars Renée Zellweger, Jane Levy, Blake Jenner, Daniella Pineda, Keith Powers, Samantha Ware, Dave Annable, Saamer Usmani, John Clarence Stewart and Louis Herthum.== Premise ==What/If is a neo-noir thriller that explores "the ripple effects of what happens when acceptable people start doing unacceptable things.User:

What you need to know ?

An assistant can be made using
Natural Language Processing(NLP) one of the most promising fields of artificial intelligence that uses natural languages to enable human interactions with machines.There are two main approaches to NLP:
  1. rule-based methods:That follow some pre specified rules and answer as per those rules.
  2. statistical methods: i.e. methods related to machine learning that learn on their own according to user inputs.
And this article will learn about how to create your own assistant using statistical methods.To create an assistant, we'll use the Python programming language, for it has all the modules required for building it. Secondly, Python is easy to understand, even if you don't have much experience with programming.

Let's get started

Before, getting into the actual code, we need to understand, that this chatbot or assistant will be voice-based, so we need to import the following modules.
  • pyttsx3: A python package that supports common text to speech engines on Mac OS, Windows and Linux.
  • speech_recognition: Library for performing speech recognition, with support for several engines and APIs, like CMU Sphinx, Microsoft Bing Voice Recognition, Google Cloud Speech API etc.
  • wolframalpha: Python's support library for WolframAlpha computational intelligence .
  • wikipedia: Python's library that makes it easy to access and parse data from Wikipedia.
To install the above modules on your system, use the following :
pip install pyttsx3pip install SpeechRecognitionpip install wolframalphapip install wikipedia

Creating a WolframAlpha Account

Since, this chatbot uses WolframAlpha API to find answers, the user must create a free account by signing up at . Follow the steps required to set up a student account that is free and is for personal use only. Generate your app-id and copy it for further reference.

Concepts Followed

  • Speech Recognition: Speech recognition is the ability of a machine or program to identify words and phrases in spoken language and convert them to a machine-readable format.
  • TTS - text to speech: a form of speech synthesis that converts text into spoken voice output.
  • Computational Knowledge Integration: Integrating your bot with computational knowledge intelligence using Wolfram|Alpha.
Below is the implementation.Python3 1==
# Python package supporting common text-to-speech enginesimportpyttsx3# For understanding speechimportspeech_recognitionassr# For fetching the answers# to computational queriesimportwolframalpha# for fetching wikipedia articlesimportwikipedia# Function to search the query# that is either entered or spoken# by userdefsearch(query):# try is used for searching with wolframAlphatry:# Generate your App ID from WolframAlphaapp_id="Your WolframAlpha App ID here"client=wolframalpha.Client(app_id)res=client.query(query)answer=next(res.results).textprint(answer)SpeakText("Your answer is "+answer)# If the query cannot be searched using# WolframAlpha then it is searched in# wikipediaexcept:query=query.split(' ')query=" ".join(query[0:])SpeakText("I am searching for "+query)print(wikipedia.summary(query,sentences=3))SpeakText(wikipedia.summary(query,sentences=3))# Function to convert text to# speechdefSpeakText(command):# Initialize the engineengine=pyttsx3.init()engine.say(command)engine.runAndWait()# Driver's code# input query from the user by# typing or by voicequery=input()query=query.lower()# if query is blank then user# is prompted to speak something.ifquery=='':r=sr.Recognizer()# uses the default microphone# as the source to record voicewithsr.Microphone()assource:print("Say Something ")# reduces the background disturbances# and noise for 2 secondsr.adjust_for_ambient_noise(source,2)# listening to sourceaudio=r.listen(source)try:speech=r.recognize_google(audio)search(speech)# Handling Exceptions if speech# is not understood.exceptsr.UnknownValueError:print("Google Speech Recognition could not\        understand audio")# Couldn't handle requests, occurs# mainly because of network errorsexceptsr.RequestErrorase:print("Could not request results from Google\        Speech Recognition service;{0}".format(e))else:search(query)
Output:python-assistantNote: The voice output of the output text is also generated.

Voice Search Wikipedia Using Python

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp