当前位置: 首页 > 工具软件 > Easy-Classify > 使用案例 >

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

慕铭
2023-12-01

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));
});

 

 类似资料: