zoukankan      html  css  js  c++  java
  • [Javascript] Classify text into categories with machine learning in Natural

    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("
    ");
        console.log(item.text);
        console.log("Label:", labelGuess);
        console.log(classifier.getClassifications(item.text));
    });
  • 相关阅读:
    return跳转 和 goto跳转
    continue跳转
    break跳转
    循环的嵌套,以for循环为例
    for“列表”型循环
    do while“直到”型循环
    while“当”型循环
    选择结构 switch
    Tomcat和Servlet简析
    并发事务和隔离级别
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7624277.html
Copyright © 2011-2022 走看看