setwd("E:/gaurav_back up/Spring 2018/Data_Mining_Professor_M_Yousuf/Project/SentimentAnalysis for twitter") #https://cran.r-project.org/web/packages/available_packages_by_date.html
library(twitteR) library(ROAuth) library(plyr) library(dplyr) library(stringr) library(ggplot2) library(httr) library(wordcloud) library(sentiment) library(RCurl) library(syuzhet) library(tm)
oauth_endpoint(authorize="https://api.twitter.com/oauth",
download.file(url='http://curl.haxx.se/ca/cacert.pem',destfile='cacert.pem')
reqURL <- 'https://api.twitter.com/oauth/request_token'
accessURL <-'https://api.twitter.com/oauth/access_token'
authURL <- 'https://api.twitter.com/oauth/authorize'
consumerKey <-'6ZRHBYyhBmCFaGw4KbwxNrmLR' consumerSecret <-'kwmV62c3a2pCLB3aG3EmXH3DZnB9n0SxsUat58QDCaL0DUEMEN' accesstoken <-'147268830-yPBIvYslwYQ38BpVK7g92QkiMIgTK4kYSMRLTlwh' accesssecret <-'xXkKqxgwigpaKD5LxmIFG1IwRRCTchOcvASK2Hg73h9tR'
#Cred <- OAuthFactory$new(consumerKey=consumerKey,
#Cred$handshake(cainfo =system.file('CurlSSL','cacert.pem',package='RCurl'))
#save(Cred,file='twitter authentication.Rdata') #load('twitter authentication.Rdata')
setup_twitter_oauth(consumerKey,consumerSecret,accesstoken,accesssecret)
#test the connection some_tweets= searchTwitter("pakistan",n=4000,since="2017-09-01",lang="en")
#retweets(952404098992803000,100)
length.some_tweets <- length(some_tweets)
some_tweets.df <- ldply(some_tweets,function(x) x$toDataFrame()) #class(some_tweets.df)
write.csv(some_tweets.df,"tweets.csv")
some_txt = sapply(some_tweets,function(x) x$getText())
some_txt1 = gsub("(RT|via)((?:\b\W*@\w+)+)","",some_txt)
some_txt2 =gsub("http[^[:blank:]]+","",some_txt1)
some_txt3 = gsub("@\w+","",some_txt2)
some_txt4 = gsub("[[:punct:]]"," ",some_txt3)
some_txt5 = gsub( "[^[:alnum:]]"," ",some_txt4)
write.csv(some_txt5,"tweets1.csv")
some_txt6 <- Corpus(VectorSource(some_txt5))
some_txt6 <- tm_map(some_txt6,removePunctuation) some_txt6 <- tm_map(some_txt6,content_transformer(tolower))
some_txt6 <- tm_map(some_txt6,removeWords,stopwords("english")) some_txt6 <- tm_map(some_txt6,stripWhitespace)
pal <- brewer.pal(8,"Dark2") x11() wordcloud(some_txt6, min_frq=5,max.words= 500,width=1000,height=1000,random.order=FALSE,color=pal)
#syuzhet library has get_nrc_sentiment function defined,and if we pass any corpus/sentence to it. It will give us the sentiments.
get_nrc_sentiment(" iphone is waste device. it is bad")
remember to pass some_txt5 and not some_txt6( it is a corpus, a bunch of words with no sense) but the sentiments are also defiend by the semanti
#cs as well. mysentiment <- get_nrc_sentiment(some_txt5)
#fix(mysentiment) SentimentScores <- data.frame(colSums(mysentiment[,])) fix(SentimentScores) names(SentimentScores) <- "Score"
SentimentScores <- cbind("sentiment" = rownames(SentimentScores),SentimentScores)
rownames(SentimentScores) <- NULL
ggplot(data=SentimentScores, aes(x=sentiment, y=Score)) + geom_bar(aes(fill=sentiment),stat="identity") + theme(legend.position="none") + xlab("Sentiment") + ylab("Score") + ggtitle("Total Sentiment Score Based on Tweets")