我之前在堆栈溢出中找到了简单的解决方案。
我实现了它。现在,我的机器人用匹配 >=3(大于等于 3)的词回复我,但如果我写 “嘿,你好吗”
,它会回复所有包含 hey
的词。我只想要一个回复“嘿,我很好”
。
此外,我还有另一个数组;我想将该数组用作同义词。两个代码都在下面。
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
的 npm 模块 - https://botkit.ai/getstarted.html .
你也可以制作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?"});
因此,在训练部分,基本上是根据您输入的数据以及您希望机器人如何响应来训练您的神经网络。因此,通过为机器人提供一些类似的输入类型,我们可以教他在收到类似类型的用户输入或问题时应该如何回应。
希望这对您有所帮助!
关于javascript - 我正在 React 创建一个聊天机器人我需要它的一些特定回复而不是单个关键字匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864633/