Skip to content

Instantly share code, notes, and snippets.

@butaji
Created March 18, 2020 02:05
Show Gist options
  • Save butaji/28b04ed288d04380e335b85feb3b5d09 to your computer and use it in GitHub Desktop.
Save butaji/28b04ed288d04380e335b85feb3b5d09 to your computer and use it in GitHub Desktop.
const natural = require('natural');
const extract = require('article-parser').extract;
const run = (c, url) => {
natural.BayesClassifier.load('classifier.json', null, async(err, classifier) => {
if (err) {
console.log(err);
classifier = new natural.BayesClassifier();
}
const article = (await extract(url)).content;
classifier.addDocument(article, c);
classifier.train();
const classifications = classifier.getClassifications(article);
console.log(classifications);
const cls = classifications.reduce((acc, v) => {
acc[v.label] = v.value;
return acc;
}, {});
await classifier.save('classifier.json');
console.log('good:', cls['good'] > cls['bad'], 'as', (cls['good'] / cls['bad']));
});
}
run(process.argv[2], process.argv[3]);
npm i natural -s
npm i article-parser -s
node p.js [bad/good] [url]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment