forked from ravikiranj/twitter-sentiment-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_helper.py~
executable file
·230 lines (216 loc) · 9.47 KB
/
html_helper.py~
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
import urllib
import datetime
from datetime import timedelta
class HTMLHelper:
#start __init__
def __init__(self):
self.html = ''
self.currDate = datetime.datetime.now()
self.weekDates = []
#self.weekDates.append(self.currDate.strftime("%Y-%m-%d"))
self.weekDates.append(self.currDate.strftime("%b %d"))
for i in range(1,7):
dateDiff = timedelta(days=-i)
newDate = self.currDate + dateDiff
#self.weekDates.append(newDate.strftime("%Y-%m-%d"))
self.weekDates.append(newDate.strftime("%b %d"))
#end
#start getDefaultHTML
def getDefaultHTML(self, error = 0):
html = '''
<html>
<head><title>Twitter Sentiment Analysis</title>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.4.1/build/cssgrids/grids-min.css" />
<link rel="stylesheet" type="text/css" href="static/styles.css" />
</head>
<body>
<div class="yui3-g" id="doc">
<div class="yui3-u" id="hd">
<h2> Twitter Sentiment Analyzer </h2>
</div>
<div class="yui3-u" id="bd">
<form name="keyform" id="key-form" method="get" onSubmit="return checkEmpty(this);">
<p><input type="text" value="" name="keyword" id="keyword"/><input type="submit" value="Search" id="sub"/></p>
<div id="timeframe">
<input type="radio" name="time" id="today" value="today" checked="true">Today</input>
<input type="radio" name="time" id="lastweek" value="lastweek">Last 7 days</input>
</div>
<div id="choice">
<input type="radio" name="method" id="baseline" value="baseline" checked="true">Baseline</input>
<input type="radio" name="method" id="naivebayes" value="naivebayes">Naive Bayes</input>
<input type="radio" name="method" id="maxentropy" value="maxentropy">Maximum Entropy</input>
</div>
</form>
'''
if(error == 1):
html += '<div id="error">Unable to fetch TWitter API data. Please try again later.</div>'
elif(error == 2):
html += '<div id="error">Unrecognized Method of Classfication, please choose one from above.</div>'
html += '''
</div>
<script type="text/javascript">
function checkEmpty(f) {
if (f.keyword.value === "") {
alert('Please enter a valid keyword');
return false;
}else{
f.submit();
return true;
}
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31119754-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
'''
return html
#end
#start getResultHTML
def getResultHTML(self, keyword, results, time, pos_count, neg_count, neut_count, checked):
keyword = urllib.unquote(keyword.replace("+", " "))
html = '''
<html>
<head><title>Twitter Sentiment Analysis</title>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.4.1/build/cssgrids/grids-min.css" />
<link rel="stylesheet" type="text/css" href="static/styles.css" />
</head>
<body>
<div class="yui3-g" id="doc">
<div class="yui3-u" id="hd">
<h2> Twitter Sentiment Analyzer </h2>
</div>
<div class="yui3-u" id="bd">
<form name="keyform" id="key-form" method="get" onSubmit="return checkEmpty(this);">
<p><input type="text" value="" name="keyword" id="keyword"/><input type="submit" value="Search" id="sub"/></p>
<div id="timeframe">
<input type="radio" name="time" id="today" value="today" checked="true">Today</input>
<input type="radio" name="time" id="lastweek" value="lastweek">Last 7 days</input>
</div>
<div id="choice">
<input type="radio" name="method" id="baseline" value="baseline">Baseline</input>
<input type="radio" name="method" id ="naivebayes" value="naivebayes">Naive Bayes</input>
<input type="radio" name="method" id="maxentropy" value="maxentropy">Maximum Entropy</input>
</div>
</form>
<div id="results">
'''
if(time == 'today'):
html += '<div id="result-chart"></div>'
elif(time == 'lastweek'):
html += '<div id="result-big-chart"></div>'
html += '<div id="content">'
left = '<div id="left"><h3>Positive</h3><ul>'
right = '<div id="right"><h3>Negative</h3><ul>'
middle = '<div id="middle"><h3>Neutral</h3><ul>'
for i in results:
res= results[i]
for j in res:
item = res[j]
if(item['label'] == 'positive'):
left += '<li title="'+self.weekDates[i]+'">' + item['tweet'] + '</li>'
elif(item['label'] == 'neutral'):
middle+= '<li title="'+self.weekDates[i]+'">' + item['tweet'] + '</li>'
elif(item['label'] == 'negative'):
right += '<li title="'+self.weekDates[i]+'">' + item['tweet'] + '</li>'
#end innerloop
#end outerloop
left += '</ul></div>'
right += '</ul></div>'
middle += '</ul></div>'
html += left + middle + right + '</div>'
if(time == 'today'):
html += '''
</div>
</div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
'''
html += "['Sentiment', 'Count'],"
html += "['Positive', "+ str(pos_count[0]) + "],"
html += "['Neutral', "+ str(neut_count[0]) + "],"
html += "['Negative', "+ str(neg_count[0]) + "]"
html += '''
]);
var options = {
'title': 'Sentiment Classification',
'titleTextStyle': {'fontSize': 15},
'hAxis': {'textStyle': {'fontSize': 15}},
'vAxis': {'textStyle': {'fontSize': 15}},
'legend' : {'position' : 'none'}
};
var chart = new google.visualization.ColumnChart(document.getElementById('result-chart'));
chart.draw(data, options);
}
'''
elif(time == 'lastweek'):
html += '''
</div>
</div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
'''
html += "['Date', 'Positive', 'Neutral', 'Negative'],"
l = len(pos_count)
for i in range(l-1, 0, -1):
html += "['"+ self.weekDates[i] + "', "+ str(pos_count[i]) + "," \
+ str(neut_count[i]) + "," + str(neg_count[i]) + "],"
#last one
html += "['"+ self.weekDates[0] + "', "+ str(pos_count[0]) + "," \
+ str(neut_count[0]) + "," + str(neg_count[0]) + "]"
html += '''
]);
var options = {
'title': 'Sentiment Classification',
'colors': ['#04B404', '#6E6E6E', '#FF0000'],
'titleTextStyle': {'fontSize': 15},
'vAxis': {'textStyle': {'fontSize': 15}},
'hAxis': {'textStyle': {'fontSize': 15}, 'slantedText': true, 'slantedTextAngle': 30}
};
var chart = new google.visualization.LineChart(document.getElementById('result-big-chart'));
chart.draw(data, options);
}
'''
checked1 = 'document.getElementById("'+checked+'").checked=true;'
checked2 = 'document.getElementById("'+time+'").checked=true;'
textValue = 'document.getElementById("keyword").value="'+keyword+'";'
html += checked1 + checked2 + textValue
html += '''
function checkEmpty(f) {
if (f.keyword.value === "") {
alert('Please enter a valid keyword');
return false;
}else{
f.submit();
return true;
}
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31119754-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
'''
return html
#end
#end class