-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathglobal.r
231 lines (173 loc) · 7.65 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
#GLOBAL.R
## Allocate memory
options(java.parameters = "-Xmx10g")
## clear console
cat("\014")
## clear global variables
rm(list=ls())
## list of packages required
list.of.packages = c("git2r","digest","devtools",
"RCurl","RJSONIO","stringr","syuzhet","httr",
"rjson","tm","NLP","RCurl","wordcloud","wordcloud2",
"tidytext","dplyr","zipcode","bit", "shiny", "shinythemes")
new.packages = list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
## for devtools
library(git2r);library(digest)
#require(devtools)
#install_github("hadley/devtools")
library(devtools)
install_github("geoffjentry/twitteR")
## data manipultion
library(dplyr);library(stringr)
# loading the libraries
## Linked to importing tweets
library(rjson);library(httr);library(twitteR);library(zipcode)
## Linked to generating a wordcloud
library(tm);library(NLP);library(RCurl);library(RJSONIO)
library(stringr);library(wordcloud);library(wordcloud2);
#To create Shiny environment
library(shinythemes)
## Linked to sentiment analysis
library(syuzhet)
# Twitter authentication key
oauth = setup_twitter_oauth(consumer_key = "LhLzIn0nbz5mORcE3wPdSmWjP",
consumer_secret = "xILbs2S5IbNiZyFwXU7VITcVCxxzf3SpA2Gbvn3qBNF8LY8woQ",
access_token = "110651492-aB9iL1exrmkb3Q2gmM2DEqCzz6eo0TQiqqjRRXec",
access_secret = "8XZIQ6eVvOwAwhvGSSIL4SEitrttjAkf6SYAJHFXMFxz9")
cat("\014")
cleanTweets = function(object.with.tweets){
# list to dataframe
df.tweets <- twListToDF(object.with.tweets)
# Removes RT
df.tweets$text_clean = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", df.tweets$text)
#Remove non-ASCII characters
Encoding(df.tweets$text_clean) = "latin1"
iconv(df.tweets$text_clean, "latin1", "ASCII", sub = "")
# Removes @<twitter handle>
df.tweets$text_clean = gsub("@\\w+", "", df.tweets$text_clean)
# Removes punctuations
df.tweets$text_clean = gsub("[[:punct:]]", "", df.tweets$text_clean)
# Removes numbers
df.tweets$text_clean = gsub("[[:digit:]]", "", df.tweets$text_clean)
# Removes html links
df.tweets$text_clean = gsub("http\\w+", "", df.tweets$text_clean)
# Removes unnecessary spaces
df.tweets$text_clean = gsub("[ \t]{2,}", "", df.tweets$text_clean)
df.tweets$text_clean = gsub("^\\s+|\\s+$", "", df.tweets$text_clean)
# Fix for error related to formatting 'utf8towcs'"
df.tweets$text_clean <- str_replace_all(df.tweets$text_clean,"[^[:graph:]]", " ")
return(df.tweets)
}
searchThis = function(search_string,geocode_string = "42.375,-71.1061111,1000mi",number.of.tweets = 100)
{
searchTwitter(search_string, geocode=geocode_string,n = number.of.tweets, lang = "en")
}
userTL = function(user.name,number.of.tweets = 100)
{
userTimeline(user.name,n = number.of.tweets)
}
# Generate Term Document Matrix using stopword list from tm pacakge
tdm.tmStopWord = function(clean.tweets.dataframe){
# Creates a text corpus from the plain text document for every tweet
text_corpus = Corpus(VectorSource(clean.tweets.dataframe$text_clean))
# Text_corpus is a collection of tweets where every tweet is a document
# creating a Term Document Matrix
tdm = TermDocumentMatrix(
# the text corpus created from the text_clean object
text_corpus,
# defining the stopwords to be removed before creating a term document matrix
control = list(
removePunctuation = TRUE,
stopwords("en"),
removeNumbers = TRUE,
tolower = TRUE)
)
return(tdm)
}
# Generate Term Document Matrix using TF-IDF
tdm.TFIDF = function(clean.tweets.dataframe){
text_corpus = Corpus(VectorSource(clean.tweets.dataframe$text_clean))
# Text_corpus is a collection of tweets where every tweet is a document
tdm <- DocumentTermMatrix(text_corpus, control = list(weighting = weightTfIdf))
return(tdm)
}
# Generate Term Document Matrix without removing stopwords
tdm.tm = function(clean.tweets.dataframe){
text_corpus = Corpus(VectorSource(clean.tweets.dataframe$text_clean))
tdm = TermDocumentMatrix(text_corpus,control = list(removePunctuation = TRUE,
removeNumbers = TRUE,
tolower = TRUE))
return(tdm)
}
getSentiments.TF_IDF.nrc = function(tdm.tfidf){
m <- as.matrix(tdm.tfidf)
word_tfidf <- sort(colSums(m), decreasing = TRUE)
dm <- data.frame(word = names(word_tfidf), tfidf = word_tfidf)
dm.subset <- dm[dm$tfidf>=quantile(dm$tfidf,0.25),]
nrc.lex <- get_nrc_sentiment(as.character(dm.subset$word))
}
generateWordCloud.positive.tmStopWords = function(tdm.tm.stopword){
# converting term document matrix to matrix
m = as.matrix(tdm.tm.stopword)
# 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)
nrc.lexicons = get_nrc_sentiment(as.character(dm$word))
tweets.positive = dm[nrc.lexicons$positive>0,]
}
generateWordCloud.negative.tmStopWords = function(tdm.tm.stopword){
# converting term document matrix to matrix
m = as.matrix(tdm.tm.stopword)
# 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)
nrc.lexicons = get_nrc_sentiment(as.character(dm$word))
tweets.negative = dm[nrc.lexicons$negative>0,]
}
generateWordCloud.positive.TF_IDF = function(tdm.tfidf, tdm.tm.nostop){
# converting term document matrix to matrix
m <- as.matrix(tdm.tfidf)
word_tfidf <- sort(colSums(m), decreasing = TRUE)
# create a data frame with words and their frequencies
dm <- data.frame(word = names(word_tfidf), tfidf = word_tfidf)
#plot(dm$freq,type = "l")
dm.subset <- dm[dm$tfidf>=quantile(dm$tfidf,0.25),]
## creating term frequency dataframe
m.word.freq <- as.matrix(tdm.tm.nostop)
word_freqs.word.freq <- sort(colSums(m), decreasing = TRUE)
dm.word.freq <- data.frame(word = names(word_freqs.word.freq), freq = word_freqs.word.freq)
## subsetting the tdm
dm.word.freq.new <- dm.word.freq[dm.word.freq$word %in% dm.subset$word,]
nrc.lexicons <- get_nrc_sentiment(as.character(dm.word.freq.new$word))
tweets.positive <- dm.word.freq.new[nrc.lexicons$positive>0,]
}
generateWordCloud.negative.TF_IDF = function(tdm.tfidf, tdm.tm.nostop){
# converting term document matrix to matrix
m <- as.matrix(tdm.tfidf)
word_tfidf <- sort(colSums(m), decreasing = TRUE)
# create a data frame with words and their frequencies
dm <- data.frame(word = names(word_tfidf), tfidf = word_tfidf)
#plot(dm$freq,type = "l")
dm.subset <- dm[dm$tfidf>=quantile(dm$tfidf,0.25),]
## creating term frequency dataframe
m.word.freq <- as.matrix(tdm.tm.nostop)
word_freqs.word.freq <- sort(colSums(m), decreasing = TRUE)
dm.word.freq <- data.frame(word = names(word_freqs.word.freq), freq = word_freqs.word.freq)
## subsetting the tdm
dm.word.freq.new <- dm.word.freq[dm.word.freq$word %in% dm.subset$word,]
nrc.lexicons <- get_nrc_sentiment(as.character(dm.word.freq.new$word))
tweets.negative <- dm.word.freq.new[nrc.lexicons$negative>0,]
}
data("zipcode")
attach(zipcode)
getLatLong.zip = function(enter.zipcode,radius.mi)
{
enter.zipcode = as.character(enter.zipcode)
radius.mi = as.character(radius.mi)
lat.long = zipcode[zip == enter.zipcode,c("latitude","longitude")]
lat.long.mi = paste0(lat.long$latitude,",",lat.long$longitude,",",radius.mi,"mi")
return(lat.long.mi)
}