Skip to content

Instantly share code, notes, and snippets.

@sempostma
Created December 17, 2018 17:26
Show Gist options
  • Save sempostma/0966f4bd6de07ff61d975429ff342a34 to your computer and use it in GitHub Desktop.
Save sempostma/0966f4bd6de07ff61d975429ff342a34 to your computer and use it in GitHub Desktop.
const NLP = require('natural');
const path = require('path');
const readline = require('readline');
const fs = require('fs');
const { stemmer } = require('./config');
const trainingDataPath = process.argv[2];
const classifierName = process.argv[3];
if (!trainingDataPath || !classifierName) {
console.log('usage: node . <path/to/trainingdata.json> <classifier_name>');
return;
}
const getPathTotrainingData = commandLineTrainingDataPathInput => {
const resolvedTraningDataPath = path.resolve(__dirname, commandLineTrainingDataPathInput);
if (!fs.existsSync(resolvedTraningDataPath)
&& fs.existsSync(path.join(__dirname, 'trainingdata', commandLineTrainingDataPathInput))) {
return path.join(__dirname, 'trainingdata', trainingDataPath);
} else {
return path.resolve(__dirname, trainingDataPath);
}
}
const pathTotrainingData = getPathTotrainingData(trainingDataPath);
const trainingData = JSON.parse(fs.readFileSync(pathTotrainingData).toString());
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const classifierFile = classifierName.endsWith('.json') ? classifierName : classifierName + '.json';
const classifierFileName = path.join(__dirname, 'classifiers', classifierFile);
NLP.LogisticRegressionClassifier.load(classifierFileName, stemmer, (err, classifier) => {
if (err) throw err;
process.on('SIGINT', () => process.exit());
const q = () => {
rl.question('Question: ', question => {
handleMessage({
reply: (msg, answer) => {
console.log(answer);
q();
}
}, { text: question });
});
}
const interpret = (phrase) => {
// console.log('interpret', phrase);
const guesses = classifier.getClassifications(phrase.toLowerCase());
// console.log('guesses', guesses);
const bestprob = Math.max(...guesses.map(x => x.value));
// ADD RANDOMNESS
guesses.map(x => x.value += Math.random() * .4);
const guess = guesses.reduce((x, y) => x && x.value > y.value ? x : y);
return {
probabilities: guesses,
bestprob,
guess: guess.label
};
}
const rnd = arr => arr[Math.floor(Math.random() * arr.length)];
const handleMessage = (speech, message) => {
const interpretation = interpret(message.text);
// console.log('InternChatBot heard: ', message.text);
// console.log('InternChatBot interpretation: ', interpretation);
if (interpretation.bestprob < 0.5) {
speech.reply(message, 'Sorry, I\'m not sure what you mean');
} else if (interpretation.bestprob < 0.7) {
speech.reply(message, 'Uhhh... ' + rnd(trainingData[interpretation.guess].answers));
} else if (interpretation.guess && trainingData[interpretation.guess]) {
// console.log('Found response');
speech.reply(message, rnd(trainingData[interpretation.guess].answers));
}
}
q();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment