Skip to content

Instantly share code, notes, and snippets.

@lmccart
Created February 13, 2015 19:22
Show Gist options
  • Save lmccart/e14b4af9e83bfd06a1ad to your computer and use it in GitHub Desktop.
Save lmccart/e14b4af9e83bfd06a1ad to your computer and use it in GitHub Desktop.
// http://www.alchemyapi.com/api/sentiment/proc.html
var http = require('http');
//Create the AlchemyAPI object
var AlchemyAPI = require('./alchemyapi');
var alchemyapi = new AlchemyAPI();
var demo_text = 'Yesterday dumb Bob destroyed my fancy iPhone in beautiful Denver, Colorado. I guess I will have to head over to the Apple Store and buy a new one.';
http.createServer(function (req, res) {
res.setHeader('Content-Type', 'application/json');
var mad_words = ['mad', 'fuck'];
// // Determine overall sentiment of text
alchemyapi.sentiment('html', demo_html, {}, function(response) {
res.end(JSON.stringify(response['docSentiment']));
var s = parseFloat(response.docSentiment.score);
var type = response.docSentiment.type;
// RULE 1: sentiment score
if (type == 'positive') {
// set color based on s
}
else if (type == 'negative') {
// set color based on s
}
// RULE 2: create a group of words and a certain number signifies an emotion
// search for a group of words
count = 0;
var personal_pronouns = ['i', 'me', 'you', 'he', 'him', 'she', 'her', 'it', 'we', 'us', 'they', 'them'];
for (var i = 0; i < words.length; i++) {
var lowerWord = words[i].toLowerCase();
for (var j=0; j<personal_pronouns.length; j++) {
if (lowerWord == personal_pronouns[j]) {
count++;
}
}
}
console.log('The text included '+count+' personal pronouns.');
if (count > 3) {
// set color to mean...
}
// RULE 3: if message is very short... etc
if (msg.length < 10) {
// cold
color = 'gray';
}
});
}).listen(3000);
console.log('Server running at 3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment