tweet emotion: real-time tweet analysis with pubnub data stream
TRANSCRIPT
![Page 1: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/1.jpg)
Realtime Tweet Analysis withPubNub Data Stream
Tomomi Imura
![Page 2: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/2.jpg)
"How is America feeling now?"Visualized cartographic data of the emotional "state" of each state at
any given moment
2
![Page 4: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/4.jpg)
Pseudo-analysisPositive 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', ...];
4
![Page 5: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/5.jpg)
Process1. using TopoJSON to display a US state map using d3.js
2. using PubNub JavaScript API to consume Twitter stream
3. colorizing the map by moods
5
![Page 6: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/6.jpg)
GeoJSON• Open standard format for encoding geographical features
• Written in JavaScript Object Notation
• Typically large file size
6
![Page 7: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/7.jpg)
TopoJSON• Extension of GeoJSON that encodes geospatial topology
• Compressed / Smaller size
• Tailored for only what you need
7
![Page 8: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/8.jpg)
TopoJSON
8
![Page 9: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/9.jpg)
Render TopoJSON with D3.js
var svg = d3.select('#map').append('svg') .attr('width', 900).attr('height', 550);
var path = d3.geo.path().projection(d3.geo.albersUsa());
9
![Page 10: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/10.jpg)
Render TopoJSON with D3.jsvar 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')}); 10
![Page 11: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/11.jpg)
Getting Realtime Tweets fromPubNub Stream
11
![Page 12: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/12.jpg)
Getting Started with PubNub APIs
<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'});
01.
02.
12
![Page 13: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/13.jpg)
Receiving Live JSON DataTo retrieve the live tweets, you simply use PubNub subscribe()
API.
pubnub.subscribe({ channel: channel, callback: processData});
13
![Page 14: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/14.jpg)
Pseudoanalyze TweetsCreate a mini dictionary that describes moods
var positiveWords = [ 'excellent', 'amazing', 'happy', 'magnificent', ':)', ...];var negativeWords = [ 'unhappy', 'sorry', 'annoyed', 'dislike', 'anxious', ':(', ...]; 14
![Page 15: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/15.jpg)
Pseudoanalyze TweetsSee 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 ...}
15
![Page 16: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/16.jpg)
Pseudoanalyze TweetsCreate a mini dictionary that describes moods
var positiveWords = [ 'excellent', 'amazing', 'happy', 'magnificent', ':)', ...];var negativeWords = [ 'unhappy', 'sorry', 'annoyed', 'dislike', 'anxious', ':(', ...]; 16
![Page 17: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/17.jpg)
Display Data on Mapfunction 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;
...
17
![Page 18: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/18.jpg)
...cont'd
// colorize svg path stateEl.style.fill = ( tally[state].positive > tally[state].negative) ? 'pink' : ((tally[state].positive < tally[state].negative) ? 'turquoise' : 'beige'); } }}
18
![Page 19: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/19.jpg)
19
![Page 20: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/20.jpg)
Full Article blog.twitter.com
20
![Page 21: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/21.jpg)
Thank you!Tomomi Imura @ PubNub
pubnub.com
@pubnub
@girlie_mac
github.com/pubnub
21
![Page 22: Tweet emotion: real-time Tweet analysis with PubNub Data Stream](https://reader030.vdocuments.mx/reader030/viewer/2022032422/55a92bd51a28ab793e8b4689/html5/thumbnails/22.jpg)
Photo Credit
• Cover: https://flic.kr/p/9id7d9 by Kate Ter Haar b
22