library(twitteR)
library(tm)
## Loading required package: NLP
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
##
## annotate
library(stringr)
library(RColorBrewer)
library(syuzhet)
library(twitteR)
#set Credentials
CONSUMER_SECRET <- "G39QeG1hxTn5lcxbVw5JOXGk0JD8diMIm1JWfkeKDCYV8MCvdo"
CONSUMER_KEY <- "25ZBYYAm2VIWomrnKgEFrfizf"
ACCESS_SECRET <- "7L6RSbD5QltdQIJbH6HYCrkM8du0QuqOhAs3DxNDv9kDD"
ACCESS_TOKEN <- "1391946408710664193-WCBR3AB4FwYBAVPggINbSDOeTKOTLm"
#connect to twitter app
setup_twitter_oauth(consumer_key = CONSUMER_KEY,
consumer_secret = CONSUMER_SECRET,
access_token = ACCESS_TOKEN,
access_secret = ACCESS_SECRET)
## [1] "Using direct authentication"
#GET TWITTER USER
twitterUser <- getUser("weareoneEXO")
tweets <- searchTwitter("weareoneEXO", n = 5000,
since = "2021-05-03",
until ="2021-05-09",
lang ="en",
retryOnRateLimit = 120)
#convert tweets to tweets.df
tweets.df = twListToDF(tweets)
#extract timeline tweets
extractTimelineTweets <- function(username,tweetCount){
# timeline tweets
twitterUser <- getUser(username)
tweets = userTimeline(twitterUser,n=tweetCount)
tweets.df = twListToDF(tweets)
issue.df$text <- sapply(tweets.df$text,function(x) iconv(enc2utf8(x), sub="byte"))
return(tweeets.df)
}
encodeSentiment <- function(x) {
if(x <= -0.5){
"1) very negative"
}else if(x > -0.5 & x < 0){
"2) negative"
}else if(x > 0 & x < 0.5){
"4) positive"
}else if(x >= 0.5){
"5) very positive"
}else {
"3) neutral"
}
}
#sentiment analysis
tweetSentiments <- get_sentiment (tweets.df$text,method = "syuzhet")
tweets <- cbind(tweets.df, tweetSentiments)
tweets$sentiment <- sapply(tweets$tweetSentiments,encodeSentiment)
head(tweets, n = 1)
## text
## 1 RT @EXOBBS50_CHARTS: Reply with your choice + #EXO #엑소 + @weareoneEXO \n\n🎯300 replies in 50 minutes https://t.co/7vTKKwBpoP
## favorited favoriteCount replyToSN created truncated replyToSID
## 1 FALSE 0 <NA> 2021-05-08 23:59:58 FALSE <NA>
## id replyToUID
## 1 1391181109707292674 <NA>
## statusSource
## 1 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>
## screenName retweetCount isRetweet retweeted longitude latitude
## 1 effy_says17 74 TRUE FALSE NA NA
## tweetSentiments sentiment
## 1 0.4 4) positive
qplot(tweets$tweetSentiments) + theme(legend.position="none")+
xlab("Sentiment Score") +
ylab("Number of tweets") +
ggtitle("Tweets by Sentiment Score")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# NRC Sample (various emotions such as anger, fear, joy, ...)
tweetSentiments <- get_nrc_sentiment(tweets.df$text)
head(tweetSentiments)
## anger anticipation disgust fear joy sadness surprise trust negative positive
## 1 0 0 0 0 0 0 0 0 0 1
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0
## 5 0 1 0 0 0 0 0 1 0 1
## 6 0 0 0 0 0 0 0 0 0 0
tweets <- cbind(tweets.df, tweetSentiments)
tweets[c(1:5),c(1, 17:26)]
## text
## 1 RT @EXOBBS50_CHARTS: Reply with your choice + #EXO #엑소 + @weareoneEXO \n\n🎯300 replies in 50 minutes https://t.co/7vTKKwBpoP
## 2 RT @SMTownEngSub: It's been exactly 2 years since Xiumin's 2nd STATION single "You" was released\n\n@weareoneEXO #Xiumin https://t.co/0UgzJTw…
## 3 @mouna_exol WE NOMINATE #XIUMIN OF #EXO \n FOR #TBworld2021 #100MostHandsomeMen2021 @weareoneEXO \n.69
## 4 RT @SMTownEngSub: It's been exactly 2 years since Xiumin's 2nd STATION single "You" was released\n\n@weareoneEXO #Xiumin https://t.co/0UgzJTw…
## 5 RT @exobrand50: #CHEN , #KYUNGSOO ,#SUHO & #XIUMIN are all trending in Twitter Top Trends🎉🎉🎉🎉\n\n@weareoneEXO #EXO
## anger anticipation disgust fear joy sadness surprise trust negative positive
## 1 0 0 0 0 0 0 0 0 0 1
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0
## 5 0 1 0 0 0 0 0 1 0 1
sentimentTotals <- data.frame(colSums(tweets[,c(17:26)]))
names(sentimentTotals) <- "count"
sentimentTotals <- cbind("sentiment" = rownames(sentimentTotals), sentimentTotals)
rownames(sentimentTotals) <- NULL
sentimentTotals
## sentiment count
## 1 anger 370
## 2 anticipation 1065
## 3 disgust 101
## 4 fear 366
## 5 joy 1012
## 6 sadness 404
## 7 surprise 645
## 8 trust 1377
## 9 negative 537
## 10 positive 1998
ggplot(data = sentimentTotals, aes(x = sentiment, y = count)) +
geom_bar(aes(fill = sentiment), stat = "identity") +
theme(legend.position = "none") +
xlab("Sentiment") + ylab("Total Count") + ggtitle("Total Sentiment Score for All Tweets")
