如何在 python nltk 聊天答案上使用反射

How to use reflections on python nltk chat answers

NLTK chat.utils module,参数之一是“反射”。除了反射的定义外,我找不到关于参数的确切解释。或者我找不到在聊天响应中显示反射映射的示例。

检查下面的例子。如果输入“go”或“hello”,如何让输出显示为“gone”或“hey there”?

我只是想知道如何在聊天对答案中注入反射?

from nltk.chat.util import Chat, reflections

my_dummy_reflections= {
    "go"     : "gone",
    "hello"    : "hey there",
    "my": "your",
    "your": "my"
}

pairs = [
    [
        r"my name is (.*)",
        ["Hello %1, How are you today ?",]
    ],
     [
        r"what is your name ?",
        ["my name is Chatty and I'm a chatbot ?",]
    ],
]

chat = Chat(pairs, my_dummy_reflections)
chat.converse()

就像文档已经(含糊地)告诉您的那样,reflections 参数用于映射表达式以反映到正确的人身上。像这样:

(nltk) tripleee$ python chat.py 
>hello there
None
>my name is my secret
Hello your secret, how are you today?

注意“我的秘密”如何映射到“你的秘密”。这就是 reflections 所关心的。简而言之,返回给用户的字符串中有任何与反射匹配的字符串被替换,因此例如来自用户的第一个参数 %1 将被反射关键字替换。

这是此代码,非常直接地改编自您的尝试。

from nltk.chat.util import Chat, reflections

my_reflections= {
    "you": "I",
    "your": "my",
    "you're": "I'm",
    "I": "you",
    "my": "your",
    "I'm": "you're"
}

pairs = [
    [
        r"my name is (.*)",
        ["Hello %1, how are you today?",]
    ],
     [
        r"what is your name?",
        ["My name is Chatty and I'm a chatbot.",]
    ],
]

chat = Chat(pairs, my_reflections)
chat.converse()

(我冒昧地把标点符号前的错误空格也去掉了。)

您询问如何完成的任务可以通过将输入短语及其响应添加到 pairs 列表来轻松实现。