我正在创建一个小型虚拟助手项目,该项目使用语音识别来帮助我使用Python获得更高级的功能。我现在已经能够获得给定城市的天气,或者当前位置,还可以使用newsapi.org进行简单的算术(加法,减法等)和获取新闻。
我想知道如何使用if语句加快识别关键字的过程。例如,现在我有这样的事情:
if "weather" in voiceRgn and "here" in voiceRgn or "weather" in voiceRgn:
#current location weather function
elif "weather" in voiceRgn and "in" in voiceRgn and "current" not in voiceRgn and "my" not in voiceRgn:
# given city/place weather function
elif "articles" in voiceRgn or "news" in voiceRgn or "headlines" in voiceRgn:
# list news headlines function
正如您所看到的那样,它很长并且可能效率低下。有没有办法缩短这个,所以也许它给出了一个单词列表,如果剪辑包含单词列表中的内容,它会运行函数吗?