Skip to content

Instantly share code, notes, and snippets.

@Froren
Created December 4, 2018 07:58
Show Gist options
  • Save Froren/e56202f870eb30605236ce90210f66f9 to your computer and use it in GitHub Desktop.
Save Froren/e56202f870eb30605236ce90210f66f9 to your computer and use it in GitHub Desktop.
const thesaurus = require("thesaurus");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let corpus = "";
rl.question('FEED ME A CORPUS!\n', (ans) => {
rl.close();
corpus = ans.toLowerCase();
console.log(`Start:\n\n ${corpus}`);
setInterval(() => {
corpus = transformText(corpus);
console.log(`${corpus}\n`);
}, 200);
});
function getRandomNumber(i) {
return Math.floor(Math.random() * i);
}
function removeGreenEscapeCodes(text) {
return text.replace("\033[32m", '').replace("\033[0m", '');
}
function wrapGreenEscapeCodes(text) {
return "\033[32m" + text + "\033[0m";
}
function transformText(corpus) {
let wordArray = removeGreenEscapeCodes(corpus).split(" ");
let selectedWordIndex = 0;
let synonyms = [];
while (synonyms.length == 0) {
selectedWordIndex = getRandomNumber(wordArray.length);
synonyms = thesaurus.find(wordArray[selectedWordIndex]);
}
let selectedSynonym = synonyms[getRandomNumber(synonyms.length)];
wordArray[selectedWordIndex] = wrapGreenEscapeCodes(selectedSynonym);
return wordArray.join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment