Skip to content

Commit

Permalink
needs some tweaking but added truth parsing back into text preprocess…
Browse files Browse the repository at this point in the history
…ing layer
  • Loading branch information
agnescameron committed Mar 26, 2020
1 parent 0c15d21 commit be3086d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
80 changes: 79 additions & 1 deletion src/helpers/textProcessing.js
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down Expand Up @@ -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<matches; i++) {
let optIndex = Math.floor(Math.random()*options.length);
sentence = sentence.replace(/\$/, options[optIndex]);
options.splice(optIndex, 1);
}
return sentence;
}

function toFirstPerson(sent) {
sent = sent.replace("your", "my");
sent = sent.replace("you", "I");
sent = sent.replace("yourself", "myself");

return sent;
}

async function parseTruthChallenge(sent) {
//parse out obvious would you rathers
var subSent;

if(natural.LevenshteinDistance("Would you rather", sent, {search: true}).distance < 3){
//remove substring
var subSent = sent.replace(natural.LevenshteinDistance("Would you rather", sent, {search: true}).substring, '').trim();
}

else if(natural.LevenshteinDistance("would u rather", sent, {search: true}).distance < 3){
var subSent = sent.replace(natural.LevenshteinDistance("would u rather", sent, {search: true}).substring, '').trim();
}

export const preProcessor = async (sent, bot) => {
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;
}
}

0 comments on commit be3086d

Please sign in to comment.