If I give a voice command using speech recognition module to search something in Wikipedia if I don't say exactly it shows an error. For example: I say(National Defence Academy) no Wikipedia page is named so but(National Defence Academy (India)) is a page, so it shows results. I want to search for the nearest page as per my voice command. Here is my code:
import pyttsx3
import datetime
import speech_recognition as sr
import Wikipedia
import web-browser
import os
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def voiceinput():
r=sr.Recognizer()
with sr.Microphone() as source:
print("I am listening")
r.pause_threshold=1
audio=r.listen(source)
try:
print("Recognizing...")
speak("Recognizing...")
query=r.recognize_google(audio, language="en-in")
print(f"You mean {query}\n")
speak(f"You mean {query}\n")
except Exception as e:
print("Please repeat")
speak("please repeat?")
return "None"
return query
if "search" in query:
speak("Searching Wikipedia")
query=query.replace("search", "")
results=wikipedia.summary(query, sentences=2)
print("According to Wikipedia")
print(results)
speak("According to Wikipedia")
speak(results)
A similar search has to be made in this case. You are using the Wikipedia package and not Pywikibot as tagged above. Anyway here is a code snippet how a similar search can be done with Pywikibot:
>>> from difflib import get_close_matches
>>> import pywikibot
>>> site = pywikibot.Site('wikipedia:en') # create a Site object
>>> title = 'National Defence Academy'
>>> gen = site.search('intitle:' + title, total=10, namespaces=0)
>>> titles = [page.title() for page in gen] # list of strings required
>>> result = get_close_matches('National Defence Academy', titles)
>>> found = pywikibot.Page(site, result[0])
found
is a Page object with closest match of the given title. Get its text:
>>> found.text[:100]