library(twitteR)
library(tm)
## Loading required package: NLP
library(syuzhet)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
##
## annotate
consumer_key <-"3kViWlWuPQ9TMnhdDYn7hxmeb"
consumer_secret <- "hPZJmxD5Zhp3BCGPaQ9l1iPZOoEoL6Plmh82CuMlEAn6HkwqB0"
access_token <- "3150218942-XQN1lMkqzrHWafBySp4lWBbLyMs0qrLMUn6IPPB"
access_secret <- "OyRzypjlRrL5Tq8dgMn0BsrORkCr40fqS7kYKBWbv1J7M"
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)
## [1] "Using direct authentication"
tweets<-searchTwitter("Coronavirus",n=100,lang = "en")
dftweets<-twListToDF(tweets)
text<-dftweets$text
text<-tolower(text)
cleanText<-gsub("^rt","",text)
cleanText<-gsub("#","",cleanText)
cleanText<-gsub("@\\w+","",cleanText)
cleanText<-gsub("http.*","",cleanText)
cleanText<-gsub("[[:punct:]]","",cleanText)
cleanText<-gsub("^ ","",cleanText)
cleanText<-gsub(" $","",cleanText)
corpusList<-Corpus(VectorSource(cleanText))
listt<-tm_map(corpusList,function(x) removeWords(x,stopwords()))
## Warning in tm_map.SimpleCorpus(corpusList, function(x) removeWords(x,
## stopwords())): transformation drops documents
sentiments<-get_nrc_sentiment(as.character(listt))
## Warning: `filter_()` is deprecated as of dplyr 0.7.0.
## Please use `filter()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `data_frame()` is deprecated as of tibble 1.1.0.
## Please use `tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
sentiments
## anger anticipation disgust fear joy sadness surprise trust negative positive
## 1 27 22 12 39 19 27 14 34 50 55
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
data<-as.data.frame(colSums(sentiments))
names(data)<-"Score"
data<-cbind("Sentiments"= rownames(data),data)
rownames(data)<-NULL
data
## Sentiments Score
## 1 anger 27
## 2 anticipation 22
## 3 disgust 12
## 4 fear 39
## 5 joy 19
## 6 sadness 27
## 7 surprise 14
## 8 trust 34
## 9 negative 50
## 10 positive 55
g<-ggplot(data=data, aes(x=Sentiments,y=Score))
g<-g+geom_bar(aes(fill=Sentiments),stat="identity")
g<-g+ ggtitle("Sentimental Analysis about Corona Virus")
g<-g+theme(plot.title = element_text(hjust = 0.5), legend.position = "none")
print(g)