using clojure for sentiment analysis of the twittersphere (euroclojure 2014)

124
Sentiment Analysis of the Twittersphere

Upload: garycrawford

Post on 11-Aug-2014

782 views

Category:

Data & Analytics


3 download

DESCRIPTION

Slides from "Using Clojure for Sentiment Analysis of the Twittersphere" - EuroClojure 2014. I had to convert from Keynote to pdf for upload, so apologies for loss of animations or notes.

TRANSCRIPT

Page 1: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Sentiment Analysis of the Twittersphere

Page 2: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 3: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 4: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Leiningen Versus the Ants!

Clojure Versus Java!

FP Versus OOP

Page 5: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Leiningen Versus the Ants!

Clojure Versus Java!

FP Versus OOP

Apache Ant

Page 6: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Leiningen Versus the Ants

Clojure Versus Java!

FP Versus OOP

Apache Ant

Page 7: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Leiningen Versus the Ants

Clojure Versus Java

Functional Versus Object Oriented

Apache Ant

Page 8: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Blood aches pain sick shivers spasm vomit dizzy fainting colic and in a coma!

1 FAVORITE

Page 9: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Blood aches pain sick shivers spasm vomit dizzy fainting colic and in a coma!

1 FAVORITE

Scores HIGH for flu symptoms

Page 10: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Blood aches pain sick shivers spasm vomit dizzy fainting colic and in a coma!

1 FAVORITE

Who is Catalina Rubottom?

Page 11: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Blood aches pain sick shivers spasm vomit dizzy fainting colic and in a coma!

1 FAVORITE

HDFS/Hadoop

Mongo/Aggregation

Mongo/MapReduce

Postgres

30 million geo-tagged tweets sent from UK

Page 12: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

How can we do fast, real time analytics of

social media?

Page 13: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Store this!

Not this!

Page 14: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ptaoussanis/carmine

Page 15: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as car]) !!(defn create-tweet-id [] (first (wcar* (car/incr "global:tweet.id")))) !!(defn set-values [sentiment location created-at] (let [tweed-id (create-tweet-id)] (wcar* (car/setbit sentiment tweet-id 1) (car/setbit location tweet-id 1) (car/setbit created-at tweet-id 1))))

Page 16: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as car]) !!(defn create-tweet-id [] (first (wcar* (car/incr "global:tweet.id")))) !!(defn set-values [sentiment location created-at] (let [tweed-id (create-tweet-id)] (wcar* (car/setbit sentiment tweet-id 1) (car/setbit location tweet-id 1) (car/setbit created-at tweet-id 1))))

Page 17: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as car]) !!(defn create-tweet-id [] (first (wcar* (car/incr "global:tweet.id")))) !!(defn set-values [sentiment location created-at] (let [tweed-id (create-tweet-id)] (wcar* (car/setbit sentiment tweet-id 1) (car/setbit location tweet-id 1) (car/setbit created-at tweet-id 1))))

Page 18: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as car]) !!(defn create-tweet-id [] (first (wcar* (car/incr "global:tweet.id")))) !!(defn set-values [sentiment location created-at] (let [tweed-id (create-tweet-id)] (wcar* (car/setbit sentiment tweet-id 1) (car/setbit location tweet-id 1) (car/setbit created-at tweet-id 1))))

Page 19: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as car]) !!(defn create-tweet-id [] (first (wcar* (car/incr "global:tweet.id")))) !!(defn set-values [sentiment location created-at] (let [tweed-id (create-tweet-id)] (wcar* (car/setbit sentiment tweet-id 1) (car/setbit location tweet-id 1) (car/setbit created-at tweet-id 1))))

find value for keyfind bit of index

set bit to 1

Page 20: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar* (car/bitcount "SCOTLAND") (car/bitcount "JOVIALITY") (car/bitcount "26062014114532"))

Page 21: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG

SCT

WAL

NIR

valuekey

Page 22: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 1

SCT 0

WAL 0

NIR 0

(car/setbit “ENG” 0 1)

tweet-idkey

valuekey

Page 23: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 1

SCT 1 0

WAL 0 0

NIR 0 0

(car/setbit “SCT” 1 1)

tweet-idkey

valuekey

Page 24: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 1 0 1

SCT 0 1 0

WAL 0 0 0

NIR 0 0 0

(car/setbit “ENG” 2 1)

tweet-idkey

valuekey

Page 25: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

WAL 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0

NIR 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

(car/setbit “SCT” 15 1)

