如何在我的词典中搜索关键词和短语以执行功能

米洛

通过做项目的一些学习,我试图构建一个简单的AI,以适当的功能响应关键字(例如“日期”)和短语(例如“明天的天气”)。它对于简单的关键字很有用,但似乎找不到一个短语。

我已经尝试过.strip命令,但是它什么也没找到。

from basics_jarvis import *
jarvis_functions = {
    "date": lambda: todays_date(), #These are functions from a different .py
    "datum": lambda: todays_date(),
    "weather": lambda: weather_today(),
    "weather tomorrow": lambda: weather_tomorrow(),
    "tomorrows weather": lambda: weather_tomorrow(),
    "What do you think?": lambda: print("Im not an AI, I dont think")
}
Loop = True
while Loop:
    command = input("Awaiting orders \n")
    for keyword in command.split():     #.strip just breaks the code
        if keyword in jarvis_functions:
            print(jarvis_functions[keyword]())

我正在尝试使程序在完整句子(例如“嘿,明天的天气怎么样?”)中注册关键短语(例如“明天的天气”),并且如果可能的话,还比较关键字和短语,并给予短语优先权,因为合适的词组比仅一个关键字更准确。

这是我第一次在这里发布信息,因此对于我犯的任何错误我深表歉意!我愿意接受任何批评!提前致谢!

数码相机

这将计算出最佳匹配键:

def get_best_key(jarvis_fct, words):
    priority = 0
    best_string = ""
    if len(words) == 0:
        return "", 0

    for i in range(0, len(words)+1):
        phrase = " ".join(str(x) for x in words[0:i])
        new_priority = len(words[0:i])
        if phrase in jarvis_fct and new_priority > priority:
            priority = new_priority
            best_string = phrase

    new_words = words[1:len(words)]
    phrase, new_priority = get_best_key(jarvis_fct, new_words)
    if new_priority > priority:
        priority = new_priority
        best_string = phrase

    return best_string, priority

while True:
    command = input("Awaiting orders \n")

    key = get_best_key(jarvis_functions, command.split()))[0]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章