Created
May 8, 2015 20:22
-
-
Save bbarr/cf28ca468bea18f5fc7b to your computer and use it in GitHub Desktop.
tweet analysis funtimes
This file contains 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
var Twitter = require('twitter'); | |
var analyze = require('Sentimental').analyze | |
var R = require('ramda') | |
var client = new Twitter({ | |
consumer_key: '', | |
consumer_secret: '', | |
access_token_key: '', | |
access_token_secret: '' | |
}); | |
var params = {screen_name: 'netflix', count: 200}; | |
client.get('statuses/user_timeline', params, function(error, tweets, response){ | |
if (error) return | |
var ts = R.map(R.pick([ 'retweet_count', 'text' ]), tweets) | |
var getCount = R.prop('retweet_count') | |
var getText = R.prop('text') | |
var ave = function(nums) { | |
return R.reduce(R.add, 0, nums) / nums.length | |
} | |
var counts = R.map(getCount, ts) | |
var aveCount = ave(counts) | |
var wasWellReceived = R.compose(R.gte(aveCount), getCount) | |
var rateResponses = R.partition(wasWellReceived) | |
var analyzeAll = R.map(R.compose(analyze, getText)) | |
var analysis = analyzeAll(ts) | |
var zipped = R.zip(ts, analysis) | |
var bySentiment = R.reduce(function(memo, v, i) { | |
if (!v[1].score) return memo | |
return memo.concat(R.merge(v[0], { x: v[1].score > 0, _raw: v[1].score })) | |
}, [], zipped) | |
var byQuote = R.reduce(function(memo, v, i) { | |
return memo.concat(R.merge(v, { x: /"[^"]+"/.test(v.text) })) | |
}, [], ts) | |
var sentimentParts = R.partition(R.prop('x'), bySentiment) | |
var quoteParts = R.partition(R.prop('x'), byQuote) | |
var yepNopeAves = R.compose(R.map(ave), R.map(R.map(getCount))) | |
var sentimentAves = yepNopeAves(sentimentParts) | |
var quoteAves = yepNopeAves(quoteParts) | |
console.log('For positive sentiment: ', sentimentAves[0]) | |
console.log('For negative sentiment: ', sentimentAves[1]) | |
console.log('-------------------') | |
console.warn('Result: ', sentimentAves[0] > sentimentAves[1] ? 'positive ' : 'negative ', 'wins!') | |
console.log('-------------------') | |
console.log('For quotes: ', quoteAves[0]) | |
console.log('For non-quotes: ', quoteAves[1]) | |
console.log('-------------------') | |
console.warn('Result: ', quoteAves[0] > quoteAves[1] ? 'quotes ' : 'non-quote ', 'wins!') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment