# Sentiment analysis of tweets

I want to use Mathematica for a sentiment analysis on tweet-messages. For that purpose i selected several tweets. Each tweet is interpreted by myself as positive (pos) of negative (neg). For example:

tweets = {{"Windows 10 why is It called windows 10 when there was no Windows 9?" , "neg"},
{"Windows 10 so called because its an OS leap forward of many times or just because some coders got lazy?" , "neg"},
{"did you hear why the new Windows is gonna be named Windows 10 no why because 7 ate 9", "pos"},
{"i think everyone is excited with the upcoming Windows 10 release", "Pos"},
{"Windows 10 Is a dirty gangsta that loves hoes", "neg"}}


To find a relation betweet the first string and de second I want to use de function Classify. Therefore I need to transforme the tweets.

tweets1 = (First@# -> Last@#) & /@ Transpose@{tweets[[1 ;;, 1]], tweets[[1 ;;, 2]]}


This gives the folowing output:

{"Windows 10 Why Is It Called Windows 10 When There Was No Windows > 9?" -> "neg", > > "Windows 10 so called because its an OS leap forward of many times or just because some coders got lazy?" -> "neg",
"Did you hear why the new Windows is gonna be named Windows 10 No,why Because 7 ate 9-> "pos",
"I think everyone is excited with the upcoming Windows 10 release" -> "Pos", "Windows 10 Is a dirty gangsta that loves hoes" -> "neg"}

Then I use Classify:

c = Classify[tweets1]

c["Windows 10 is trending with more than 168 thousand tweet ! We wonder if it's a positive buzz or a negative one"]


gives 'neg'.

I think it's better to do a analysis on the separated words (or combination of words) of each tweet. Therefore I write:

tweets3 = (First@# -> Last@#) & /@
Transpose@{StringCases[ToLowerCase[tweets2[[1 ;;, 1]]],
specials | RegularExpression["\\w(?<!\\d)[\\w'-]*"]],
tweets2[[1 ;;, 2]]}


The output is:

{{"windows", "why", "is", "it", "called", "windows", "when", "there", "was", "no", "windows"} -> "neg", {"windows", "so", "called", "because", "its", "an", "os", "leap", "forward", "of", "many", "times", "or", "just", "because", "some", "coders", "got", "lazy"} -> "neg", {"did", "you", "hear", "why", "the", "new", "windows", "is", "gonna", "be", "named", "windows", "no", "why", "because",
"ate"} -> "pos", {"i", "think", "everyone", "is", "excited", "with", "the", "upcoming", "windows", "release"} -> "Pos", {"windows", "is", "a", "dirty", "gangsta", "that", "loves",
"hoes"} -> "neg"}

Then I use Classify again:

c = Classify[tweets3]


Now I get a error: Classify::bftlgth: Examples should have the same number of features.

Anyone a suggestion how to perform a sentiment analysis on twitter-messages?

• Transpose@{tweets[[1 ;;, 1]], tweets[[1 ;;, 2]]} is the same as tweets. – Chip Hurst Oct 2 '14 at 16:17

FYI a more elegant way to get tweets1 is

tweets1 = Rule @@@ tweets;


Classify automatically separates words under the hood (via StringSplit), so you don't actually need to do that yourself.

Classify has a built in sentiment classifier:

Classify["Sentiment", "Windows 10 why is It called windows 10 when there was no Windows 9?"]

(* "Negative" *)

• Thanks Chip. To add to this answer: we're working on discrete NLP functionality for 10.1, which should also unlock better performance from Classify when applied to textual data. – Taliesin Beynon Nov 5 '14 at 2:36