Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我在react中创建了一个聊天机器人,我需要一些特定的回复,而不是单一的关键字匹配_Javascript_Arrays_Reactjs_Algorithm_Multidimensional Array - Fatal编程技术网

Javascript 我在react中创建了一个聊天机器人,我需要一些特定的回复,而不是单一的关键字匹配

Javascript 我在react中创建了一个聊天机器人,我需要一些特定的回复,而不是单一的关键字匹配,javascript,arrays,reactjs,algorithm,multidimensional-array,Javascript,Arrays,Reactjs,Algorithm,Multidimensional Array,我在前面的堆栈溢出中找到了简单的解决方案。 我实现了它。现在,我的机器人用匹配>=3(大于等于3)的单词回复我,但如果我写“你好”,它会回复所有包含嘿的单词。我只需要一个回复“嘿,我很好”。 此外,我还有另一个数组;我想用这个数组作为同义词。下面是两个代码 handleSubmitChat = (event) => { //algorithm chatbot algorithm event.preventDefault();

我在前面的堆栈溢出中找到了简单的解决方案。 我实现了它。现在,我的机器人用匹配>=3(大于等于3)的单词回复我,但如果我写
“你好”
,它会回复所有包含
的单词。我只需要一个回复
“嘿,我很好”
。 此外,我还有另一个数组;我想用这个数组作为同义词。下面是两个代码

 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 I am fine”,而不是所有与hey相关的答案。 如果我的问题是“你好吗” 机器人回答:“嘿,我很好”,“嘿,这是花聊天机器人”。。它还回答了您的相关问题:


解决问题的一个简单方法是从计算的响应数组中选择一个元素。让我们假设上面的人类问题-
嘿,你好吗
,你得到4个句子作为回答,选择第一个

老实说,编写机器人逻辑并不是那么简单

在您使用javascript时,我建议您看看这个名为
Botkit
-的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?"}); 
所以,在训练部分,基本上你是在训练你的神经网络,基于你输入的数据以及你希望机器人如何响应。因此,通过给机器人提供一些类似的输入类型,我们可以教他当他收到来自用户的类似输入或问题时应该如何响应


希望这有帮助

嘿,谢谢@zenwraight的评论。但是你能更具体地选择第一句话吗??关于botkit,我是js的中级用户,我现在不想使用任何API,我想使用字典数组作为同义词。如果有其他方法可以实现,那么使用数组确实可以共享它。再次感谢:)@dr.kaizoku所以你需要想出一个算法,就像因为你只是在字典里寻找相同的单词,你也会得到不相关的回答。你如何选择哪一个是最合适的答案?是的,我正在努力(算法部分)。。另一方面,我正在考虑使用brain.js来训练我的机器人,我不知道,但我必须深入了解如何在代码中实现brain.js。但是谢谢你的回复。。。如果你有任何建议或想法,请与我分享。。再次感谢你,你需要一些简单的例子来理解brain.js吗?我可以编辑我的答案吗?是的,当然你不需要编辑,你可以写一个新的:)。。。现在,brain.js与react一起用于简单ai。我很感激。