Skip to content

Instantly share code, notes, and snippets.

@Lissy93
Created August 10, 2017 09:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lissy93/0d9a3d5dc9d9c49c7b4b9319a7715703 to your computer and use it in GitHub Desktop.
Save Lissy93/0d9a3d5dc9d9c49c7b4b9319a7715703 to your computer and use it in GitHub Desktop.
A quick example of calculating percentage positive or negativeness of a given sentence, using the npm sentiment-analysis module
// Include the sa module, needs installing first (npm install sentiment-analysis)
const sentimentAnalysis = require('sentiment-analysis');
/**
* Gets the sentiment of a sentence
* using the sentiment-analysis module
* formats it, and prints to console
*/
function displaySentiment(inputText, sentimentScore){
// Determine if the score is positive, neutral or negative
let overallSentiment = 'neutral';
if (sentimentScore > 0.2) overallSentiment = 'positive';
else if (sentimentScore < -0.2) overallSentiment = 'negative';
// Get persnt from score
const persentageScore = `${sentimentScore * 100}%`;
// This is the sentence to return (e.g. 40% positive)
const sentence = `${persentageScore} (${overallSentiment}) `;
return sentence;
}
/**
* Returns a color to represent the sentimentScore
* Either green, yellow or red. In the console format
*/
function getChalkColor(sentimentScore){
let chalkColor = '\x1b[33m';
if(sentimentScore > 0.2) chalkColor = '\x1b[32m';
else if(sentimentScore < -0.2) chalkColor = '\x1b[31m';
return chalkColor;
}
// Array of example text, to calculate sentiments from
const exampleText = [
'I am so angry about this useles product that fraudster sold me',
'Really cant be bothered with this dumb assignment, will do it tomorrow',
'What a gloomy day in London, wish the smog would lift a little',
'Going to write my next app in Node.js, as I like it so much more then C#',
'Whos in town tonight, and wants to head to the City Arms to watch the match?',
'Though the latest iPhone is ok, I still dont like it compared to Android',
'Just discovered the chip sandwitch, best end to a great night out ever!',
'Congratulations to my best friend who has just married her lovely man!',
'Super happy about the great result for England in that terrific football match'
];
// Iterate though each sentence, calculate sentiment and color, then print to console
for (var text of exampleText) {
const sentiment = sentimentAnalysis(text); // Get the senitment score
const color = getChalkColor(sentiment); // Get a colour to represent score
const resetColor = '\x1b[36m'; // Default text color
// Actuall print the results to the console (in color!)
console.log(color, displaySentiment(text, sentiment), resetColor, '\t'+text);
}
@Lissy93
Copy link
Author

Lissy93 commented Aug 10, 2017

This was just a quick example of how you can use the sentiment-analysis module to find out how positive of negative a sentence is.

Here's an example of the output:

image

For documentation, see: https://www.npmjs.com/package/sentiment-analysis
For source code, see: https://github.com/Lissy93/sentiment-analysis

😃

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