如果输入的内容没有完全符合维基百科页面的名称,维基百科图书馆会显示错误

发布于 2025-01-12 09:23:09 字数 1182 浏览 0 评论 0原文

如果我使用语音识别模块发出语音命令来在维基百科中搜索某些内容,如果我没有准确地说出它会显示错误。例如:我说(国防学院)没有维基百科页面如此命名,但(国防学院(印度))是一个页面,所以它显示结果。我想根据我的语音命令搜索最近的页面。这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2025-01-19 09:23:09

在这种情况下必须进行类似的搜索。您正在使用 Wikipedia 软件包,而不是上面标记的 Pywikibot。无论如何,这里是一个如何使用 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 是与给定标题最匹配的 Page 对象。获取其文本:

  >>> found.text[:100]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文