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

时间:2019-06-04 19:35:12

标签: python dictionary

通过做项目的一些学习,我试图构建一个简单的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]())

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

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

2 个答案:

答案 0 :(得分:0)

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

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: 

我希望您能找到正确的解决方案。

答案 1 :(得分:0)

这将计算出最佳匹配键:

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]