From 1dac91bdf25a863096bdf1c5a30acf60fb9e5864 Mon Sep 17 00:00:00 2001 From: Nero Ignis Date: Sun, 16 Aug 2020 13:56:15 +0200 Subject: [PATCH] Move answers to data. --- .idea/workspace.xml | 23 ++++++++---- app.js | 86 +++++++++++++++++++++++++-------------------- 2 files changed, 64 insertions(+), 45 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bcca83b..15f7fd4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,6 +5,7 @@ + @@ -240,17 +248,18 @@ - - + - + @@ -267,10 +276,10 @@ - + - + diff --git a/app.js b/app.js index 22f44f4..edccd08 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,31 @@ let kara = new Vue({ isTyping: false, templates: { initialGreeting: "Hi! I'm Kara. :)" - } + }, + answers: [ + { + includeAll: false, + keywords: [ + 'hi', 'hallo', 'servas', 'servus', 'hello' + ], + responses: [ + 'Hey! :)', + 'Hello!', + 'How are you?' + ] + }, + { + includeAll: true, + keywords: [ + 'how', 'are', 'you' + ], + responses: [ + 'I\'m good! Thanks for asking! :) How about you?', + 'A bit tired from trying to do that think called "thinking".. i\'ll figure it out one day.', + 'Wooo! I\'m hyped for party! You\'re in?' + ] + } + ] }, mounted() { this.initialGreeting(); @@ -49,10 +73,10 @@ let kara = new Vue({ }, 2500); setTimeout(() => { - this.aiMessage( - // this.getAnswerFromDB(message) - this.getAnswer(message) - ); + let answer = this.getAnswer(message); + console.log(answer); + + this.aiMessage(answer); this.scrollDown(); }, 3000); }, @@ -63,7 +87,6 @@ let kara = new Vue({ }, oneOf(answers) { let amountOfAnswers = answers.length; - console.log(answers); let randomIndex = Math.floor(Math.random() * (amountOfAnswers)); console.log(randomIndex); @@ -121,42 +144,29 @@ let kara = new Vue({ message = message.toLowerCase(); message = this.cleanupMessage(message); let phrases = message.split(' '); + let answer = undefined; - // TODO: get options from json - if ( - this.includesOneOf(phrases,['hi', 'hallo', 'servas', 'servas', 'servus', 'hello']) - ) { - return this.oneOf([ - 'Hey! :)', - 'Hello!', - 'How are you?' - ]); - } + this.answers.forEach((answerOption) => { + if (answerOption.includeAll === true) { + if ( + this.includesAllOf(phrases, answerOption.keywords) + ) { + answer = this.oneOf(answerOption.responses); + } + } else { + if ( + this.includesOneOf(phrases, answerOption.keywords) + ) { + answer = this.oneOf(answerOption.responses); + } + } + }); - if ( - this.includesAllOf(phrases,['how', 'are', 'you']) - ) { - return this.oneOf([ - 'I\'m good! Thanks for asking! :) How about you?', - 'A bit tired from trying to do that think called "thinking".. i\'ll figure it out one day.', - 'Wooo! I\'m hyped for party! You\'re in?' - ]); + if (answer) { + return answer; } return 'I don\'t know what to say..'; - }, - // getAnswerFromDB(message) { - // let answer = null; - // - // axios.post('/karaAI/', { - // message: message - // }) - // .then(function (response) { - // console.log(response); - // }) - // .catch(function (error) { - // console.log(error) - // }) - // } + } } }) \ No newline at end of file