Skip to content

Instantly share code, notes, and snippets.

@magalhini
Created October 1, 2019 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magalhini/dc7c0d75dd4fe2973e108b4287ed2193 to your computer and use it in GitHub Desktop.
Save magalhini/dc7c0d75dd4fe2973e108b4287ed2193 to your computer and use it in GitHub Desktop.
const nlp = require('compromise');
function elizaBot(input) {
const doc = nlp(input);
if (doc.has('i #Adverb? (am|feel) #Adverb? #Adjective')) {
const feeling = doc.match('i #Adverb? (am|feel) #Adverb? [#Adjective]').out('normal');
return `When did you become ${feeling}?`;
} else if (doc.has('i live in #Place')) {
const place = doc.match('[#Place]');
return `Do you like living in ${place.normalize().toTitleCase().out()}?`;
} else if (doc.has('(father|mother|dad|mom)')) {
const whichParent = doc.match('(father|mother|dad|mom)').out('normal');
return `Why don't you tell me about your ${whichParent}.`;
} else if (doc.has('how? (do|are) you #Adjective?')) {
const feeling = doc.match('[#Adjective]').out('normal');
if (feeling) return `I am ${feeling}`;
return `I am okay.`;
} else if (doc.has('^you are .')) {
const areWhat = doc.after('^you are');
return `I am ${areWhat.terms().normalize().out()}? But this is not about me.`;
} else {
return 'Can you elaborate on that?';
}
}
console.log(elizaBot(process.argv[2]));
const natural = require('natural');
const tfidf = new natural.TfIdf();
const documents = [
'Stick charts were made and used by the Marshallese to navigate the Pacific Ocean by canoe off the coast of the Marshall Islands.',
'The charts represented major ocean swell patterns and the ways the islands disrupted those patterns, typically determined by sensing disruptions in ocean swells by islands during sea navigation.',
'Most stick charts were made from the midribs of coconut fronds that were tied together to form an open framework.',
'Island locations were represented by shells tied to the framework, or by the lashed junction of two or more sticks.',
'One of the most powerful ways you can communicate and convey your message is by incorporating your designs with a beautiful canoe. We’ve come up with 37 of the best font pairings you can use in a wide range of projects.',
'The threads represented prevailing ocean surface wave-crests and directions they took as they approached islands and met other similar wave-crests formed by the ebb and flow of breakers.',
'Individual charts varied so much in form and interpretation that the individual navigator who made the chart was the only person who could fully interpret and use it.',
'The use of stick charts ended after World War II when new electronic technologies made navigation more accessible and travel among islands by canoe lessened.',
'Fonts and fonts pairings are one of the most crucial parts of a design. It allows you to create a wide range of effects and moods by utilizing different styles of typography.',
'The stick charts are a significant contribution to the history of cartography because they represent a system of mapping ocean swells, which was never before accomplished.',
'They also use different materials from those common in other parts of the world. They are an indication that ancient maps may have looked very different, and encoded different features from the earth, than the maps we use today.',
'Marshallese navigators used their senses and memory to guide them on voyages by crouching down or lying prone in the canoe to feel how the canoe was being pitched and rolled by underlying swells.'
];
// Adding all documents to the corpus
documents.forEach(function(item) {
tfidf.addDocument(item);
});
// Canoe word for last document:
// const tf = 2;
// const idf = 1 + Math.log(10 / (1 + 4));
// const combinedTfIdf = tf * idf // 3.87723;
// Getting the values for "canoe"
tfidf.tfidfs('canoe', (docIndex, tfidfMeasure) => {
console.log('Document', `${docIndex}: ${tfidfMeasure}`);
});
// to find the most important words
tfidf.listTerms(0).forEach((item) => console.log(`${item.term}: ${item.tfidf}`));
const nlp = require('compromise');
const wtf = require('wtf_wikipedia');
const text = wtf.fetch('https://en.wikipedia.org/wiki/Computer_programming').then((doc) => {
const parsed = doc.plaintext();
const topics = nlp(parsed).normalize({ plurals: true, parentheses: true, possessives: true }).nouns();
console.log(topics.out('topk'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment