码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Classify text into categories with machine learning in Natural

时间:2017-10-03 19:43:51      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:esc   var   info   party   hang   ble   lin   training   scl   

In this lesson, we will learn how to train a Naive Bayes classifier or a Logistic Regression classifier - basic machine learning algorithms - in order to classify text into categories.

 

var natural = require(‘natural‘);
var classifier = new natural.BayesClassifier();

var trainingData = [
    {text: ‘RE: Canadian drugs now on sale‘, label: ‘spam‘}, 
    {text: ‘Earn more from home‘, label: ‘spam‘},
    {text: ‘Information now available!!!‘, label: ‘spam‘},
    {text: ‘Earn easy cash‘, label: ‘spam‘},
    {text: ‘Your business trip is confirmed for Monday the 4th‘, label: ‘notspam‘},
    {text: ‘Project planning - next steps‘, label: ‘notspam‘},
    {text:‘Birthday party next weekend‘, label: ‘notspam‘},
    {text: ‘Drinks on Monday?‘, label: ‘notspam‘}
];

var testData = [
    {text: ‘Drugs for cheap‘, label: ‘spam‘},
    {text: ‘Next deadline due Monday‘, label: ‘notspam‘},
    {text: ‘Meet me at home?‘, label: ‘notspam‘},
    {text: ‘Hang out with someone near you‘, label: ‘spam‘}
];

trainingData.forEach(function(item){
    classifier.addDocument(item.text, item.label);
});

classifier.train();

testData.forEach(function(item){
    var labelGuess = classifier.classify(item.text);
    console.log("\n");
    console.log(item.text);
    console.log("Label:", labelGuess);
    console.log(classifier.getClassifications(item.text));
});

 

[Javascript] Classify text into categories with machine learning in Natural

标签:esc   var   info   party   hang   ble   lin   training   scl   

原文地址:http://www.cnblogs.com/Answer1215/p/7624277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!