【问题标题】:i am creating a chatbot in react i need some specific replies from it instead of single keyword match我正在创建一个聊天机器人来响应我需要一些特定的回复而不是单个关键字匹配
【发布时间】: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


    【解决方案1】:

    解决您的问题的一个简单方法是从您计算的响应数组中选择一个元素。因此,假设对于上述人类问题 - 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?"}); 
    

    因此,在训练部分,您基本上是在根据您输入的数据以及您希望机器人如何响应来训练您的神经网络。因此,通过给机器人一些类似的输入类型,我们可以教他在收到来自用户的类似输入或问题时应该如何响应。

    希望这会有所帮助!

    【讨论】:

    • 嘿,感谢@zenwraight 的评论。但你能更具体地选择第一句话吗?关于 botkit,我是 js 的中间人,我现在不想使用任何 API,我想使用我的字典数组作为同义词。如果还有其他方法可以做到这一点,那么玩数组确实可以分享它。再次感谢:)
    • @dr.kaizoku 所以你需要为此想出一个算法,就像因为你只是在你的字典中寻找相同的单词,你也会得到不相关的响应。您将如何选择适合的回复?
    • 是的,我正在努力解决这个问题(算法部分)。另一方面,我正在考虑使用 Brain.js 来训练我的机器人,我不知道,但我必须深入了解才能理解如何在我的代码中实现brain.js。但是,嘿,感谢您的回复...如果您有任何建议或想法,请与我分享..再次感谢
    • 嘿,你需要一些简单的例子来理解brain.js吗?那我可以编辑我的答案了吗?
    • 是的,你不必编辑,你可以写一个新的 :) ...brain.js 与简单的人工智能反应。 o 会应用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2017-10-12
    • 2021-03-29
    • 2020-12-09
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多