Skip to main content

Free easy Twitter sentiment analysis using R console (r-studio)in ubuntu(linux)

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
   

Comments

Popular posts from this blog

how to get hive table size from metastore mysql

select    d.name  as db_name ,t.tbl_name     as tbl_name ,from_unixtime(min(t.create_time))   as create_time ,min(t.owner)          as owner ,min(case when tp.param_key = 'COLUMN_STATS_ACCURATE'  then tp.param_value                 end) as COLUMN_STATS_ACCURATE ,min(case when tp.param_key = 'last_modified_by'       then tp.param_value                 end) as last_modified_by ,min(case when tp.param_key = 'last_modified_time'     then from_unixtime(tp.param_value)  end) as last_modified_time  ,min(case when tp.param_key = 'numFiles'               then tp.param_value                 end) as numFiles ,min(case when tp.param_key = 'numRows'                th...

Hadoop Yarn MR(MapReduce) streaming using Shell script part 2

Friends, This is a streaming MapReduce job (shell script) that reads any text input and computes the average length of all words that start with each character . --------------------------------------------------------------------------------------------------------------------------------------------------------------- $ cat avg_ln_mpr.sh #! /bin/bash while  read  line do  for word in `echo $line`  do     c=`expr substr $word 1 1`     l=`expr length $word`     echo $c $l  done     done --------------------------------------------------------------------------------------------------------------------------------------------------------------- $ cat avg_ln_rdr.sh #! /bin/bash old='' new='' val='' cnt=1 sum=0 avg=0 start=0 while  read  line do new=`echo $line|cut -d' ' -f1` val=`echo $line|cut -d' ' -f2` if [ "$old" != "$new" ]; then [ $start -ne 0 ] &...

MySQL replication - Master Slave Easy way with Crash test sample

I expect we have Master and Slave machines having MySQL installed  on both with server-id as 1 and 2 on Master and Slave . Mysql Replication steps: On Master: stop all transactions. mysql> FLUSH TABLES WITH READ LOCK; mysql> show master status ; +---------------+----------+--------------+------------------+ | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | binlog.000005 |  4913710 |              |                  | +---------------+----------+--------------+------------------+ take mysql dump of Master $ mysqldump -u root -p --all-databases --master-data > dbdump.sql mysql> unlock tables; transfer dump file  to slave host scp dbdump.sql  usr@slave:/tmp/ On Slave: [usr@slave ~]$ ls -ltr -rwx------ 1 usr usr 57319214 Nov  6 06:06 dbdump.sql...