Skip to content

Instantly share code, notes, and snippets.

@germanattanasio
Last active August 29, 2015 14:27
Show Gist options
  • Save germanattanasio/2eba167fbc9fb6c8a5f6 to your computer and use it in GitHub Desktop.
Save germanattanasio/2eba167fbc9fb6c8a5f6 to your computer and use it in GitHub Desktop.
How to create a classifier using the nodejs wrapper
'use strict';
var watson = require('../nodejs-wrapper/lib/index');
var fs = require('fs');
var natural_language_classifier = watson.natural_language_classifier({
url: 'https://gateway.watsonplatform.net/natural-language-classifier/api',
username: 'USERNAME',
password: 'PASSWORD',
version: 'v1'
});
// Classifiers can be created using a string, csv stream or json object
// 1. string
var csv = fs.readFileSync('./training_data.csv');
// 2. stream - util for large files
var stream = fs.createReadStream('./training_data.csv');
// 3. json - converted to csv
var json = [
{ text: 'text 1', classes: ['class1','class2'] },
{ text: 'text 2', classes: ['class1'] },
//...
];
var params = {
language: 'en',
name: 'my-json-classifier',
training_data: json //replace with stream or csv
};
natural_language_classifier.create(params, function(err, response) {
if (err)
console.log(err);
else
console.log(JSON.stringify(response, null, 2));
});
@germanattanasio
Copy link
Author

  • The training_data.csv file can be found here.
  • When creating a classifier using json make sure you have more than 5 training elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment