Created
March 3, 2016 14:07
-
-
Save jwinternheimer/318ebf510e1534a51d9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(twitteR); library(wordcloud); library(tm); library(RColorBrewer); library(praise) | |
# Twitter Oauth | |
my_key <- "xxxxxxxxxxxxxxxxxxxxxxxxx" | |
my_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxx" | |
access_token <- "xxxxxxxxxxxxxxxxxxxxxxxxx" | |
access_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxx" | |
setup_twitter_oauth(my_key,my_secret) | |
# clean text function | |
clean.text <- function(some_txt) { | |
some_txt = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", some_txt) | |
some_txt = gsub("@\\w+", "", some_txt) | |
some_txt = gsub("[[:punct:]]", "", some_txt) | |
some_txt = gsub("[[:digit:]]", "", some_txt) | |
some_txt = gsub("http\\w+", "", some_txt) | |
some_txt = gsub("[ \t]{2,}", "", some_txt) | |
some_txt = gsub("^\\s+|\\s+$", "", some_txt) | |
some_txt = gsub("amp", "", some_txt) | |
# define "tolower error handling" function | |
try.tolower = function(x) { | |
y = NA | |
try_error = tryCatch(tolower(x), error=function(e) e) | |
if (!inherits(try_error, "error")) | |
y = tolower(x) | |
return(y) | |
} | |
some_txt = sapply(some_txt, try.tolower) | |
some_txt = some_txt[some_txt != ""] | |
names(some_txt) = NULL | |
return(some_txt) | |
} | |
# get tweets | |
tweets = searchTwitter("#bufferchat", 500, lang="en") | |
# get text | |
tweet_txt = sapply(tweets, function(x) x$getText()) | |
tweet_txt = iconv(tweet_txt,to="utf-8-mac") | |
# create text corpus | |
text_corpus = Corpus(VectorSource(tweet_txt)) | |
# create document term matrix | |
terms <- tm_map(text_corpus, removeWords, stopwords('english')) | |
tdm = TermDocumentMatrix(terms) | |
# define tdm as matrix | |
m = as.matrix(tdm) | |
# get word counts in decreasing order | |
word_freqs = sort(rowSums(m), decreasing=TRUE) | |
# create a data frame with words and their frequencies | |
dm = data.frame(word=names(word_freqs), freq=word_freqs) | |
dm$word <- gsub("[[:punct:]]", "", dm$word) | |
# plot and save wordcloud | |
png("~/Rrobot/tweeting/bufferchat_cloud.png", 800, 800, res = 200) | |
wordcloud(dm$word, dm$freq, random.order=FALSE, rot.per=.15, colors=brewer.pal(8, "Dark2"), | |
vfont=c("sans serif","bold"),max.words = 200) | |
dev.off() | |
# Tweet | |
tweettxt <- praise("${Exclamation}!") | |
tweet(paste(tweettxt,"A #bufferchat wordcloud! #Rbot"), mediaPath = "~/Rrobot/tweeting/bufferchat_cloud.png") | |
line <- paste(as.character(Sys.time()), tweettxt, sep="\t") | |
write(line, file="tweets.log", append=TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, never used R but I think this is the occasion.
I can run R directly from NodeJS https://www.npmjs.com/package/rstats