我正在尝试构建的机器人具有这样的问题结构:
你想了解一下新闻还是天气?
-The天气
您想要检查哪个城市?
-New york
目标是为指定的关键字纽约,机器人回答特定的天气网址。如果你回答“新闻”,然后“纽约”,它会回答网址是关于纽约的新闻。bot中有许多可供选择的选项,因此很难找到将同名关键字分离的解决方案。
发布于 2022-02-01 13:00:00
我给你举了一个你可以用的简单例子。
首先,我们定义了一种结构来保存每一个答案树和它的叶子。这是您可能使用的上下文的一种有意义的格式:
{
q: "question",
a: {
"answer1": {
q: "question2",
a: {
"answer1": {
...
},
"answer2": {
...
}
}
},
"answer2": {
q: "question2",
a: ...
}
}
}这里有一些代码可以使简单的bot逻辑的主要逻辑更加清晰:
const ctx = {q:"Would you like to know about weather or news?",a:{weather:{q:"For which city do you want ot know the current weather? (london or berlin)",a:{london:{q:"Blablbla weather... London!"},berlin:{q:"Blablabla weather... Berlin!"}}},news:{q:"For which city do you want ot know the current news? (london or berlin)",a:{london:{q:"Blablbla news... London!"},berlin:{q:"Blablabla news... Berlin!"}}}}};
// create button with text and callback arguments
const createButton = (txt, cb) => {
const b = document.createElement("button");
b.innerText = txt;
b.onclick = () => {
[...document.body.querySelectorAll("button")].forEach(e => e.remove());
cb(txt.toLowerCase())
}
document.body.appendChild(b);
}
// your main loop
const bot = (ctx) => {
alert(ctx.q); // display / alert question
const cb = (a) => (ctx = ctx.a[a], bot(ctx)); // callback goes to next inner context
ctx.a && Object.keys(ctx.a).forEach(e => createButton(e, cb)); // create buttons
}
// startup call
bot(ctx);
https://stackoverflow.com/questions/70940765
复制相似问题