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)
## [1] "Rate limited .... blocking for a minute and retrying up to 119 times ..."
## [1] "Rate limited .... blocking for a minute and retrying up to 118 times ..."
#tweets favorite Count
tweets[[1]]$favoriteCount
## [1] 0
#retweet Count
tweets[[1]]$retweetCount
## [1] 74
library(twitteR)
library(tm)
## Loading required package: NLP
library(wordcloud)
## Loading required package: RColorBrewer
library(stringr)
class(tweets)
## [1] "list"
#convert tweets to tweets.df
tweets.df = twListToDF(tweets)
tweets.df$text <- sapply(tweets.df$text,function(x) 
  iconv(x,to='UTF-8'))
#CLEANING TEXT
nohandles <- str_replace_all(tweets.df$text, "@\\w+", " ")
nohandles$cleanedText <- gsub("http.*", " ", nohandles)
## Warning in nohandles$cleanedText <- gsub("http.*", " ", nohandles): Coercing LHS
## to a list
nohandles$cleanedText <- gsub("https.*", " ", nohandles$cleanedText )
head(nohandles$cleanedText)
## [1] "RT  : Reply with your choice + #EXO #엑소 +   \n\n🎯300 replies in 50 minutes  "                                                    
## [2] "RT  : It's been exactly 2 years since Xiumin's 2nd STATION single \"You\" was released\n\n  #Xiumin  "                             
## [3] "  WE NOMINATE #XIUMIN OF #EXO \n FOR #TBworld2021 #100MostHandsomeMen2021    \n.69"                                                
## [4] "RT  : It's been exactly 2 years since Xiumin's 2nd STATION single \"You\" was released\n\n  #Xiumin  "                             
## [5] "RT  : #CHEN , #KYUNGSOO ,#SUHO &amp; #XIUMIN are all trending in Twitter Top Trends🎉🎉🎉🎉\n\n  #EXO"                                 
## [6] "RT  : raise ur standards people, look how he's so fine without efforts\n\nI nominate #KYUNGSOO of #weareoneEXO for #100MostHandso…"
nohandles$cleanedText <- str_replace_all(nohandles$cleanedText, "[^[:alnum:]]", " ")
nohandles$cleanedText <- str_replace_all(nohandles$cleanedText,"[[^a-zA-Z0-9]]", " ")
head(nohandles$cleanedText)
## [1] "RT    Reply with your choice    EXO            300 replies in 50 minutes  "                                                      
## [2] "RT    It s been exactly 2 years since Xiumin s 2nd STATION single  You  was released     Xiumin  "                               
## [3] "  WE NOMINATE  XIUMIN OF  EXO   FOR  TBworld2021  100MostHandsomeMen2021      69"                                                
## [4] "RT    It s been exactly 2 years since Xiumin s 2nd STATION single  You  was released     Xiumin  "                               
## [5] "RT     CHEN    KYUNGSOO   SUHO  amp   XIUMIN are all trending in Twitter Top Trends         EXO"                                 
## [6] "RT    raise ur standards people  look how he s so fine without efforts  I nominate  KYUNGSOO of  weareoneEXO for  100MostHandso "
wordCorpus <- Corpus(VectorSource(nohandles$cleanedText))
wordCorpus[[1]]$content
## [1] "RT    Reply with your choice    EXO            300 replies in 50 minutes  "
#removing punctuations
wordCorpus <- tm_map(wordCorpus, removePunctuation)wordCorpus[[1]]$content
wordCorpus <- tm_map(wordCorpus, removeNumbers)
wordCorpus <- tm_map(wordCorpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(wordCorpus, content_transformer(tolower)):
## transformation drops documents
wordCorpus[[1]]$content
## [1] "rt    reply with your choice    exo            300 replies in 50 minutes  "
# removing words such as “a”, “an”, “the”, “is”, etc
wordCorpus <- tm_map(wordCorpus, removeWords, stopwords("english"))
## Warning in tm_map.SimpleCorpus(wordCorpus, removeWords, stopwords("english")):
## transformation drops documents

`# wordCorpus <- tm_map(wordCorpus, removeWords, stopwords(“SMART”)) wordCorpus[[1]]$content``



```r
# manual removing of words that cannot be read
wordCorpus <- tm_map(wordCorpus, removeWords, c("amp"))
## Warning in tm_map.SimpleCorpus(wordCorpus, removeWords, c("amp")):
## transformation drops documents
wordCorpus <- tm_map(wordCorpus, removeWords, c("ste"))
## Warning in tm_map.SimpleCorpus(wordCorpus, removeWords, c("ste")):
## transformation drops documents
wordCorpus[[5]]$content
## [1] "rt     chen    kyungsoo   suho     xiumin   trending  twitter top trends         exo"
wordCorpus <- tm_map(wordCorpus, stripWhitespace)
## Warning in tm_map.SimpleCorpus(wordCorpus, stripWhitespace): transformation
## drops documents
wordCorpus[[1]]$content
## [1] "rt reply choice exo 300 replies 50 minutes "
pal <- brewer.pal(9,"YlGnBu")
pal <- pal[-(1:4)]
set.seed(123)
par(mfrow = c(1,1))
str(wordCorpus)
wordcloud(words = wordCorpus, scale=c(3,0.3), max.words=300, random.order=FALSE, 
          rot.per=0.5, use.r.layout=FALSE, colors = brewer.pal(8,"Dark2"))