0

I just started learning Node.js and can't figure out how to load data from a local CSV file and use wink Naive Bayes Text Classifier to learn the data.

I can load the data from local CSV file using this or this example. But the problem is that I do not know how to load wink's Naive Bayes library to client side JS. Is there a way to include node's modules (like wink) in the script that I wrote below?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Naive Bayes</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"
            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous"></script>
    <script src="./papaparse.min.js"></script>

</head>
<body>
<input type="file" id="csv-file" name="files"/>
</body>
<script>
    // REF: https://www.joyofdata.de/blog/parsing-local-csv-file-with-javascript-papa-parse/
    // http://archive.is/ySSC8
    var data;
    function handleFileSelect(evt) {
        var file = evt.target.files[0];
        Papa.parse(file, {
            header: true,
            dynamicTyping: true,
            complete: function(results) {
                data = results;
            }
        });
    }

    $(document).ready(function(){
        $("#csv-file").change(handleFileSelect);
    });
</script>

</html>
user1330974
  • 2,500
  • 5
  • 32
  • 60
  • 1
    You can transpile node modules into bundles usable in the front end using a module bundler such as webpack or browserify. More info here: https://webpack.js.org/ – Jake Miller Jun 26 '18 at 02:45
  • @JakeMiller Thank you! I'll take a look at it and may come back with follow-up questions, if any. – user1330974 Jun 26 '18 at 02:54

1 Answers1

1

An easy way to use node modules when working in the browser is to use browserify. In your working directory:

npm install wink-naive-bayes-text-classifier --save
npm install -g browserify

You'll have to move your code into a separate script file, let us say process-data.js. And, from your HTML you'll be including a different script — bundle.js (we'll come to this at the end):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Naive Bayes</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"
            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous"></script>
    <script src="./papaparse.min.js"></script>
</head>
<body>
    <input type="file" id="csv-file" name="files"/>
    <script src="bundle.js"></script>
</body> 
</html>

In your process-data.js, you can now simply require the library as is shown in its documentation.

// REF: https://www.joyofdata.de/blog/parsing-local-csv-file-with-javascript-papa-parse/
// http://archive.is/ySSC8

// Load Naive Bayes Text Classifier
var Classifier = require( 'wink-naive-bayes-text-classifier' );
// Instantiate
var nbc = Classifier();

var data;
function handleFileSelect(evt) {
    var file = evt.target.files[0];
    Papa.parse(file, {
        header: true,
        dynamicTyping: true,
        complete: function(results) {
            data = results;
            // You can now use nbc and data :)
            // nbc.learn(data[0]);
        }
    });
}

$(document).ready(function(){
    $("#csv-file").change(handleFileSelect);
});

Finally, to create the bundle.js file you'll run browserify:

browserify process-data.js -o bundle.js

This will bundle all the modules you need together into a the file that your HTML is calling. If you don't want to type so much every time you might consider adding an npm script.

prtksxna
  • 643
  • 6
  • 17