-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchfun.R
222 lines (138 loc) · 4.57 KB
/
searchfun.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
# Searchfun R
library(twitteR)
stock.tweets = searchTwitter("zbuechler", n=1500)
since=as.character(time.from), until=as.character(time.to))
stock.tweets = searchTwitter("julieako", n=1500)
time.from
time.to
rm(puppies)
puppies <- character()
str(puppies)
as.character(time.from)
puppies <- c(puppies, as.character(time.from))
puppies
str(puppies[1])
puppies[1]
kk <- data.frame(puppies)
kk
str(puppies[1])
stock.query <- my.stocks[[i]]
stock.query <- "$MSFT"
stock.query <- "MSFT"
stock.query <- "Microsoft"
time.from
time.to
stock4 <- list("MSFT", "@MSFT", "Microsoft", "Bill Gates")
stock5 <- list("XOM")
my.stocks<- stock5
stock.query
time.from
time.to
rm(stock.tweets)
stock.tweets = searchTwitter(stock.query, n=1500, lang = 'en',
since=as.character(time.from), until=as.character(time.to))
time.from <- as.Date(time.from, origin = "1900-01-01") + 1
time.to <- as.Date(time.from, origin = "1900-01-01") + 1
time.from <- as.Date(time.from, origin = "1900-01-01") - 1
time.to <- as.Date(time.from, origin = "1900-01-01") + 1
stock.tweets = searchTwitter(stock.query, n=1500,
since=as.character(time.from), until=as.character(time.to))
stock.query <- "ibm"
stock.tweets = searchTwitter(stock.query, n=1500, lang = 'en')
> length(stock.tweets)
#[1] 599
> length(stock.tweets2)
#[1] 899
stock.tweets = searchTwitter("Gates", n=1500, lang = 'en')
stock.tweets2b = searchTwitter("Bill Gates", n=1500, lang = 'en')
str(stock.tweets2b)
st2<- stock.tweets2b
length(st2)
kh2 <- character()
for(i in 1:length(st2))
{
kh2[[i]] <- st2[[i]]$text
}
kh2
kh2[1]
kh2[2]
kh2
library(plyr)
stock.text = laply(st2, function(t) t$getText() )
getwd()
#FUCK YEA THIS WORKS
write.csv(stock.text, file = "data8.txt")
laa2<- read.csv("data8.txt", stringsAsFactors = FALSE)
stjwjw <- as.character(laa2$x)
stjwjw == stock.text
#ahuh <- paste(stock.query, time.from, time.to, ".txt")
str(Sys.Date())
ahuh2 <- paste(time.from, time.to, Sys.Date(), stock.query, ".txt")
ahuh2
str(ahuh2)
str(ahuh)
write.csv(stock.text, file = ahuh)
laa2b<- read.csv(ahuh, stringsAsFactors = FALSE)
stjwjwa <- as.character(laa2b$x)
stjwjwa == stock.text
#Use these two
str(time.from)
str(time.to)
str(as.character(time.from))
str(as.character(time.to))
as.character(time.from)
str(stock.query)
paste(stock.query, time.from, time.to)
stock.scores = score.sentiment(stock.text, pos.words, neg.words, .progress='text')
str(st2[[2]])
str(st2[[1]])
st2[[1]]$text
st2[[1]]$
write.
stock.tweets3 = searchTwitter("XOM", n=1500)
stock.tweets3
for(i in 1:search.terms)
{
#For searching multiple stocks + multiple stock terms
#stock.query <- my.stocks[[a]][[i]]
#For searching one stock's terms
stock.query <- my.stocks[[i]]
rm(stock.tweets)
stock.tweets = searchTwitter(stock.query, n=1500,
since=as.character(time.from), until=as.character(time.to))
# stock.tweets = searchTwitter(stock.query, n=1500,
# since=as.character("2013-11-05"), until=as.character(time.to))
rm(stock.text)
stock.text = laply(stock.tweets, function(t) t$getText() )
stock.scores = score.sentiment(stock.text, pos.words, neg.words, .progress='text')
#Assumption: we assume all tweets get equally exposed at all times
#In the future we can see how many followers a tweet has.
#then we can factor a score in to each sentiment, to see how real exposed it is.
running.total<- c(running.total, stock.scores$score)
# *** Add progress meter for long load times
}
#count of tweets?
num1 <- length(running.total)
tweet.count <- c(tweet.count, num1)
sent1 <- mean(running.total)
sentiment<- c(sentiment, sent1)
day1 <- time.from
dates <- c(dates, as.character(day1))
# *** later we can store every keyword mean for each day as a list too
time.from <- as.Date(time.from, origin = "1900-01-01") + 1
time.to <- as.Date(time.from, origin = "1900-01-01") + 1
# *** Do we care about weekends? Implement weekend separator to skipp weekends
# and do three day analysis: Fr-Su!
}
stock.sentiment.data <- data.frame(sentiment, dates, tweet.count)
colnames(stock.sentiment.data) <- c("Sentiment","Date", "Number of Tweets")
getwd()
write.matrix(stock1final,file = "stock1finalold.csv", sep = ",")
dropbox.path <- "/Users/Vivek/Dropbox/Bus 192/Shared Bus 192/Twitter/"
setwd(dropbox.path)
stocktest <- read.csv(file = "stock1finalold.csv", stringsAsFactors = FALSE)
#stock1sent <-
rm(Thurman)
rm(Thurman2)
rm(canada.cities)
rm(laa, laa2b)