如何在我的字典中搜索关键字和短语以执行函数

2024-04-29 02:42:51 发布

您现在位置:Python中文网/ 问答频道 /正文

作为一个“边做边学”项目,我正在尝试构建一个简单的人工智能,它通过适当的功能响应关键字(如“日期”)和短语(如“明天天气”)。它对简单的关键词很有用,但似乎找不到短语

我已经试过了。去掉这个命令,但是没有找到任何东西

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]())

我试图让程序在一个完整的句子中注册一个关键短语(例如“明天的天气”),例如“嘿,明天的天气怎么样?”如果可能的话,还可以比较关键词和短语,并给短语优先级,因为一个合适的短语比一个关键词更准确

这是我第一次在这里发帖,所以我为我犯的任何错误道歉!我愿意接受任何形式的批评!提前谢谢


Tags: lambdafromloopdatefunctions关键词keywordcommand
2条回答

我在您当前的代码中添加了一些打印语句来说明这个问题:

while True:
    command = input("\nAwaiting orders: ")
    print('received command:', repr(command))

    for keyword in command.split():
        print('   keyword', repr(keyword))
        if keyword in jarvis_functions:
            print(jarvis_functions[keyword]())

结果输出为:

Awaiting orders: hey, whats tomorrows weather
received command: 'hey, whats tomorrows weather'
   keyword 'hey,'
   keyword 'whats'
   keyword 'tomorrows'
   keyword 'weather'

Awaiting orders:

如您所见,该命令被拆开,tomorrowsweather不再在一起


相反,我建议迭代这些关键字,看看它们是否出现在命令中。也许是这样:

jarvis_functions = {
    "tomorrows weather": lambda: print('1'),
    "What do you think?": lambda: print("Im not an AI, I dont think"),
}

while True:
    command = input("\nAwaiting orders: ")
    print('received command:', repr(command))

    for keyword, func in jarvis_functions.items():
        print('   keyword', repr(keyword))

        if keyword in command:
            print('   keyword was found')
            func()

            # no need to check other keywords
            break

产出将是:

Awaiting orders: hey, whats tomorrows weather
received command: 'hey, whats tomorrows weather'
   keyword 'tomorrows weather'
   keyword was found
1

Awaiting orders: something new
received command: 'something new'
   keyword 'tomorrows weather'
   keyword 'What do you think?'

Awaiting orders: 

我希望这能让你找到正确的解决办法

这将计算最佳匹配密钥:

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]

相关问题 更多 >