【发布时间】:2020-01-11 20:20:36
【问题描述】:
我之前在堆栈溢出中找到了简单的解决方案。
我实现了它。现在我的机器人用匹配 >=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',]
}
]
}
我希望输出是“嘿,我很好”,而不是所有相关的答案。 如果我的问题是“嘿,你好吗” 机器人回复:“嘿,我很好”,“嘿,这是花聊天机器人”.. 它还回复了您相关的答案:
【问题讨论】:
标签: javascript arrays reactjs algorithm multidimensional-array