DEV Community

Pallavi Ratra for WinkJS

Posted on • Originally published at


How to do sentiment analysis?

Sentiment analysis is used to check if a phrase has positive or negative sentiment. To get the sentiment score, start by reading the phrase using readDoc. Then using the its.sentiment property of the out() method, get the sentiment score of the phrase. This is how you can check if a phrase is positive or negative:

// Load wink-nlp package & helpers.
const winkNLP = require( 'wink-nlp' );
const its = require( 'wink-nlp/src/its.js' );
const model = require( 'wink-eng-lite-model' );
const nlp = winkNLP( model );

const text = 'Very excited to be part of the @winkjs_org team:D!';
const doc = nlp.readDoc( text );

// Calculate sentiment score
const sentiment = doc.out( its.sentiment );
console.log( 'Sentiment Score:', sentiment );

if( sentiment > 0 ) {
  console.log( 'Sentiment: Positive' );
} else {
  console.log( 'Sentiment: Negative' );

// -> Sentiment Score: 0.6
// -> Sentiment: Positive
Enter fullscreen mode Exit fullscreen mode

The sentiment score will be between -1 and 1, with 0 representing an overall neutral sentiment. You can get the sentiment of the entire document, or a single sentence in a text.

Sentiment analysis can be used to judge the overall response to an event, product etc. In chatbots, it can be used to change the response based on the sentiment of the message it has received. See our Twitter Hashtag showcase to see this in action.

winkNLP's English language lite model uses ML-SentiCon as a base with further training. For emojis it uses the Emoji Sentiment Ranking. Together, they deliver an f-score of about 84.5%.

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await