tweet-idkey

valuekey

Page 26: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

WAL 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0

NIR 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

(car/bitcount "ENG")

8

valuekey

Page 27: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

WAL 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0

NIR 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

(car/bitcount "SCT")

84

valuekey

Page 28: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

WAL 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0

NIR 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

(car/bitcount "WAL")

843

valuekey

Page 29: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

WAL 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0

NIR 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

(car/bitcount "NIR")

8431

valuekey

Page 30: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JOVIAL

SHY

HOSTILE

FATIGUE

valuekey

Page 31: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JOVIAL 1

SHY 0

HOSTILE 0

FATIGUE 0

(car/setbit “JOVIAL” 0 1)

tweet-idkey

valuekey

Page 32: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JOVIAL 0 1

SHY 0 0

HOSTILE 1 0

FATIGUE 0 0

(car/setbit “HOSTILE” 1 1)

tweet-idkey

valuekey

Page 33: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JOVIAL 0 0 1

SHY 1 0 0

HOSTILE 0 1 0

FATIGUE 0 0 0

(car/setbit “SHY” 2 1)

tweet-idkey

valuekey

Page 34: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

SHY 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0

FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

(car/setbit “JOVIAL” 15 1)

tweet-idkey

valuekey

Page 35: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

SHY 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0

FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

(car/bitcount [*])

8134

valuekey

Page 36: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

