22
Realtime Tweet Analysis with PubNub Data Stream Tomomi Imura

Tweet emotion: real-time Tweet analysis with PubNub Data Stream

Embed Size (px)

Citation preview

Page 1: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

Realtime Tweet Analysis withPubNub Data Stream

Tomomi Imura

Page 2: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

"How is America feeling now?"Visualized cartographic data of the emotional "state" of each state at

any given moment

2

Page 3: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

http://pubnub.github.io/tweet-emotion/

3

Page 4: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

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

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

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

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

TopoJSON

8

Page 9: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

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

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

Getting Realtime Tweets fromPubNub Stream

11

Page 12: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

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

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

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

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

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

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

...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

19

Page 21: Tweet emotion: real-time Tweet analysis with PubNub Data Stream

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

Photo Credit

•  Cover: https://flic.kr/p/9id7d9 by Kate Ter Haar b

22