-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.R
295 lines (257 loc) · 8.07 KB
/
global.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# title: "IE6600 - Group 8 Project"
# author: "Zechen Li"
# Installing package if not already installed
EnsurePackage<-function(x)
{x <- as.character(x)
if (!require(x,character.only=TRUE))
{
install.packages(pkgs=x)
require(x,character.only=TRUE)
}
}
#Identifying packages required
PrepareTwitter<-function()
{
EnsurePackage("dplyr")
EnsurePackage("rtweet")
EnsurePackage("httpuv")
EnsurePackage("slam")
EnsurePackage("wordcloud2")
EnsurePackage("shiny")
EnsurePackage("DT")
EnsurePackage("stringr")
EnsurePackage("shinythemes")
EnsurePackage("tm")
EnsurePackage("qdapRegex")
EnsurePackage("rjson")
EnsurePackage("jsonlite")
EnsurePackage("leaflet")
EnsurePackage("gganimate")
EnsurePackage("lubridate")
EnsurePackage("maps")
EnsurePackage("ggthemes")
EnsurePackage("ggdark")
EnsurePackage("plotly")
EnsurePackage("tibble")
EnsurePackage("lubridate")
EnsurePackage("gapminder")
EnsurePackage("gifski")
EnsurePackage("plotrix")
EnsurePackage("reshape")
}
#devtools::install_github("lchiffon/wordcloud2")
#devtools::install_github("dgrtwo/gganimate")
library(dplyr)
library(tm)
library(ggdark)
library(rtweet)
library(httpuv)
library(slam)
library(stringr) # Removing characters
library(qdapRegex) # Removing URLs
library(shiny)
library(DT)
library(shinythemes)
library(wordcloud2)
library(rjson)
library(jsonlite)
library(leaflet)
library(gganimate)
library(lubridate)
library(maps)
library(ggthemes)
library(plotly)
library(tibble)
library(lubridate)
library(gapminder)
library(gifski)
library(plotrix)
library(reshape)
#Define key and secret
consumer_key <- 'xxx'
consumer_secret <- 'xxx'
Access_token <-'xxx'
Access_tokensecret <- 'xxx'
#Create a token to connect to Twitter's API using your key and secret
token <- create_token(app="RyanDV", consumer_key, consumer_secret,
Access_token, Access_tokensecret,set_renv = TRUE)
# Clean the tweets
TweetClean<-function(tweets)
{
text <-
str_c(tweets$text, collapse = "") %>%
tolower() %>%
str_remove("\\n") %>% # remove linebreaks
rm_twitter_url() %>% # Remove URLS
rm_url() %>%
str_remove_all("#\\S+") %>% # Remove any hashtags
str_remove_all("@\\S+") %>% # Remove any @ mentions
removeWords(stopwords("SMART")) %>% # Remove common words (a, the, it etc.)
removeNumbers() %>%
stripWhitespace() %>%
removeWords(c("amp")) # Final cleanup of other small changes
#removal of emoticons
text <- sapply(text,function(row) iconv(row, "latin1", "ASCII", sub="")) #If you wish to print emoticons just comment this line
text = gsub("(f|ht)tp(s?)://(.*)[.][a-z]+", "", text)
return (text)
}
wordclouds2 <- function(tweets_clean)
{
# Convert the data into a summary table
textCorpus <-
Corpus(VectorSource(tweets_clean)) %>%
TermDocumentMatrix() %>%
as.matrix()
textCorpus <- sort(rowSums(textCorpus), decreasing=TRUE)
textCorpus <- data.frame(word = names(textCorpus), freq=textCorpus, row.names = NULL)
return (textCorpus)
}
toptrends <- function(location)
{
trend <- get_trends(location)
trends <- trend %>%
arrange(desc(tweet_volume)) %>%
mutate(id = row_number())%>%
select("Id" = id, "Trend" = trend, "Tweet Volume" = tweet_volume)
return (trends[1:10,])
}
# create new df with just the tweet texts & usernames
world_map_plot <- function(tweet_geo_data)
{
# plot points on top of a leaflet basemap
site_locations_base <- leaflet(tweet_geo_data) %>%
addProviderTiles(providers$Stamen.Toner) %>%
addCircleMarkers(lng = ~long, lat = ~lat, popup = ~tweet_text,
radius = 4, stroke = FALSE,color = "red",
fillOpacity = 0.6)
return (site_locations_base)
}
animate_plot <- function(tweet_geo_data){
tweet_geo_data$date_time <-format(tweet_geo_data$date_time, tz="America/Los_Angeles",usetz=TRUE)
tweet_geo_data$date_time <- as.POSIXct(tweet_geo_data$date_time)
world <- ggplot() +
borders("world", colour = "gray35", fill = "gray80") +
theme_map()
map <- world +
geom_point(aes(x = long,
y = lat,
size = followers_count),
data = tweet_geo_data,
colour = '#CC0000', alpha = .6) +
scale_size(range = c(1,8),
breaks = c(100, 500, 1000, 3000, 6000)) +
labs(size = 'Followers',
title = 'Time: {closest_state} PDT') +
dark_theme_gray() +
theme(
legend.background = element_blank(),
legend.position = c(0.05, 0.23),
legend.key = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank()
) +
transition_states(date_time, 0, 1, wrap = F) +
shadow_mark()
return(map)
}
TweetFrame<-function(tweets1)
{
#removal of emoticons
tweets1$text <- sapply(tweets1$text,function(row) iconv(row, "latin1", "ASCII", sub="")) #If you wish to print emoticons just comment this line
tweets1$text = gsub("(f|ht)tp(s?)://(.*)[.][a-z]+", "", tweets1$text)
return (tweets1$text)
}
#"positive_words.txt" file
pos.words = scan('www/positive_words.txt', what='character', comment.char=';')
#"negative_words.txt" file
neg.words = scan('www/negative_words.txt', what='character', comment.char=';')
wordDatabase<-function()
{
pos.words<<-c(pos.words)
neg.words<<-c(neg.words)
}
score.sentiment <- function(sentences, pos.words, neg.words, .progress='none')
{
list=lapply(sentences, function(sentence, pos.words, neg.words)
{
sentence = gsub('[[:punct:]]',' ',sentence)
sentence = gsub('[[:cntrl:]]','',sentence)
sentence = gsub('\\d+','',sentence)
sentence = gsub('\n','',sentence)
sentence = tolower(sentence)
word.list = str_split(sentence, '\\s+')
words = unlist(word.list)
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
pp=sum(pos.matches)
nn = sum(neg.matches)
score = sum(pos.matches) - sum(neg.matches)
list1=c(score, pp, nn)
return (list1)
}, pos.words, neg.words)
score_new=lapply(list, `[[`, 1)
pp1=score=lapply(list, `[[`, 2)
nn1=score=lapply(list, `[[`, 3)
scores.df = data.frame(score=score_new, text=sentences)
positive.df = data.frame(Positive=pp1, text=sentences)
negative.df = data.frame(Negative=nn1, text=sentences)
list_df=list(scores.df, positive.df, negative.df)
return(list_df)
}
sentimentAnalyser<-function(result)
{
#Creating a copy of result data frame
test1=result[[1]]
test2=result[[2]]
test3=result[[3]]
#Creating three different data frames for Score, Positive and Negative
#Removing text column from data frame
test1$text=NULL
test2$text=NULL
test3$text=NULL
#Storing the first row(Containing the sentiment scores) in variable q
q1=test1[1,]
q2=test2[1,]
q3=test3[1,]
qq1=melt(q1, var='Score')
qq2=melt(q2, var='Positive')
qq3=melt(q3, var='Negative')
qq1['Score'] = NULL
qq2['Positive'] = NULL
qq3['Negative'] = NULL
#Creating data frame
table1 = data.frame(Text=result[[1]]$text, Score=qq1)
table2 = data.frame(Text=result[[2]]$text, Score=qq2)
table3 = data.frame(Text=result[[3]]$text, Score=qq3)
#Merging three data frames into one
table_final=data.frame(Text=table1$Text, Positive=table2$value, Negative=table3$value, Score=table1$value)
return(table_final)
}
percentage<-function(table_final)
{
#Positive Percentage
#Renaming
posSc=table_final$Positive
negSc=table_final$Negative
#Adding column
table_final$PosPercent = posSc/ (posSc+negSc)
#Replacing Nan with zero
pp = table_final$PosPercent
pp[is.nan(pp)] <- 0
table_final$PosPercent = round(pp*100, 2)
#Negative Percentage
#Adding column
table_final$NegPercent = negSc/ (posSc+negSc)
#Replacing Nan with zero
nn = table_final$NegPercent
nn[is.nan(nn)] <- 0
table_final$NegPercent = round(nn*100, 2)
return(table_final)
}
wordDatabase()