forked from jwillage/trumpbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.R
140 lines (115 loc) · 4.73 KB
/
start.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
library(twitteR)
library(dplyr)
library(tidytext)
library(ggplot2)
library(quanteda)
library(jsonlite)
creds <- readLines("creds.txt")
setup_twitter_oauth(creds[1], creds[2], creds[3], creds[4])
all_tweets <- userTimeline("realdonaldtrump", n = 3200, includeRts = F)
updateTweets <- function(all_tweets){
recent <- userTimeline("realdonaldtrump", n = 3200, includeRts = F, sinceID = all_tweets[[1]]$id)
all_tweets <- c(all_tweets, recent)
all_tweets
}
getArchive <- function(years) {
dfs <- lapply(years, function(x)
fromJSON(paste0("http://www.trumptwitterarchive.com/data/", x, ".json")))
bind_rows(dfs)
}
archived <- getArchive(2010:2016)
# http://varianceexplained.org/r/trump-tweets/ suggests iphone == trump, android == staff
# tweets from other sources appear to be in the style of trump
archived <- archived[archived$source != "Twitter for Android", ]
texts <- archived$text
#filter links
links <- grepl("http[s]*", texts)
texts[links] <- gsub("http[s]*://.*", "", texts[links])
tweets <- data.frame(text = texts, line = 1:length(texts), stringsAsFactors = FALSE)
tidy_tweets <- tweets %>% unnest_tokens(word, text)
#tidy_tweets <- tidy_tweets %>% anti_join(stop_words)
tidy_tweets %>% count(word, sort=T) %>%
anti_join(stop_words) %>%
filter(n > 200) %>% mutate(word=reorder(word, n)) %>%
ggplot(aes(word, n)) +
geom_bar(stat="identity") +
coord_flip()
sentiments <- tidy_tweets %>% inner_join(get_sentiments("nrc")) %>% count(word, sentiment, sort=T) %>% ungroup()
sentiments %>%
filter(n > 1, sentiment %in% c("positive", "negative")) %>%
mutate(n = ifelse(sentiment=="negative", -n, n)) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill=sentiment)) +
geom_bar(stat="identity") +
coord_flip()
# break down for NLP
tweet_sentences <- tweets %>% unnest_tokens(sent, text, token="sentences")
tweet_sentences$sent <- paste("zzstart", tweet_sentences$sent, "zzend")
uni_tweets <- tweet_sentences %>% unnest_tokens(word, sent, to_lower = FALSE)
unigrams <- uni_tweets %>% group_by(word) %>% count(sort=T)
unigrams$Freq <- unigrams$n/nrow(unigrams)
for (i in 1:nrow(tweet_sentences)) {
tryCatch(
x <- ngrams(tokenize(tweet_sentences$sent[i], concatenator = "_") )
,error = function(e)
print(i)
)
}
problems <- c(7012, 7339, 10810)
tt_bigrams <- buildTidytextModel(tweet_sentences, 2)
tt_trigrams <- buildTidytextModel(tweet_sentences, 3)
qt_bigrams <- buildQuantedaModel(tweet_sentences, 2, problems)
qt_trigrams <- buildQuantedaModel(tweet_sentences, 3, problems)
gen_tweet(tt_bigrams, tt_trigrams)
gen_tweet(qt_bigrams, qt_trigrams)
### tidytext's tokenization
buildTidytextModel <- function(tweet_sentences, n){
bi_tweets <- tweet_sentences %>% unnest_tokens(tok, sent, token="ngrams", n=n, to_lower = FALSE)
bigrams <- bi_tweets %>% group_by(tok) %>% count(sort=T)
bigrams$Freq <- bigrams$n/nrow(bigrams)
split <- strsplit(bigrams$tok, " ")
bigrams$idx <- sapply(split, function(x) paste(x[1:n - 1], collapse = " "))
bigrams$gram <- sapply(split, function(x) x[n])
# todo remove garbage ngrams (cont, ...)
# 'amp' == &
tryCatch({
bigrams[bigrams$gram == "amp",]$gram <- "&"
bigrams$idx <- gsub("amp", "&", bigrams$idx)
})
bigrams
}
# quanteda tokenization
buildQuantedaModel <- function(tweet_sentences, n, problems){
bi_tweets <- ngrams(tokenize(tweet_sentences$sent[-problems], concatenator = "_"), n)
bigrams <- data.frame(table(unlist(bi_tweets)), stringsAsFactors = F)
bigrams$Var1 <- as.character(bigrams$Var1)
split <- strsplit(bigrams$Var1, "_")
bigrams$idx <- sapply(split, function(x) paste(x[1:n - 1], collapse = " "))
bigrams$gram <- sapply(split, function(x) x[n])
bigrams
}
gen_tweet <- function(bigrams, trigrams) {
# init with start sentence
tweet <- list(start="zzstart")
l <- tweet[[1]]
noise <- rnorm(nrow(bigrams[bigrams$idx==l,]), mean(bigrams[bigrams$idx==l,]$Freq), 10*sd(bigrams[bigrams$idx==l,]$Freq))
tweet <- c(tweet,
bigrams[bigrams$idx == l, ][which.max(bigrams[bigrams$idx==l,]$Freq + noise), "gram"])
loop = TRUE
while(loop) {
l <- paste(tweet[[length(tweet)-1]], tweet[[length(tweet)]])
if (nrow(trigrams[trigrams$idx==l,]) > 1)
noise <- rnorm(nrow(trigrams[trigrams$idx==l,]), mean(trigrams[trigrams$idx==l,]$Freq), 10*sd(trigrams[trigrams$idx==l,]$Freq))
else
noise <- 0
tweet <- c(tweet,
trigrams[trigrams$idx == l, ][which.max(trigrams[trigrams$idx==l,]$Freq + noise), "gram"])
# [round(runif(1, 1, nrow(trigrams[trigrams$idx == l, ])), 0), "gram"])
# shouldnt necessarily stop on end of sentence
if (tweet[[length(tweet)]] == "zzend")
loop = FALSE
loop
}
tokens <- unlist(tweet)
paste(tokens[3:length(tokens)-1], collapse=" ")
}