如果输入的内容没有完全符合维基百科页面的名称,维基百科图书馆会显示错误
如果我使用语音识别模块发出语音命令来在维基百科中搜索某些内容,如果我没有准确地说出它会显示错误。例如:我说(国防学院)没有维基百科页面如此命名,但(国防学院(印度))是一个页面,所以它显示结果。我想根据我的语音命令搜索最近的页面。这是我的代码:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下必须进行类似的搜索。您正在使用 Wikipedia 软件包,而不是上面标记的 Pywikibot。无论如何,这里是一个如何使用 Pywikibot 完成相似搜索的代码片段:
found
是与给定标题最匹配的 Page 对象。获取其文本: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:
found
is a Page object with closest match of the given title. Get its text: