问题背景:
基本上,我正在制作一个“聊天机器人”,我想从一个文本文件向它提供大量数据。在本例中,0表示人类,1表示聊天机器人。列表总是以人类对话开始,人类和机器人都可以有多重反应。根据可能的回答列表,我想生成所有可能对话的列表。我知道这可以通过递归来实现,但我就是不知道该怎么做:( 最好用伪代码或python来回答这个问题。不过,我对任何编程语言都持开放态度
示例:
Given: [ [0, "Hello"], [0, "Good morning"], [1, "Beep bop Im a bot"], [1, "Hello, dear human"], [0, "Okay, what time is it?" ]
Output: [
["Hello", "Beep bop Im a bot", "Okay, what time is it?"],
["Hello", "Hello, dear human", "Okay, what time is it?"],
["Good morning", "Beep bop Im a bot", "Okay, what time is it?"],
["Good morning", "Hello, dear human", "Okay, what time is it?"],
]
使用字典有什么好处?您可以添加键列表(人类句子)及其值(机器人响应)。我想把句子分成几类是必要的,这样才能进行合理的对话
比如说
您正在寻找一个简单的定向图中的最长路径,如下所示:
因此,您正在以下位置中寻找替代路径:
这里的递归答案很自然:
given
为空,则只有一个空路径李>given
不是空的,则让head, tail
作为given
的第一个元素,并分别作为given
的其余元素。你有三种可能:tail
为空,则唯一路径为[head.sentence]
李>head.speaker != first element tail.speaker
,则为tail
中的每个路径生成[head.sentence] + path
李>head.speaker == first element tail.speaker
,则: A.在tail
中产生路径(递归调用) B删除tail
的第一个元素并循环到3。直到你在案例1中。或2李>它为什么有效?(演示示意图):
(I)你有:
given[0].speaker == path.speaker
对于paths(given)
中的每一个path
。如果given
只有一个元素,则为真。否则,您将生成路径head.sentence + <something> == given[0].sentence + <something>
,或者在paths(tail)
中生成一个path
,其中tail[0].speaker == head.speaker == given[0].speaker
head.sentence
后面跟着paths(tail)
并且尾部第一个元素的说话人不同于head
的说话人时。假设命题(I),你可以肯定一个人的句子后面总是跟着一个计算机句子,反之亦然李>paths
,直到出现旋转为止李>在python中:
输出:
相关问题 更多 >
编程相关推荐