【问题标题】:Dictionary and Input: How to use the user input in the value字典和输入:如何在值中使用用户输入
【发布时间】:2022-01-02 09:58:18
【问题描述】:

我正在开发一种聊天机器人。我想知道是否有办法在字典中使用input 作为value。例如:当输入是“我的名字是 x”时,我想得到答案“Hello x”。 我试过这个:"my name is (.*)": ["Hello ! % 1"],但效果不佳。我想,我需要定义 (.*) 和 %1 但我不知道怎么做。

如果你知道一种方法,请帮助我。谢谢。

words = {"good night": ["nighty night", "good night", "sleep well"],
         "good morning": ["good morning", "wakey-wakey!", "rise and shine!"],
         "hi": ["hello", "hey", "hi"],
         "how are you": ["I am good, thank you", "Pretty good,thank you", "Very well, thanks",
                         "I am doing great thanks", "Better than some, not as good as others",
                         "I am better now, it is good to talk to you"],
         "bye": ["see you later", "bye", "bye-bye", "see you soon", "bye for now", "catch you later"],
         "my name is (.*)": ["Hello ! % 1"]
         }

text_punk = input("text something: ")

greet_words = words.keys() #check if the key words is in input_text.
word_available = [word for word in greet_words if word in text_punk]

if word_available: # if words are available take the first of key.
    punk = random.choice(words[word_available[0]])
    print(punk)
    talk(punk) #this is for pyttsx3
else:
    print("problem!")

【问题讨论】:

  • 输入good时,你不能得到晚安或早安的答案,这正常吗?
  • 尝试使用正则表达式来匹配这样的键:stackoverflow.com/a/21025121/17706960
  • @azro 好吧,我认为是?您应该输入整个键来获取值,不是吗?

标签: python dictionary input chatbot


【解决方案1】:

看起来你打算这样做:

from random import choice
from re import match, sub

text_punk = ''

while 'bye' not in text_punk:
    words = {"good night": ["nighty night", "good night", "sleep well"],
             "good morning": ["good morning", "wakey-wakey!", "rise and shine!"],
             "hi": ["hello", "hey", "hi"],
             "how are you": ["I am good, thank you", "Pretty good,thank you", "Very well, thanks",
                             "I am doing great thanks", "Better than some, not as good as others",
                             "I am better now, it is good to talk to you"],
             "bye": ["see you later", "bye", "bye-bye", "see you soon", "bye for now", "catch you later"],
             "my name is (.*)": ["Hello ! {1}"]
             }

    text_punk = input("text something: ").lower()

    response = 'I did not get that, sorry'

    for expression in words:
        if m := match(expression, text_punk):
            response = choice(words[expression]).format(*(m.group(i) for i in range(len(m.groups())+1)))
            break

    print(response)

使用一些输入运行它:

text something: hi
hello
text something: my name is grismar
Hello ! grismar
text something: bye!
catch you later

当然,这就是魔法发生的地方:

choice(words[expression]).format(*(m.group(i) for i in range(len(m.groups())+1)))

如果expressiontext_punk 中的输入匹配,它会从该字典值中选择一个随机短语。然后,它会尝试使用匹配组格式化所选值(替换 {} 中的值)。

它通过列出所有组来实现这一点,从 0 到使用 range(len(m.groups())+1) 的组有很多。

例如,如果输入为'my name is grismar',则0 组将为'my name is grismar'1 组将为grismar。所以命令归结为:

choice(words['my name is (.*)']).format('my name is grismar', 'grismar')

而且由于choice(words['my name is (.*)'])只能是'Hello ! {1}',所以真的是:

'Hello ! {1}'.format('my name is grismar', 'grismar')

它也适用于多个问候语:

"my name is (.*)": ["Hello {1}!", "Hey {1}, long time no see!"]

你可以有多个匹配:

"my name is ([^\s]+) (.*)": ["Hello {1}!", "Greetings, {2}!", "Mx {2}, first name {1}, OK"]

例子:

text something: my name is jaap van der velde
Hello jaap!
text something: my name is jaap van der velde
Greetings, van der velde!
text something: my name is jaap van der velde
Mx van der velde, first name jaap, OK
text something: 

【讨论】:

  • omg 谢谢@Grismar 的时间和详细的回答,你是最棒的!我真的很感激
  • 不客气,您已经非常接近了 - 仔细查看 .format() 的文档以了解其工作原理和方式。您可以通过在.format() 中使用命名正则表达式匹配和命名替换来使其更加聪明,但我会留给您。
  • 嗨@Grismar,我仍在处理相同的代码。我有一个问题,如果你能帮助我,我将不胜感激。那么,是否有可能做出一种反思?即使答案是 I'm 而不是 I am...,我也想得到答案。当用户输入我很高兴时,我得到了答案,但是当我很高兴时,没有答案......抱歉打扰你了。
  • 最简单的方法当然是分别包含两个答案,但您可以使用像"(?:I'm|I am) happy" : "Glad to hear it."? 这样的正则表达式但是您会在某些时候遇到您选择的方法的限制 - 例如,如果您需要机器人记住更多关于所说的内容,您需要做的不仅仅是匹配表达式并选择随机答案。但这超出了您最初的问题。 (请注意,?: 仅用于不匹配的组,您可以将其省略)
  • 我认为现在将它们全部单独包含在内会更好,更容易......我是一名高中老师,正在尝试为我的学生构建一个聊天机器人,以鼓励他们说英语。 .我是python新手,我为聊天机器人创建了一个GUI,包括语音识别和pyttsx3。如果机器人能记住输入,那就太好了,但我认为这超出了我的想象。感谢您的回答,感谢您为我留出时间@Grismar。我将只添加收缩。
猜你喜欢
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多