library(twitteR)
setup_twitter_oauth("PlCtxAuNAfWdYwBjtdWk8NDAy","uNfrYZAkS8vQhD9GXOr43mIYM3LE6FDWzS4zQdPafAMLwSbVPw","118065065-hP0YCXBb86irtPuaEcBhJKIVxQDKDckZPxJv5oxK","gwrf2gXs9GgF3N3X0V3P7OfNjJtPYX8BaXV9F3TmIuhFs")
## [1] "Using direct authentication"
rdmTweets <- userTimeline("cia", n=500)
rdmTweets[1:3]
## [[1]]
## [1] "CIA: CIA #Museum Artifact of the Week: Afghan Hat\nA gift from Afghan President Karzai to former DCI George Tenet<U+0085> https://t.co/zUAZ9OqMQB"
##
## [[2]]
## [1] "CIA: ICYMI:\nNew Anthology: \nCIA & the Wars in Southeast Asia, 1947-75\n\n41 #unclassified articles & more!<U+0085> https://t.co/ukmH3tTIoY"
##
## [[3]]
## [1] "CIA: ICYMI:\nNew #Unclassified \"Studies in Intel\":\n-Intel for Warfighter\n-Why Bad Things Happen to Good Analysts\n-& more!<U+0085> https://t.co/pNPryg92u1"
df <- do.call("rbind", lapply(rdmTweets, as.data.frame))
library(tm)
## Loading required package: NLP
Corpus1=Corpus(VectorSource(df$text))
Corpus1 <- tm_map(Corpus1, removePunctuation)
Corpus1 <- tm_map(Corpus1, removeNumbers)
Corpus1 <- tm_map(Corpus1, tolower)
Corpus1 <- tm_map(Corpus1, removeWords, stopwords("english"))
Corpus1 <- tm_map(Corpus1, stemDocument)
Corpus1 <- tm_map(Corpus1, stripWhitespace)
Corpus1 <- tm_map(Corpus1, PlainTextDocument)
dtm <- DocumentTermMatrix(Corpus1)
tdm <- TermDocumentMatrix(Corpus1)
matx1=as.matrix(tdm)
sort1=sort(rowSums(matx1),decreasing=T)
di=data.frame(Word=names(sort1),Frequency=sort1)
library(wordcloud)
## Loading required package: RColorBrewer
wordcloud(di$Word, di$Frequency, max.words=100,colors=brewer.pal(6, "Set1"))

findFreqTerms(dtm, lowfreq=10)
## [1] "amp" "artifact" "cia" "icymi" "inmemoriam"
## [6] "intel" "intelcon" "museum" "oss" "pdb"
## [11] "week"
findAssocs(dtm, 'usa', 0.30)
## $usa
## numeric(0)