-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentimentalAnalysis.R
316 lines (204 loc) · 12.1 KB
/
SentimentalAnalysis.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#------------------------------------------------------------------------------------------------------#
# R Script Name: Sentimental Analysis.R
# R Script Designed On: 30/9/2016
# R Script Designed On: 30/9/2016
# R Script Descrption: This script is used to calculate sentimental scores of the extracted tweets.
#------------------------------------------------------------------------------------------------------#
#-- Importing Required library
library("RODBC", lib.loc="~/R/win-library/3.3")
library("stringi", lib.loc="~/R/win-library/3.3")
library("stringr", lib.loc="~/R/win-library/3.3")
library("tm", lib.loc="~/R/win-library/3.3")
library("RColorBrewer", lib.loc="~/R/win-library/3.3")
library("wordcloud", lib.loc="~/R/win-library/3.3")
library("plyr", lib.loc="~/R/win-library/3.3")
library("ggplot2", lib.loc="~/R/win-library/3.3")
#-- Script Begin
Systime <- Sys.time()
Systime<- str_replace_all(Systime,"[[:punct:]]","_")
Systime<- str_replace_all(Systime," ","_")
my_conn <- odbcConnect("RSQL", uid="sa", pwd="**")
inprogress_count<- sqlQuery(my_conn, "select count(1) from t_search_staging where search_status = 'InProgress' ")
if(inprogress_count==1){
inprogress_sid <- sqlQuery(my_conn, "select search_id from t_search_staging where search_status = 'InProgress' ")
}else{
inprogress_sid<- -2
}
count<- sqlQuery(my_conn, "select count(1) from t_search_process_status where process_status = 'Queued' and process_description in ('Sentimental Analysis using R scripts','Sentimental Analysis Plot') ")
if(count==2){
count_sid<- sqlQuery(my_conn, "select search_id from t_search_process_status where process_status = 'Queued' and process_description='Sentimental Analysis using R scripts' ")
}else{
count_sid<- -1
}
if(count_sid==inprogress_sid){
flag <- 1
}else{
flag <- 0
}
if(flag==1)
{
Search_id_request<- sqlQuery(my_conn, "select search_Id from t_search_process_status where process_status='QUEUED' and process_description='Sentimental Analysis using R scripts' ")
file_location<- paste("C:\\Users\\lenovo\\Desktop\\Logs\\SentimentalAnalysisScriptLogs\\", paste("Logs_SearchId_",Search_id_request,"_",Systime, ".txt", sep=""),sep="")
log_con <- file(file_location, open="a")
text <- paste("RODBC library has been imported sucesfully at: ",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
text <- "......................................................................................."
cat( text , file = log_con, sep="\n", append=FALSE)
text <- paste("Imorting CSV cleaned tweets data file from location of Search Id:",Search_id_request," at:",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
text <- "......................................................................................."
cat( text , file = log_con, sep="\n", append=FALSE)
x<- "C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\Output\\"
# Hardcode
# Search_id_request = 114
y<- paste0(x,"Ouput_Search_Id_",Search_id_request)
if(!dir.exists(y)){
dir.create(y)
}
z<- paste0(y,"\\S_ID_",Search_id_request,"_Cleaned_Tweets_Collected.csv")
twwets_df <- read.csv(file=z, header=TRUE, sep=",",colClasses = "character")
text <- paste("CSV file has been impoted for Search Id:",Search_id_request," at:",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
text <- "......................................................................................."
cat( text , file = log_con, sep="\n", append=FALSE)
text <- paste("Sentimental Analysis has been started for data having Search Id:",Search_id_request," at:",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
text <- "......................................................................................."
cat( text , file = log_con, sep="\n", append=FALSE)
twwets_df<- subset(twwets_df, select = c(text))
# Import Positive and Negative Words
pos = readLines("C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\R Files\\positive_words.txt")
neg = readLines("C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\R Files\\negative_words.txt")
#dump("my_score_function", file="C:\\Users\\lenovo\\Documents\\score_sentiment.R")
source("C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\R Files\\score_sentiment.R")
# Apply function score.sentiment
scores = score.sentiment(twwets_df$text, pos, neg, .progress='text')
scores["Search_id"] = Search_id_request
rownames(scores) <- NULL
my_conn <- odbcConnect("RSQL", uid="sa", pwd="**")
sqlSave(my_conn, scores, tablename = "t_search_output_sentmnt_score",rownames = FALSE, append = TRUE)
my_query <- paste("select search_String from t_search_staging where search_status='InProgress' and search_Id=",Search_id_request, sep="")
Search_string_request <- sqlQuery(my_conn, my_query)
scores["search_String"] = Search_string_request
x<- "C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\Output\\"
y<- paste0(x,"Ouput_Search_Id_",Search_id_request)
if(!dir.exists(y)){
dir.create(y)
}
# Saving Sentiments score in project location
# Sentimental Analysis Bar Plot location = z.
z<- paste0(y,"\\S_ID_",Search_id_request,"_Sentiments_Score.csv")
write.csv(file=z, x=scores)
my_query <- paste("select search_String from t_search_staging where search_status='InProgress' and search_Id=",Search_id_request, sep="")
Search_string_request <- sqlQuery(my_conn, my_query)
scores$score = as.numeric(as.character(scores$score))
scores["Sentiments"] = NA
scores[scores$score> 0, "Sentiments"] = "Positive"
scores[scores$score< 0, "Sentiments"] = "Negative"
scores[scores$score == 0, "Sentiments"] = "Neutral"
scores_aggregate <- aggregate(cbind(count = Sentiments) ~ Sentiments,
data = scores,
FUN = function(x){NROW(x)})
rownames(scores_aggregate) <- NULL
scores_aggregate$count = as.numeric(as.character(scores_aggregate$count))
x<- "C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\Output\\"
y<- paste0(x,"Ouput_Search_Id_",Search_id_request)
if(!dir.exists(y)){
dir.create(y)
}
z1<- paste0(y,"\\S_ID_",Search_id_request,"_Sentiments_Bar_Plot.png")
#png(z1, width=800,height=600)
# Sentimental Analysis Bar Plot location = z1.
ggplot(data=scores_aggregate, aes(x=scores_aggregate$Sentiments, y=scores_aggregate$count)) +
geom_bar(stat="identity")
ggsave(z1)
#dev.off()
text <- paste("Sentimental Analysis has been done for Search Id:",Search_id_request," at:",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
x<- "C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\Output\\"
# Hardcode
#Search_id_request = 114
y<- paste0(x,"Ouput_Search_Id_",Search_id_request)
if(!dir.exists(y)){
dir.create(y)
}
loc <- paste0(y,"\\S_ID_",Search_id_request,"_Frequent_Words.csv")
Freq_Words_df <- read.csv(file=loc, header=TRUE, sep=",",colClasses = "character")
Frequent_Words_df = subset(Freq_Words_df, select = c(Word))
# Apply function score.sentiment
scores_frq_words = score.sentiment(Frequent_Words_df$Word, pos, neg, .progress='text')
scores_frq_words["Sentiments"] = NA
scores_frq_words[scores_frq_words$score> 0, "Sentiments"] = "Positive"
scores_frq_words[scores_frq_words$score< 0, "Sentiments"] = "Negative"
scores_frq_words[scores_frq_words$score == 0, "Sentiments"] = "Neutral"
x<- "C:\\Users\\lenovo\\Documents\\Visual Studio 2015\\Projects\\WebApplication1\\WebApplication1\\Output\\"
y<- paste0(x,"Ouput_Search_Id_",Search_id_request)
if(!dir.exists(y)){
dir.create(y)
}
z2<- paste0(y,"\\S_ID_",Search_id_request,"_Sentiments_Wise_Words.csv")
write.csv(file=z2, x=scores_frq_words)
my_query<- paste0("DECLARE @GetOutput int;
exec InsertSearchProcessStatus
@search_Id =", Search_id_request ,",
@process_description = 'Sentimental Analysis Words Lists',
@process_status = 'Queued',
@location = '",NA,"',
@id = @GetOutput Output
select @GetOutput;")
sqlQuery(my_conn, my_query)
text <- paste("Sentimental Analysis plot has been generated for Search Id:",Search_id_request," at:",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
text <- "......................................................................................."
cat( text , file = log_con, sep="\n", append=FALSE)
my_query <- paste("update t_search_process_status set process_status ='Completed' , location='",z,"' where process_description = 'Sentimental Analysis using R scripts' and search_Id=",Search_id_request, sep=" ")
sqlQuery(my_conn, my_query)
my_query <- paste("update t_search_process_status set process_status ='Completed' , location='",z1,"' where process_description = 'Sentimental Analysis Plot' and search_Id=",Search_id_request, sep=" ")
sqlQuery(my_conn, my_query)
my_query <- paste("update t_search_process_status set process_status ='Completed' , location='",z2,"' where process_description = 'Sentimental Analysis Words Lists' and search_Id=",Search_id_request, sep=" ")
sqlQuery(my_conn, my_query)
text <- paste("Sentimental Analysis using R Script Sucesfully Completed for Search Id:",Search_id_request," at:",Sys.time(), sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
text <- "......................................................................................."
cat( text , file = log_con, sep="\n", append=FALSE)
my_query<- paste0("DECLARE @GetOutput int;
exec InsertSearchProcessStatus
@search_Id =", Search_id_request ,",
@process_description = 'Location Analytics using R Script',
@process_status = 'Queued',
@location = '",NA,"',
@id = @GetOutput Output
select @GetOutput;")
# here is the partially completed update
#my_query <- paste("update t_search_staging set search_status ='Completed' where search_Id =",Search_id_request, sep=" ")
sqlQuery(my_conn, my_query)
remove(list=c( "Search_id_request", "twwets_df"))
remove(list=c("x", "y", "z","my_query"))
remove(list=c("z1", "z2", "pos","neg","loc"))
remove(list=c("Freq_Words_df", "Frequent_Words_df", "scores","scores_aggregate","scores_frq_words","Search_string_request"))
close(log_con)
close(my_conn)
}else if(flag == 0){
file_location<- paste("C:\\Users\\lenovo\\Desktop\\Logs\\SentimentalAnalysisScriptLogs\\", paste("Logs_SearchId_",Systime, ".txt", sep=""),sep="")
log_con <- file(file_location, open="a")
text <- paste("Terminating Sentimental script as no request is queue.", sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
close(log_con)
}else{
file_location<- paste("C:\\Users\\lenovo\\Desktop\\Logs\\SentimentalAnalysisScriptLogs\\", paste("Logs_SearchId_",Systime, ".txt", sep=""),sep="")
log_con <- file(file_location, open="a")
text <- paste("Terminating Sentimental script as no request is queue", sep=" ")
cat( text , file = log_con, sep="\n", append=FALSE)
close(log_con)
}
remove(list=c("file_location", "Systime", "text"))
remove(list=c("inprogress_count", "count"))
temp<- tryCatch({odbcGetInfo(my_conn);TRUE},error=function(...)FALSE)
if (temp){
close(my_conn)
}
temp<- tryCatch({odbcGetInfo(log_con);TRUE},error=function(...)FALSE)
if (temp){
close(log_con)
}
remove(list=c("temp","inprogress_sid","count_sid","flag"))