Last active
August 29, 2015 14:18
-
-
Save reklis/01308ac2d9d9591f7fbc to your computer and use it in GitHub Desktop.
twitter sentiment analysis graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# brew install jshon | |
# npm i -g json | |
# npm i -g underscore-cli | |
json -g -c 'this.tweet.in_reply_to_screen_name' < sample.json | underscore process '_.map(data, function(value) { return { | |
valence: value.valence, | |
polarity: value.polarity, | |
user: value.tweet.user.screen_name, | |
mentions: _.map(value.tweet.entities.user_mentions, function(mention) { return mention.screen_name }) | |
}})' | underscore filter 'value.mentions.length' | underscore process '_.groupBy(data, "valence")' | jshon -e positive | underscore process '_.map(data, function(d) { | |
return _.map(d.mentions, function(mention) { | |
return d.user + " " + mention | |
})})' | underscore select 'string' | jshon -a -u > positive.graph |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# brew install graphviz | |
awk 'BEGIN { print "digraph G {" } { print $1 " -> " $2 ";" } END { print "}" }' < positive.graph | neato -Tsvg > positive.graph.svg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var | |
util = require('util'), | |
Twitter = require('twitter'), | |
Retext = require('retext'), | |
visit = require('retext-visit'), | |
sentiment = require('retext-sentiment'), | |
rt, twclient, endpoint, terms | |
function debug (o) { | |
console.log(util.inspect(o, { depth: null, colors: true })) | |
} | |
rt = new Retext() | |
.use(visit) | |
.use(sentiment) | |
twclient = new Twitter({ | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY, | |
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET | |
}) | |
terms = (process.argv.length > 2) ? { track: process.argv.splice(2).join(' ') } : null | |
endpoint = terms ? 'statuses/filter' : 'statuses/sample' | |
twclient.stream(endpoint, terms, function (stream) { | |
stream.on('data', function (tweet) { | |
rt.parse(tweet.text, function (rterr, rttree) { | |
if (rterr) { | |
debug(rterr) | |
return | |
} | |
if (!rttree.data.polarity) { | |
return | |
} | |
var point = { | |
tweet: tweet, | |
valence: rttree.data.valence, | |
polarity: rttree.data.polarity | |
} | |
console.log(JSON.stringify(point)) | |
}) | |
}) | |
stream.on('error', function (error) { | |
debug(error) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment