-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_xb_get_tweets.R
193 lines (126 loc) · 5.13 KB
/
3_xb_get_tweets.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Goal: Crawling Trump's tweets and anlyzing his keywords
# library and global options ----------------------------------------------
install.packages('twitteR')
library(twitteR)
options(stringsAsFactors = F)
# Copy consumer keys and access_token -------------------------------------
# https://paper.dropbox.com/doc/R-twitterR-UYzYvP2cMSBGTbKMN34a2#:uid=547357062117335&h2=1.-Preparation
consumer_key <- ''
consumer_secret <- ''
access_token <- ''
access_secret <- ''
# Set up authentication ---------------------------------------------------
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
# Send query --------------------------------------------------------------
tweets <- userTimeline("realdonaldtrump", n = 3200)
# Combine results ---------------------------------------------------------
# Create df by the 1st tweet
df <- as.data.frame(tweets[[1]])
# Use rbind and for-loop to combine the 2nd and following tweets
for(i in 2:length(tweets)){
df.next <- as.data.frame(tweets[[i]])
df <- rbind(df, df.next)
print(i)
}
# A better way to combine all results
# df <- do.call("rbind", lapply(tweets, as.data.frame))
class(tweets[[1]])
test <- as.data.frame(tweets[[1]])
# Load stopwords ----------------------------------------------------------
library(tm) # for stopWords()
stopWords <- stopwords("en")
stopWords
# if you can not load tm library, you can source() the stopWords.RData I provides
getwd() # get current working directory
#setwd() # set current working directory
system("ls") # send system command ls
list.files(".") # list all files in current directory
list.files("RData") # list all files in RData directory
load("RData/stopWords.RData") # loading RData files
# Text preprocessing ------------------------------------------------------
# Load stopwords
library(tm) # for stopWords()
stopWords <- stopwords("en")
# if you can not load tm library, you can load() the stopWords.RData
load("RData/stopWords.RData") # loading RData files
# Split sentence by non-alphabetical characters
df$words <- strsplit(df$text, "[^a-zA-Z']+")
# convert character to lower case
df$words <- sapply(df$words, tolower)
# Calculate term frequency ------------------------------------------------
freq <- table(unlist(df$words)) # try to use dplyr::count()?
length(freq)
names(freq)
# Extract key terms from term list ----------------------------------------
# delete words in stopWords
freq <- freq[!(names(freq) %in% stopWords)]
length(freq)
# sort the frequency table
freq <- sort(freq, decreasing=T)
# eliminate words with low frequency <=3
freq2 <- freq[freq > 3]
# eliminate garbage words
freq3 <- freq2[!(names(freq2) %in% c('https', 'co', 't'))]
# eliminate word with only one letter
freq4 <- freq3[nchar(names(freq3))>1]
# Visualization -----------------------------------------------------------
# Barplot the words
freq4 <- subset(freq4, freq4>=5)
barplot(freq4[1:50], las=2, cex.names =1)
library(ggplot2)
freq4.df <- as.data.frame(freq4[1:50])
ggplot(freq4.df, aes(x=Var1, y=Freq)) +
geom_bar(stat="identity") +
xlab("Terms") + ylab("Count") +
coord_flip() +
theme(axis.text=element_text(size=7))
# Plot sentimental terms only ---------------------------------------------
# http://tidytextmining.com/sentiment.html
# Loading sentimet corpus
library(tidytext)
corpus <- get_sentiments("bing")
sentiment.freq <- freq4[names(freq4) %in% unlist(corpus$word)]
sentiment.df <- as.data.frame(sentiment.freq)
ggplot(sentiment.df, aes(x=Var1, y=Freq)) +
geom_bar(stat="identity") +
xlab("Terms") + ylab("Count") +
coord_flip() +
theme(axis.text=element_text(size=7))
# Plot wordcloud ----------------------------------------------------------
library(wordcloud)
pal.p <- brewer.pal(9, "BuGn")[-(1:4)]
pal.n <- brewer.pal(9, "OrRd")[-(1:4)]
wordcloud(words = sentiment.df$Var1, freq = sentiment.df$Freq, random.order = F, colors = pal.p)
corpus.n <- corpus[corpus$sentiment == "negative", ]
corpus.p <- corpus[corpus$sentiment == "positive", ]
n.freq <- freq4[names(freq4) %in% unlist(corpus.n$word)]
p.freq <- freq4[names(freq4) %in% unlist(corpus.p$word)]
n.df <- as.data.frame(n.freq)
p.df <- as.data.frame(p.freq)
par(mfrow=c(1, 2), mai=rep(0.3, 4))
wordcloud(words = p.df$Var1, freq = p.df$Freq, random.order = F, colors = pal.p)
wordcloud(words = n.df$Var1, freq = n.df$Freq, random.order = F, colors = pal.n)
# Practice ---------------------------------------------------------------
# finding hillary's id and crawling her tweets
# how do we specify the tweet before trump winning the election?
# Further analysis --------------------------------------------------------
# extract to whom message
library(stringr)
df$to <- sapply(df$text,function(tweet) str_extract(tweet,"(@[[:alnum:]]*)"))
# get follower IDs
user <- getUser("realdonaldtrump")
user$getLocation()
followerIDs <- user$getFollowerIDs()
head(followerIDs)
length(followerIDs)
length(unique(followerIDs))
user$toDataFrame()
friends <- user$getFriends()
followers <- user$getFollowers()
save(followerIDs, file="R105/RData/trumpFollower.RData")
class(followerIDs)
fuser <- getUser(followerIDs[3])
fuser$location
fuser$name
fuser$screenName
fuser$followersCount