(car/expire “ENGLAND&JOVIALITY" 10)

(car/bitcount “ENGLAND&JOVIALITY"))

How many people in England are Happy?

Page 37: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

(car/expire “ENGLAND&JOVIALITY" 10)

(car/bitcount “ENGLAND&JOVIALITY"))

How many people in England are Happy?

Page 38: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

(car/expire “ENGLAND&JOVIALITY" 10)

(car/bitcount “ENGLAND&JOVIALITY"))

How many people in England are Happy?

Page 39: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

(car/expire “ENGLAND&JOVIALITY" 10)

(car/bitcount “ENGLAND&JOVIALITY"))

How many people in England are Happy?

Page 40: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

How many people in England are Happy?

Page 41: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

How many people in England are Happy?

Page 42: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

How many people in England are Happy?

Page 43: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

1

AND

How many people in England are Happy?

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

Page 44: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

0 1

AND

How many people in England are Happy?

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

Page 45: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

0 0 1

AND

How many people in England are Happy?

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

Page 46: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1

AND

How many people in England are Happy?

(car/bitop "AND" “ENGLAND&JOVIALITY" "ENGLAND" "JOVIALITY")

Page 47: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

ENG 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1

JOVIAL 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1

0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1

How many people in England are Happy?

(car/bitcount “ENGLAND&JOVIALITY")

4

Page 48: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "OR" "FATIGUE|HOSTILITY" "FATIGUE" “HOSTILITY")

(car/expire "FATIGUE|HOSTILITY" 10)

(car/bitop "AND" "SCOTLAND&(FATIGUE|HOSTILITY)"

"SCOTLAND" "FATIGUE|HOSTILITY")

(car/expire "SCOTLAND&(FATIGUE|HOSTILITY)" 10)

(car/bitcount "SCOTLAND&(FATIGUE|HOSTILITY)"))

How many people in Scotland are Tired and Grumpy?

Page 49: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "OR" "FATIGUE|HOSTILITY" "FATIGUE" “HOSTILITY")

(car/expire "FATIGUE|HOSTILITY" 10)

(car/bitop "AND" "SCOTLAND&(FATIGUE|HOSTILITY)"

"SCOTLAND" "FATIGUE|HOSTILITY")

(car/expire "SCOTLAND&(FATIGUE|HOSTILITY)" 10)

(car/bitcount "SCOTLAND&(FATIGUE|HOSTILITY)"))

How many people in Scotland are Tired and Grumpy?

Page 50: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "OR" "FATIGUE|HOSTILITY" "FATIGUE" “HOSTILITY")

(car/expire "FATIGUE|HOSTILITY" 10)

(car/bitop "AND" "SCOTLAND&(FATIGUE|HOSTILITY)"

"SCOTLAND" "FATIGUE|HOSTILITY")

(car/expire "SCOTLAND&(FATIGUE|HOSTILITY)" 10)

(car/bitcount "SCOTLAND&(FATIGUE|HOSTILITY)"))

How many people in Scotland are Tired and Grumpy?

Page 51: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(wcar*

(car/bitop "OR" "FATIGUE|HOSTILITY" "FATIGUE" “HOSTILITY")

(car/expire "FATIGUE|HOSTILITY" 10)

(car/bitop "AND" "SCOTLAND&(FATIGUE|HOSTILITY)"

"SCOTLAND" "FATIGUE|HOSTILITY")

(car/expire "SCOTLAND&(FATIGUE|HOSTILITY)" 10)

(car/bitcount "SCOTLAND&(FATIGUE|HOSTILITY)"))

How many people in Scotland are Tired and Grumpy?

Page 52: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

How many people in Scotland are Tired and Grumpy?

Page 53: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0

How many people in Scotland are Tired and Grumpy?

Page 54: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

How many people in Scotland are Tired and Grumpy?

Page 55: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH

OR

How many people in Scotland are Tired and Grumpy?

Page 56: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0

OR

How many people in Scotland are Tired and Grumpy?

Page 57: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 1 0

OR

How many people in Scotland are Tired and Grumpy?

Page 58: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0 1 0

How many people in Scotland are Tired and Grumpy?

Page 59: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0

OR

How many people in Scotland are Tired and Grumpy?

Page 60: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0

OR

How many people in Scotland are Tired and Grumpy?

Page 61: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0

How many people in Scotland are Tired and Grumpy?

Page 62: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0

AND

How many people in Scotland are Tired and Grumpy?

Page 63: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

SCT 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 00 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0

HOSTILE 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0FATIGUE 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0

BOTH 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0

How many people in Scotland are Tired and Grumpy?

2

Page 64: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

Page 65: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

adamwynne/twitter-api

Page 66: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

clojurewerkz/meltdown

LMAX-Exchange/disruptor

Page 67: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

dakrone/clojure-opennlp

Page 68: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

“I’m loving #EuroClojure! ”

Page 69: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Positive Affect: enthusiastic, active, alert !

Negative Affect: subjective distress !

Emerged as distinctive, orthogonal dimensions

Page 70: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

PANAS

1

23

4

51

3153

2335345221

Page 71: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

PANAS

1

23

4

51

3153

2335345221

1 3 5 9 10 12 14 16 17 19

2 4 6 7 8 11 13 15 18 20

Page 72: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

PANAS

1

2345

1

31

53

233534

5 22 1

1 3 5 9 10 12 14 16 17 19

2 4 6 7 8 11 13 15 18 20

39 19

positive affect! negative affect

Page 73: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

PANAS-xGeneral Dimension Scales!Negative Affect (10) Positive Affect (10)

Basic Positive Emotions Scales!Joviality (8) Self-assurance (6) Attentiveness (4)

Basic Negative Emotions Scales!Fear (6) Hostility (6) Guilt (6) Sadness (5)

Other Affective States!Shyness (4) Fatigue (4) Serenity (3) Surprise (3)

Page 74: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

PANAS-t

Accounts for bias on social media!

Outlines sanitisation

Validate against 10 real events

Page 76: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

SHYNESS FATIGUE

SERENITY SURPRISE

FEAR HOSTILITY

GUILT SADNESS

JOVIALITY SELF ASSURANCE ATTENTIVENESS

We have sentiment key

s!

Page 77: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

mbostock/d3

Page 78: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

[-75.14310264, 40.05701649]

Page 79: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Reverse Geocoding Issues• Don’t want external services

• Don’t want heavy IO

• Don’t want round trips to the database

• Accuracy not too much of a concern

Page 80: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 81: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 82: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Equirectangular Projection

Page 83: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Courtesy of Eric Gaba (Wikimedia Commons User: Sting)

Page 84: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x = λcosφ1 y = φ

Tissot's indicatrix of deformation

Courtesy of Eric Gaba (Wikimedia Commons User: Sting)

Page 85: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 86: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

Page 87: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

[lon, lat] = [55.86140, -4.26477]

[x, y] = [329, 601]

[x, y] = = Scotland!

Page 88: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

[lon, lat] = [55.86140, -4.26477]

[x, y] = [329, 601]

[x, y] = = Scotland!

Page 89: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

[lon, lat] = [55.86140, -4.26477]

[x, y] = [329, 601]

[x, y] = = Scotland!

Page 90: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

[lon, lat] = [55.86140, -4.26477]

[x, y] = [329, 601]

[x, y] = = Scotland!

Page 91: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

[lon, lat] = [55.86140, -4.26477]

[x, y] = [329, 601]

[x, y] = = Scotland!

Page 92: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

x (px)

y (px)

[lon, lat] = [55.86140, -4.26477]

[x, y] = [329, 601]

[x, y] = = Scotland!

Page 93: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Server side! (JavaFX)

Mike Bostock & Nick Rabinowitz

Page 94: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [hiccup.page :as hiccup]) (:import (javafx.scene.web WebView)) !(def scripts ["uk-data.js" "topojson.v1.min.js" "d3.v3.min.js" "geocoder.js"]) !(defn create-browser [html] (-> WebView. .getEngine (.loadContent html))) !(defn build-page [script-paths] (hiccup/html5 [:head (for [path script-paths] [:script (slurp (io/resource path))])])) !(defn coordinates->name [longitude latitude] (let [browser (-> scripts build-page create-browser)] (.executeScript browser (str "geo.decode([" longitude "," latitude "])"))))

JavaFX

Page 95: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [hiccup.page :as hiccup]) (:import (javafx.scene.web WebView)) !(def scripts ["uk-data.js" "topojson.v1.min.js" "d3.v3.min.js" "geocoder.js"]) !(defn create-browser [html] (-> WebView. .getEngine (.loadContent html))) !(defn build-page [script-paths] (hiccup/html5 [:head (for [path script-paths] [:script (slurp (io/resource path))])])) !(defn coordinates->name [longitude latitude] (let [browser (-> scripts build-page create-browser)] (.executeScript browser (str "geo.decode([" longitude "," latitude "])"))))

JavaFX

Page 96: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [hiccup.page :as hiccup]) (:import (javafx.scene.web WebView)) !(def scripts ["uk-data.js" "topojson.v1.min.js" "d3.v3.min.js" "geocoder.js"]) !(defn create-browser [html] (-> WebView. .getEngine (.loadContent html))) !(defn build-page [script-paths] (hiccup/html5 [:head (for [path script-paths] [:script (slurp (io/resource path))])])) !(defn coordinates->name [longitude latitude] (let [browser (-> scripts build-page create-browser)] (.executeScript browser (str "geo.decode([" longitude "," latitude "])"))))

JavaFX

Page 97: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [hiccup.page :as hiccup]) (:import (javafx.scene.web WebView)) !(def scripts ["uk-data.js" "topojson.v1.min.js" "d3.v3.min.js" "geocoder.js"]) !(defn create-browser [html] (-> WebView. .getEngine (.loadContent html))) !(defn build-page [script-paths] (hiccup/html5 [:head (for [path script-paths] [:script (slurp (io/resource path))])])) !(defn coordinates->name [longitude latitude] (let [browser (-> scripts build-page create-browser)] (.executeScript browser (str "geo.decode([" longitude "," latitude "])"))))

JavaFX

Page 98: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [hiccup.page :as hiccup]) (:import (javafx.scene.web WebView)) !(def scripts ["uk-data.js" "topojson.v1.min.js" "d3.v3.min.js" "geocoder.js"]) !(defn create-browser [html] (-> WebView. .getEngine (.loadContent html))) !(defn build-page [script-paths] (hiccup/html5 [:head (for [path script-paths] [:script (slurp (io/resource path))])])) !(defn coordinates->name [longitude latitude] (let [browser (-> scripts build-page create-browser)] (.executeScript browser (str "geo.decode([" longitude "," latitude "])"))))

Stuart Sierras

Component lib!!

JavaFX

Page 99: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

KirinDave/clj-time

Page 100: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

“Thu Jun 26 14:35:57 +0000 2014”

over 86 thousand seconds in a dayover 31 million seconds in a year

Page 101: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 102: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 103: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

mm:260620141435

Page 104: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

HH:260620141400

Page 105: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 27 28 29

30 31

dd:260620140000

Page 106: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

JAN FEB MAR APR

MAY JUL AUG

SEP OCT NOV DEC

MM:010620140000

Page 107: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

2013

2015

yy:010120140000

Page 108: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [clj-time.format :as time]) !

!

(def input-formatter "EE MMM d HH:mm:ss Z yyyy") (def formats { "mm:" "ddMMyyHHmm" "HH:" "ddMMyyHH00" ... }) !

!

(defn create-key [date [prefix formatter]] (let [out-date (time/unparse formatter date)] (str prefix out-date))) !

!

(defn date->keys [created_at] (let [date (time/parse input-formatter created_at)] (map #(create-key date %) formats)))

Page 109: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [clj-time.format :as time]) !

!

(def input-formatter "EE MMM d HH:mm:ss Z yyyy") (def formats { "mm:" "ddMMyyHHmm" "HH:" "ddMMyyHH00" ... }) !

!

(defn create-key [date [prefix formatter]] (let [out-date (time/unparse formatter date)] (str prefix out-date))) !

!

(defn date->keys [created_at] (let [date (time/parse input-formatter created_at)] (map #(create-key date %) formats)))

Page 110: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [clj-time.format :as time]) !

!

(def input-formatter "EE MMM d HH:mm:ss Z yyyy") (def formats { "mm:" "ddMMyyHHmm" "HH:" "ddMMyyHH00" ... }) !

!

(defn create-key [date [prefix formatter]] (let [out-date (time/unparse formatter date)] (str prefix out-date))) !

!

(defn date->keys [created_at] (let [date (time/parse input-formatter created_at)] (map #(create-key date %) formats)))

Page 111: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

RINGSyncing

Journaling

Business logic

What

Where

When

Mem

ory

Web

API

Incoming Data

ptaoussanis/carmine

Page 112: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

SENTIMENT !

LOCATION !

mm:date-time HH:date-time dd:date-time MM:date-time yy:date-time

We have 11 keys in total!

What

Where

When

Page 113: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as redis]) !

!

(defn create-tweet-id [] (redis/incr "global:tweet.id")) !

!

(defn set-bits [data-keys tweet-id] (doseq [data-key data-keys] (redis/setbit data-key tweet-id 1))) !

!

(defn data->analytics [data-keys] (let [tweet-id (create-tweet-id)] (set-bits data-keys tweet-id)))

Page 114: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as redis]) !

!

(defn create-tweet-id [] (redis/incr "global:tweet.id")) !

!

(defn set-bits [data-keys tweet-id] (doseq [data-key data-keys] (redis/setbit data-key tweet-id 1))) !

!

(defn data->analytics [data-keys] (let [tweet-id (create-tweet-id)] (set-bits data-keys tweet-id)))

Page 115: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(:require [taoensso.carmine :as redis]) !

!

(defn create-tweet-id [] (redis/incr "global:tweet.id")) !

!

(defn set-bits [data-keys tweet-id] (doseq [data-key data-keys] (redis/setbit data-key tweet-id 1))) !

!

(defn data->analytics [data-keys] (let [tweet-id (create-tweet-id)] (set-bits data-keys tweet-id)))

Page 116: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

{ 'query-type': 'count', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

weavejester/compojure

JSON query

Page 117: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

{ 'query-type': 'count', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

weavejester/compojure

JSON query

/api?_query={json}

Page 118: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(defmulti execute :query-type) !!(defmethod execute “count" [] … logic here) !!(defmethod execute “p-affect“ [] … logic here) !!(defmethod execute “n-affect“ [] … logic here)

{ 'query-type': 'count', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

weavejester/compojure

JSON query

/api?_query={json}

Page 119: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(defmulti execute :query-type) !!(defmethod execute “count" [] … logic here) !!(defmethod execute “p-affect“ [] … logic here) !!(defmethod execute “n-affect“ [] … logic here)

{ 'query-type': 'count', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

weavejester/compojure

JSON query

/api?_query={json}

Page 120: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(defmulti execute :query-type) !!(defmethod execute “count" [] … logic here) !!(defmethod execute “p-affect“ [] … logic here) !!(defmethod execute “n-affect“ [] … logic here)

weavejester/compojure

JSON query

/api?_query={json}

{ 'query-type': 'count', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

Page 121: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(defmulti execute :query-type) !!(defmethod execute “count" [] … logic here) !!(defmethod execute “p-affect“ [] … logic here) !!(defmethod execute “n-affect“ [] … logic here)

weavejester/compojure

JSON query

/api?_query={json}

{ 'query-type': ‘p-affect', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

Page 122: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

(defmulti execute :query-type) !!(defmethod execute “count" [] … logic here) !!(defmethod execute “p-affect“ [] … logic here) !!(defmethod execute “n-affect“ [] … logic here)

weavejester/compojure

JSON query

/api?_query={json}

{ 'query-type': ‘p-affect', 'params': { 'sentiments': [ 'FATIGUE', 'HOSTILITY' ], 'locations': [ 'SCT' ] } }

Page 123: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)
Page 124: Using Clojure for Sentiment Analysis of the Twittersphere (EuroClojure 2014)

The code for this project is about to be open sourced

!

We would love to have more contributors! !

To find out more, follow: #APassionForCode