javascript - 我正在创建一个聊天机器人来响应我需要一些特定的回复而不是单个关键字匹配
问题描述
我之前在堆栈溢出中找到了简单的解决方案。我实现了它。现在我的机器人用匹配 >=3(大于等于 3)"hey how are you"
的单词回复我,但如果我写它,它会回复所有包含hey
. 我只想要一个答复"hey I am fine"
。此外,我还有另一个数组;我想将该数组用作同义词。两个代码都在下面。
handleSubmitChat = (event) => {
//algorithm chatbot algorithm
event.preventDefault();
//for single string match example: user can input water an get water word related answers...
// let matches = this.state.keywords.filter(s => s.includes(this.state.humanarea))
// Split spaces after user sends
let searchString = this.state.humanarea.toLowerCase()
let searchStringSplit = searchString.split(/(\s+)/).filter(function (e) { return e.trim().length >= 3; })
if (searchStringSplit.length >= 1) {
this.state.matches = this.state.keywords.filter(replykey => {
let containsAtLeastOneWord = false;
// If at least a word is matched it returns the matching answers!
searchStringSplit.forEach(word => {
if (replykey.toLowerCase().includes(word)) {
containsAtLeastOneWord = true;
}
})
if (containsAtLeastOneWord) {
return replykey
}console.log(replykey)
})
this.setState({
botarea: this.state.matches
})
console.log(this.state.matches)
}
}
constructor(props) {
super(props);
this.state = {
humanarea: '',
matches: [''],
keywords: [
'hi',
'hey i am fine',
'hey this is flower chatbot',
'i need water',
'water makes me bloom',
'protect me i am hurt',
'disable my defence so bees can come',
'bees can make honey all day',
'thanks bees for making honey ',
'you are awesome',
'only you can touch me'
],
dictonary: [ //synonyms
{
greet: `//user input should first match these key words as synonyms...`
[
'greetings',
'hi',
'hey',
'howdy',
'welcome',
'good morning',
'how are you',
'how goes it',
'howdy-do',
'whats happening',
'whats up'
],
`//other synonyms....`
group2: ['water', 'need',],
group3: ['bloom',],
group4: ['bees',],
group5: ['honey',]
}
]
}
我希望输出是“嘿,我很好”,而不是所有相关的答案。如果我的问题是“嘿,你好吗”机器人回复:“嘿,我很好”,“嘿,这是花聊天机器人”.. 它还回复了您相关的答案:
解决方案
您的问题的一个简单解决方案可能是从您计算的响应数组中选择一个元素。所以让我们说对于上面的人类问题 - Hey how are you
,你得到 4 个句子作为回应,选择第一个。
现在说实话,编写机器人逻辑并不是那么简单。
当您使用 javascript 时,我建议您查看这个名为Botkit
- https://botkit.ai/getstarted.html的 npm 模块。
你也可以制作Brain.js
请按照以下步骤操作:-
1.使用此命令安装 Brain.js - npm install brain.js。
2.现在在你的逻辑文件中,你将使用类似的东西来训练你的神经网络。
const net = new brain.NeuralNetwork();
net.train([{input: {"Hey, how are you?"}, output: { "I am fine": 1 }},
{input: { "Hey, how are you doing?" }, output: { "Hey, I am fine": 1 }},
{input: { "Hey, how is your day going?" }, output: { "Hey, I am doing fine, How about you?": 1 }}]);
const output = net.run({"Hey How are you?"});
所以,在训练部分,基本上你正在训练你的神经网络,基于你输入的数据以及你希望机器人如何响应。因此,通过给机器人一些类似的输入类型,我们可以教他在收到来自用户的类似输入或问题时应该如何响应。
希望这可以帮助!
推荐阅读
- azure-ad-b2c - 政策变化没有立即反映
- kubernetes - 如何使用 kubectl 找出 K8s 作业是失败还是成功?
- javascript - 将功能分离到单独的 index.js 文件中
- ios - 动画按钮的图像旋转
- java - 带有 ajc 的 Gradle 不读取 Lombok 注释
- javascript - 使用具有多个索引的反应式搜索,但它们并不都具有相同的数据字段
- c# - C#如何创建泛型类型参数
- python - 多线程 OpenCV Python 脚本
- ios - 从 Swift 4 中的核心数据对 TableView 中的数据进行分组
- xml - 查找直到某个点的元素