-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.R
196 lines (150 loc) · 8.97 KB
/
server.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
# Backend/server of R app
# Performs sentiment analysis of tweets based on emotion and polarity classification
# The visualization of the sentiment class distributions is performed using ggplot2 package
# required pakacges
library(twitteR)
library(sentiment)
library(plyr)
library(ggplot2)
library(RColorBrewer)
# loading twitter credentials
#load("twitteR_credentials")
#registerTwitterOAuth(twitCred)
api_key <- "Epoyc1VfIrHOyD6K5DIV4QlN3"
api_secret <- "STu3FNc87kimdiMV6Nw4ouWBKKo9Qbxgi6ultTvDVPCWSa0Kgs"
access_token <- "469047588-S8cXA0zY01AlcZUAQMm5afRszhkML3xR9QkKrXxe"
access_token_secret <- "zct6ERd2AkJhJwyXRUOf4x0pIjKlF0wIkPrc9ClDSFE4i"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
# loading the helper functions
source('helpers.R')
source('helpers1.R')
source('helpers2.R')
shinyServer(function(input, output) {
# Step 1: Getting the tweets based on search terms
# cainfo="cacert.pem" is required for data access
# tweets <- reactive ({ searchTwitter(input$searchTerm,n=1000, lang="en") })
tweets <- reactive ({ searchTwitter(input$searchTerm,n=1000, lang="en") })
#tweets <- renderText({ input$searchTerm })
# Step2: Preprocessing to clean up the tweets
txtTweets <- reactive ({ preprocess_tweet (tweets()) })
output$plot_emotion <- renderPlot({
# Step 3: Emotion sentiment analysis
emotion <- emotionSentimentAnal(txtTweets())
# Step 4: Polarity sentiment analysis
polarity <- polaritySentimentAnal(txtTweets())
# Step 5: Store results in dataframe
results_df <- data.frame(text=txtTweets(), emotion=emotion, polarity=polarity)
# Step 6: Plot distribution of tweet sentiments
if (input$plot_opt == 'emotion') {
ggplot(results_df) +
geom_bar(aes(x=emotion, y=..count.., fill=emotion)) +
ggtitle(paste('Using Bayes Method Intention Mining of Search Term "', input$searchTerm, '"', sep='')) +
xlab("Emotion Class") + ylab("No of Tweets") +
scale_fill_brewer(palette="Set1") +
theme_bw() +
theme(axis.text.y = element_text(colour="black", size=18, face='plain')) +
theme(axis.title.y = element_text(colour="black", size=18, face='plain', vjust=2)) +
theme(axis.text.x = element_text(colour="black", size=18, face='plain', angle=90, hjust=1)) +
theme(axis.title.x = element_text(colour="black", size=18, face='plain')) +
theme(plot.title = element_text(colour="black", size=20, face='plain', vjust=2.5)) +
theme(legend.text = element_text(colour="black", size=16, face='plain')) +
theme(legend.title = element_text(colour="black", size=18, face='plain')) +
guides(fill = guide_legend(keywidth = 2, keyheight = 2))
} else {
ggplot(results_df, aes()) +
geom_bar(aes(x=polarity, y=..count.., fill=polarity), width=0.6) +
ggtitle(paste('Using Bayes Method Intention Mining of Search Term "', input$searchTerm, '"', sep='')) +
xlab("Polarity Class") + ylab("No of Tweets") +
scale_fill_brewer(palette="Set1") +
theme_bw() +
theme(axis.text.y = element_text(colour="black", size=18, face='plain')) +
theme(axis.title.y = element_text(colour="black", size=18, face='plain', vjust=2)) +
theme(axis.text.x = element_text(colour="black", size=18, face='plain', angle=90, hjust=1)) +
theme(axis.title.x = element_text(colour="black", size=18, face='plain')) +
theme(plot.title = element_text(colour="black", size=20, face='plain', vjust=2.5)) +
theme(legend.text = element_text(colour="black", size=16, face='plain')) +
theme(legend.title = element_text(colour="black", size=18, face='plain')) +
guides(fill = guide_legend(keywidth = 2, keyheight = 2))
}
})
output$plot_emotion1 <- renderPlot({
# Step 3: Emotion sentiment analysis
emotion <- emotionSentimentAnal1(txtTweets())
# Step 4: Polarity sentiment analysis
polarity <- polaritySentimentAnal1(txtTweets())
# Step 5: Store results in dataframe
results_df <- data.frame(text=txtTweets(), emotion=emotion, polarity=polarity)
# Step 6: Plot distribution of tweet sentiments
if (input$plot_opt == 'emotion') {
ggplot(results_df) +
geom_bar(aes(x=emotion, y=..count.., fill=emotion)) +
ggtitle(paste('Using MAXENT Intention Mining of Search Term "', input$searchTerm, '"', sep='')) +
xlab("Emotion Class") + ylab("No of Tweets") +
scale_fill_brewer(palette="Set1") +
theme_bw() +
theme(axis.text.y = element_text(colour="black", size=18, face='plain')) +
theme(axis.title.y = element_text(colour="black", size=18, face='plain', vjust=2)) +
theme(axis.text.x = element_text(colour="black", size=18, face='plain', angle=90, hjust=1)) +
theme(axis.title.x = element_text(colour="black", size=18, face='plain')) +
theme(plot.title = element_text(colour="black", size=20, face='plain', vjust=2.5)) +
theme(legend.text = element_text(colour="black", size=16, face='plain')) +
theme(legend.title = element_text(colour="black", size=18, face='plain')) +
guides(fill = guide_legend(keywidth = 2, keyheight = 2))
} else {
ggplot(results_df, aes()) +
geom_bar(aes(x=polarity, y=..count.., fill=polarity), width=0.6) +
ggtitle(paste('Using MAXENT Intention Mining of Search Term "', input$searchTerm, '"', sep='')) +
xlab("Polarity Class") + ylab("No of Tweets") +
scale_fill_brewer(palette="Set1") +
theme_bw() +
theme(axis.text.y = element_text(colour="black", size=18, face='plain')) +
theme(axis.title.y = element_text(colour="black", size=18, face='plain', vjust=2)) +
theme(axis.text.x = element_text(colour="black", size=18, face='plain', angle=90, hjust=1)) +
theme(axis.title.x = element_text(colour="black", size=18, face='plain')) +
theme(plot.title = element_text(colour="black", size=20, face='plain', vjust=2.5)) +
theme(legend.text = element_text(colour="black", size=16, face='plain')) +
theme(legend.title = element_text(colour="black", size=18, face='plain')) +
guides(fill = guide_legend(keywidth = 2, keyheight = 2))
}
})
output$plot_emotion2 <- renderPlot({
# Step 3: Emotion sentiment analysis
emotion <- emotionSentimentAnal2(txtTweets())
# Step 4: Polarity sentiment analysis
polarity <- polaritySentimentAnal2(txtTweets())
# Step 5: Store results in dataframe
results_df <- data.frame(text=txtTweets(), emotion=emotion, polarity=polarity)
# Step 6: Plot distribution of tweet sentiments
if (input$plot_opt == 'emotion') {
ggplot(results_df) +
geom_bar(aes(x=emotion, y=..count.., fill=emotion)) +
ggtitle(paste('Using HMM Intention Mining of Search Term "', input$searchTerm, '"', sep='')) +
xlab("Emotion Class") + ylab("No of Tweets") +
scale_fill_brewer(palette="Set1") +
theme_bw() +
theme(axis.text.y = element_text(colour="black", size=18, face='plain')) +
theme(axis.title.y = element_text(colour="black", size=18, face='plain', vjust=2)) +
theme(axis.text.x = element_text(colour="black", size=18, face='plain', angle=90, hjust=1)) +
theme(axis.title.x = element_text(colour="black", size=18, face='plain')) +
theme(plot.title = element_text(colour="black", size=20, face='plain', vjust=2.5)) +
theme(legend.text = element_text(colour="black", size=16, face='plain')) +
theme(legend.title = element_text(colour="black", size=18, face='plain')) +
guides(fill = guide_legend(keywidth = 2, keyheight = 2))
} else {
ggplot(results_df, aes()) +
geom_bar(aes(x=polarity, y=..count.., fill=polarity), width=0.6) +
ggtitle(paste('Using HMM Intention Mining of Search Term "', input$searchTerm, '"', sep='')) +
xlab("Polarity Class") + ylab("No of Tweets") +
scale_fill_brewer(palette="Set1") +
theme_bw() +
theme(axis.text.y = element_text(colour="black", size=18, face='plain')) +
theme(axis.title.y = element_text(colour="black", size=18, face='plain', vjust=2)) +
theme(axis.text.x = element_text(colour="black", size=18, face='plain', angle=90, hjust=1)) +
theme(axis.title.x = element_text(colour="black", size=18, face='plain')) +
theme(plot.title = element_text(colour="black", size=20, face='plain', vjust=2.5)) +
theme(legend.text = element_text(colour="black", size=16, face='plain')) +
theme(legend.title = element_text(colour="black", size=18, face='plain')) +
guides(fill = guide_legend(keywidth = 2, keyheight = 2))
}
})
})