python - 如何从 Python 语音识别中提取子字符串
问题描述
我正在尝试从用户所说的内容中提取字符串;但是,我在使用 -re 时遇到了当前解决方案的问题。
我首先尝试创建一个虚拟助手来查找有关用户请求的信息,例如电影。而不是必须第二次运行语音识别来创建一个变量,我想从原始输入中提取它。这就是我的意思:
if "about the movie" in command:
about_the_movie(command)
def about_the_movie(command):
m = re.match(r'(.*) about the movie (.*)', command)
movie = m.group(2)
movie.replace(" ", "+")
url = ('http://www.omdbapi.com/?t=' + movie + '&apikey=********')
r = requests.get(url).json()
我现在遇到的问题是,如果用户说“关于电影‘黑暗骑士’,它会抛出一个AttributeError: 'NoneType' object has no attribute 'group'
. 我理解这是因为那时 m.group(2) 不存在,但我我不确定我能做些什么来解决这个问题。
我还想添加查找单词定义的功能,但是如果用户说“汽车的定义”与“汽车的定义是什么”,则会遇到同样的问题
谢谢!
解决方案
所以我一直在搞砸这个并意识到如果有空格它会抛出那个错误:
m = re.match(r'(.*) about the movie (.*)', command)
对比
m = re.match(r'(.*)about the movie (.*)', command)
我很感激你的回应!
推荐阅读
- javascript - 为特定文件添加一个新行的 Gulp 任务
- elasticsearch - Filebeat 没有将日志发送到 logstash
- c# - 如何在带有 C# 的 .Net 应用程序中使用 Oracle 云基础设施 JAVA SDK 来访问存储
- javascript - 如何禁止用户从 DOM 中手动删除 HTML 元素?
- c# - 如何在 C# 中使用 mongodb changeStream?
- zabbix - Zabbix 外部检查和仪表板日志记录
- python - Python sed 正则表达式匹配
- r - 抓取跨越多行的单元格的维基百科表格
- asp.net-mvc - 显示错误行的 ASP.Net MVC Razor 视图
- testing - loadrunner11 Replay Log中服务器的中文日志显示乱码