Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Sentiment classifier

node v0.12.18
version: 1.0.0
endpointsharetweet
Import libraries: the main library qminer and a helper package for the example dataset.
var qm = require('qminer-try'); var loader = require('qminer-data-loader'); 'loaded'
Define the storage schema. We define one store called 'tweets' where each record has two fields: text and target (+1 is for positive sentiment and -1 for negative sentiment).
var base = new qm.Base({ mode: 'createClean', schema: [{ name: 'tweets', fields: [{ name: 'text', type: 'string' }, { name: 'target', type: 'float' }] }] }); 'defined schema'
Import data and select all records.
loader.loadSentimentDataset(base.store('tweets')); var tweets = base.store('tweets').allRecords; 'got ' + tweets.length + ' tweets';
Build feature space (mapping from records to linear algebra vectors). Here we use a simple tfidf weighted bag-of-words feature extractor.
var featureSpace = new qm.FeatureSpace(base, { type: 'text', source: 'tweets', field: 'text'}); featureSpace.updateRecords(tweets); 'built feature space with ' + featureSpace.dim + ' dimensions'
Build a sentiment classifier model. We use a Support Vector Classifier with default parameters (C = 1).
var SVC = new qm.analytics.SVC({ maxTime: 5 }); SVC.fit(featureSpace.extractSparseMatrix(tweets), tweets.getVector('target')); 'trained model'
Predict sentiment of a new example.
var y1 = SVC.predict(featureSpace.extractSparseVector(base.store('tweets').newRecord({ text: "Cats are stupid." }))); 'predicted sentiment ' + (y1 > 0 ? 'positive' : 'negative');
Another example.
var y2 = SVC.predict(featureSpace.extractSparseVector(base.store('tweets').newRecord({ text: "Cats are totally amazing!" }))); 'predicted sentiment ' + (y2 > 0 ? 'positive' : 'negative');
Loading…

no comments

    sign in to comment