Twitter sentiment analysis using r-studio (r console) in ubuntu
1) login as user
su
2) Download all files from following link:
https://drive.google.com/folderview?id=0B1WeP8XHW0OzcEY2TEtwMlZDTmc&usp=sharing#list
keep these files in /home/hduser/sentiment ( sample path)
positive-words.txt
negative-words.txt
sentiment.r
3) pre-requisites
open /etc/apt/sources.list and add
deb http://<my.favorite.cran.mirror>/bin/linux/ubuntu raring/
sudo apt-get install r-base
-- now we have R Console in ubuntu.
$ cd /home/hduser/sentiment/
call "R" from command prompt
$ R
-- Sett working directory(wd) and libraries required for analysis.
setwd("/home/hduser/sentiment")
install.packages('twitteR')
install.packages("ROAuth")
install.packages("RCurl")
install.packages("plyr")
install.packages("stringr")
-- it's not required since we have this file. if u want u can download it by issuing following command
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
--- authorization and registering. create account/app in twitter using link below.
-- https://dev.twitter.com/apps/new
--- pass consumerkey and secret key
cred <- OAuthFactory$new(consumerKey="dxhN2ylcdUPm1xUC64Q",consumerSecret="2TLzWOHQoEj1PE52U2uq6RwpWUe5U77MyvyVSpuYw",requestURL="https://api.twitter.com/oauth/request_token",accessURL="https://api.twitter.com/oauth/access_token",authURL="https://api.twitter.com/oauth/authorize")
cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
--- this will ask to to type PIN . Type it and continue.
> registerTwitterOAuth(cred)
[1] TRUE
--- Now we are connected with twitter. Now we can continue testing sentiment analysis on twitter.
----------------------------------
Testing- twitter analysis
-----------------------
library("ROAuth")
library("twitteR")
library("RCurl")
library("plyr")
library("stringr")
> tweets = searchTwitter("#abortion",n=100)
> length(tweets)
tweets.text = laply(tweets,function(t)t$getText())
pos = scan('positive-words.txt',what='character',comment.char=';')
neg = scan('negative-words.txt',what='character',comment.char=';')
source('/home/hduser/sentiment/sentiment.r')
analysis = score.sentiment(tweets.text, pos, neg)
table(analysis$score)
mean(analysis$score)
hist(analysis$score)
reference url :
http://cran.rstudio.com/
Note:
Sometimes it doesn´t work because there are some tweets with invalid characters in it. Then you have to do the data mining again or change the keyword. As soon an update is available I will update this article.
About these ads
1) login as user
su
2) Download all files from following link:
https://drive.google.com/folderview?id=0B1WeP8XHW0OzcEY2TEtwMlZDTmc&usp=sharing#list
keep these files in /home/hduser/sentiment ( sample path)
positive-words.txt
negative-words.txt
sentiment.r
3) pre-requisites
open /etc/apt/sources.list and add
deb http://<my.favorite.cran.mirror>/bin/linux/ubuntu raring/
sudo apt-get install r-base
-- now we have R Console in ubuntu.
$ cd /home/hduser/sentiment/
call "R" from command prompt
$ R
-- Sett working directory(wd) and libraries required for analysis.
setwd("/home/hduser/sentiment")
install.packages('twitteR')
install.packages("ROAuth")
install.packages("RCurl")
install.packages("plyr")
install.packages("stringr")
-- it's not required since we have this file. if u want u can download it by issuing following command
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
--- authorization and registering. create account/app in twitter using link below.
-- https://dev.twitter.com/apps/new
--- pass consumerkey and secret key
cred <- OAuthFactory$new(consumerKey="dxhN2ylcdUPm1xUC64Q",consumerSecret="2TLzWOHQoEj1PE52U2uq6RwpWUe5U77MyvyVSpuYw",requestURL="https://api.twitter.com/oauth/request_token",accessURL="https://api.twitter.com/oauth/access_token",authURL="https://api.twitter.com/oauth/authorize")
cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") )
--- this will ask to to type PIN . Type it and continue.
> registerTwitterOAuth(cred)
[1] TRUE
--- Now we are connected with twitter. Now we can continue testing sentiment analysis on twitter.
----------------------------------
Testing- twitter analysis
-----------------------
library("ROAuth")
library("twitteR")
library("RCurl")
library("plyr")
library("stringr")
> tweets = searchTwitter("#abortion",n=100)
> length(tweets)
tweets.text = laply(tweets,function(t)t$getText())
pos = scan('positive-words.txt',what='character',comment.char=';')
neg = scan('negative-words.txt',what='character',comment.char=';')
source('/home/hduser/sentiment/sentiment.r')
analysis = score.sentiment(tweets.text, pos, neg)
table(analysis$score)
mean(analysis$score)
hist(analysis$score)
reference url :
http://cran.rstudio.com/
Note:
Sometimes it doesn´t work because there are some tweets with invalid characters in it. Then you have to do the data mining again or change the keyword. As soon an update is available I will update this article.
About these ads
Comments
Post a Comment