diff --git a/package.json b/package.json index 45dbc2c..dbadd93 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,10 @@ "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "actions-on-google": "^2.12.0", + "compendium-js": "0.0.31", "dialogflow-fulfillment": "^0.6.1", "http-proxy-middleware": "^0.20.0", + "natural": "^0.6.3", "netlify-lambda": "^1.6.3", "ngrok": "^3.2.7", "node-fetch": "^2.6.0", diff --git a/src/App.js b/src/App.js index 9de019f..c8d16cd 100644 --- a/src/App.js +++ b/src/App.js @@ -82,7 +82,10 @@ class App extends Component { if (this.state.step !== 1) { this.appendMessage(text, true); - const preProcess = await preProcessor(text, this.state.currentBot); + + //hacky line for now, need to figure out a good way to manage this + let context = this.state.step === 7 ? "truthChallenge" : "other" + const preProcess = await preProcessor(text, this.state.currentBot, context); if(!preProcess){ runSample(text, this.state.currentBot) .then( diff --git a/src/helpers/textProcessing.js b/src/helpers/textProcessing.js index c8ae5a6..d06e382 100644 --- a/src/helpers/textProcessing.js +++ b/src/helpers/textProcessing.js @@ -1,5 +1,10 @@ +const natural = require('natural'); +const compendium = require('compendium-js'); + +//text processing lib const truths = require('./lib/truths.json'); const blacklist = require('./lib/blacklist.json'); +const wyrResponse = require('./lib/wyrResponse.json'); export const runSample = async (sample, bot) => { const response = await fetch(".netlify/functions/runSample", { @@ -62,18 +67,91 @@ export const chooseTruth = async (bot) => { return truth.truth; } +async function replacementGrammar(options, sentences){ + var sentence = sentences[Math.floor(Math.random()*sentences.length)]; + var matches = (sentence.match(/\$/g) || []).length; + for(let i=0; i { + else if(natural.LevenshteinDistance("wd u rather", sent, {search: true}).distance < 3){ + var subSent = sent.replace(natural.LevenshteinDistance("wd u rather", sent, {search: true}).substring, '').trim(); + } + + if(subSent) { + var options = subSent.split(' or '); + options = options.map(function(str) {return str = toFirstPerson(str.replace(/\?+/g, ""));}); + + if(options.length > 1){ + const response = replacementGrammar(options, wyrResponse); + return response; + } + } + + //check it's a question + //recompose into full sentence + const profile = compendium.analyse(sent)[0].profile; + + if(profile.types.includes('interrogative')){ + console.log(`asked a ${profile.dirtiness > 0.15 ? "dirty " : ''}question in the ${profile.main_tense} tense`) + } + + else{ + await createContext('notQuestion'); + } + +} + +export const preProcessor = async (sent, bot, context) => { let sentArr = sent.split(" "); //check against words blacklist let matched = sentArr.filter(word => blacklist.includes(word)) if(matched.length !== 0){ await createContext('blacklist', 5, bot); + return "hey, not cool"; } if(sent === 'truth') { const output = await chooseTruth(bot); return output; } + + let parsed; + + switch(context){ + case "truthChallenge": + parsed = await parseTruthChallenge(sent); + break; + } + + if(parsed !== undefined){ + console.log('parsed', parsed) + return parsed; + } }