-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (176 loc) · 6.36 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Tweet Sentiment to CSV</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/paper/bootstrap.min.css" rel="stylesheet" integrity="sha384-awusxf8AUojygHf2+joICySzB780jVvQaVCAt1clU3QsyAitLGul28Qxb2r1e5g+" crossorigin="anonymous">
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div class ="container jumbotron">
<div class='container-fluid'>
<div class='text-center'>
<h1>Tweets Analyzer</h1>
<h5>Search for Tweets and download the data labeled with it's Polarity in CSV format</h5>
<div id="search_box" class="container-fluid">
<input type="text" id="query">
<button id="search">Search</button>
<button id="getcsv">Download CSV</button>
<div id="loading">Loading...</div>
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
</div>
</div>
<div id="tweets"></div>
<footer class="text-center">
Made with ❤ | <a href="https://github.com/vipernitj" target='_blank'>Github</a> | <a href="https://www.linkedin.com/in/abhishek-verma-484296144/" target='_blank'>Abhishek Verma</a>
</footer> </div>
</div>
<script type="text/javascript">
var current_data;
function sentiment_to_color(sentiment){
if(sentiment == 'positive') return 'panel-success';
else if(sentiment == 'negative') return 'panel-danger';
else return 'panel-primary';
}
function load_tweets(querystring){
$.ajax({
url: 'tweets',
data: {'query': querystring, 'retweets_only': 'false', 'with_sentiment': 'true'},
dataType: 'json',
type: 'GET',
success: function(data) {
buildChart(data);
current_data = data['data'];
var tweets = data['data'];
var container = $('#tweets');
var contents = '';
contents+='<div>'
for(i = 0; i < tweets.length; i++){
contents+= '<div class="panel '+ sentiment_to_color(tweets[i].sentiment) +'"> <div class="panel-heading"> <h3 class="panel-title">'+ tweets[i].user +'</h3> </div> <div class="panel-body"><blockquote>'+ tweets[i].text + '</blockquote> </div> </div>'
// contents += '<li class="list-group-item '+ sentiment_to_color(tweets[i].sentiment) +'">'+ tweets[i].user + ": " + tweets[i].text + '</li>';
}
contents+='</div>';
container.html(contents);
$('#query').val(querystring);
$('#loading').html(data['count'] + " Tweets loaded about "+ querystring +".");
}
});
}
function get_csv(json){
var fields = Object.keys(json[0]);
var csv = json.map(function(row){
return fields.map(function(fieldName){
return JSON.stringify(row[fieldName] || '');
});
});
csv.unshift(fields);
return csv.join('\r\n');
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
$(document).ready(function(){
load_tweets('Data Science');
});
$('#search').click(function(){
$('#loading').html('Loading...');
$('#tweets').html('');
load_tweets($('#query').val());
});
$('#getcsv').click(function(){
download('data.csv', get_csv(current_data));
});
function buildChart(data) {
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'last 100 tweets on '+$('#query').val()
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: getPercentage(data)
});
};
function getNegativePercentage(data) {
var current_data = data['data'];
var counter = 0 ;
for (var i = current_data.length - 1; i >= 0; i--) {
if(current_data[i].sentiment == 'negative')
counter ++;
}
console.log('negative',counter)
return counter/data.count;
}
function getPositivePercentage(data) {
var current_data = data['data'];
var counter = 0 ;
for (var i = current_data.length - 1; i >= 0; i--) {
if(current_data[i].sentiment == 'positive')
counter ++;
}
console.log('positive',counter)
return counter/data.count;
}
function getNeutralPercentage(data) {
var current_data = data['data'];
var counter = 0 ;
for (var i = current_data.length - 1; i >= 0; i--) {
if(current_data[i].sentiment == 'neutral')
counter ++;
}
console.log('neutral',counter)
return counter/data.count;
}
function getPercentage(data) {
var neutral = getNeutralPercentage(data);
var positive = getPositivePercentage(data);
var negative = getNegativePercentage(data);
return [{
name: 'Tweets',
//colorByPoint: true,
data: [{
name: 'Positive',
y: positive
}, {
name: 'Negative',
y: negative,
sliced: true,
selected: true
}, {
name: 'Neutral',
y: neutral
}]
}]
// body...
}
</script>
</body>
</html>