You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3.9调用wikipedia库部分人名无法匹配对应页面问题求解

故障根因
  • 调用wikipedia.summary时设置了auto_suggest=False,关闭了自动近似匹配功能。语音识别返回的查询词「Robert Downey jr.」和维基百科官方条目标准标题「Robert Downey Jr.」大小写、格式不完全一致,无法直接命中条目触发报错。
  • 代码没有针对维基查询的异常场景做捕获处理,查询失败后程序直接终止运行。
修复方案

修改main函数内的维基查询逻辑,开启自动匹配,增加异常捕获即可,修改后的代码如下:

def main():
    you = inputCommand().lower()
    if "wikipedia" in you:
        you = you.replace("wikipedia", "")
        Jarvis_brain("what topic you need to listen to")
        topic=inputCommand()
        try:
            # 开启auto_suggest自动匹配近似标题,提高命中概率
            results = wikipedia.summary(topic,sentences=3, auto_suggest=True, redirect=True)
            print(results)
            Jarvis_brain(results)
        # 捕获条目不存在的错误
        except wikipedia.exceptions.PageError:
            Jarvis_brain("抱歉,没有找到对应的维基条目,请换个关键词重试")
        # 捕获一词多义的消歧义错误
        except wikipedia.exceptions.DisambiguationError:
            Jarvis_brain("该关键词对应多个条目,请补充信息后重试")
    else:
        print("I can't hear you, please try again")
额外优化建议

如果对匹配精度要求更高,可以先调用wikipedia.search(topic, results=1)拿到搜索结果第一位的标准标题,再用这个标题查询摘要,能进一步降低匹配失败的概率。

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 21:30:03