Visualized cartographic data of the emotional "state" of each state at any given moment
Positive or negative sentiments are loosely determined based on an analysis of keywords used in tweets
positiveWords = [
'excellent', 'amazing', 'marvelous', 'magnificent',
'fabulous', 'astonishing', 'fantastic', 'peaceful',
'brilliant', 'glorious', 'cheerful', 'gracious',
...
];
var svg = d3.select('#map').append('svg')
.attr('width', 900).attr('height', 550);
var path = d3.geo.path().projection(d3.geo.albersUsa());
var g = svg.append('g');
d3.json('us-states.json', function(error, topology) {
g.selectAll('path')
.data(topojson.feature(topology,
topology.objects.usStates).features)
.enter().append('path')
.attr('class', function(d){
return 'states ' + d.properties.STATE_ABBR;} )
.attr('d', path)
.attr('fill', 'gray')
});
<script src="//cdn.pubnub.com/pubnub.min.js"></script>
var channel = 'pubnub-twitter'; var pubnub = PUBNUB.init({ subscribe_key:'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe' });
To retrieve the live tweets, you simply use PubNub subscribe()
API.
pubnub.subscribe({
channel: channel,
callback: processData
});
Create a mini dictionary that describes moods
var positiveWords = [
'excellent', 'amazing', 'happy', 'magnificent', ':)',
...
];
var negativeWords = [
'unhappy', 'sorry', 'annoyed', 'dislike', 'anxious', ':(',
...
];
See if these words are used in each tweet, in the callback function
function processData(data) {
// looking for US data only for this demo
if(data.place.country_code !== 'US') return;
// Check if emotional words are used in the tweet
if (positiveWords.some(function(v) {
return data.text.toLowerCase().indexOf(v) > 0; })) {
displayData(data, positive);
} else if
...
}
var positiveWords = [
'excellent', 'amazing', 'happy', 'magnificent', ':)',
...
];
var negativeWords = [
'unhappy', 'sorry', 'annoyed', 'dislike', 'anxious', ':(',
...
];
function displayData(data, emotion) {
var state = data.place.full_name.substring(
city.lastIndexOf(',')+1).trim();
if(document.querySelector('.'+state)) {
var stateEl = document.querySelector('.'+state);
// count
tally[state] = (tally[state] || {positive:0, negative:0});
tally[state][emotion] = (tally[state][emotion] || 0) + 1;
...
// colorize svg path
stateEl.style.fill = (
tally[state].positive > tally[state].negative)
? 'pink' :
((tally[state].positive < tally[state].negative)
? 'turquoise' : 'beige');
}
}
}