-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.html
More file actions
217 lines (175 loc) · 6.42 KB
/
twitter.html
File metadata and controls
217 lines (175 loc) · 6.42 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ConveyAPI - Simple Example</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
font-size: 14px;
line-height: 20px;
}
</style>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="index.html">ConveyAPI</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="document.html">Document Example</a></li>
<li class="active"><a href="twitter.html">Twitter Example</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<h1>Try it: Analyze Twitter</h1>
<p>Below is an example of using ConveyAPI with the Twitter Search API.</p>
<form class="well form-horizontal">
<div class="control-group">
<label class="control-label">API Key:</label>
<div class="controls">
<input id="apiKey" type="text" class="span4" placeholder="Your ConveyAPI Key...">
</div>
</div>
<div class="control-group">
<label class="control-label">Twitter Search:</label>
<div class="controls">
<input id="terms" type="text" class="span4" placeholder="Search Terms...">
</div>
</div>
<div class="form-actions">
<button id="submit" type="button" class="btn btn-primary">Search</button>
<button id="clear" class="btn" type="button">Clear</button>
</div>
</form>
<div class="page-header">
<div class="pull-right">
<span class="badge badge-success">positive <span id="positiveCount"></span></span>
<span class="badge badge-warning">neutral <span id="neutralCount"></span></span>
<span class="badge badge-important">negative <span id="negativeCount"></span></span>
</div>
<br/>
</div>
<div id="tweets"></div>
<h2>Source Code</h2>
<p>To use ConveyAPI in with jQuery, make sure to use the 'callback' parameter in order to have your response returned as JSONP (JSON with Padding).</p>
<pre>
var apiUrl = "http://beta.conveyapi.com/analysis-engine/process?callback=?";
var searchUrl = "http://search.twitter.com/search.json?callback=?";
...
function search(terms) {
clear();
$.getJSON(searchUrl,
{ q: terms, rpp: 100, result_type: "recent"},
function(jsonData) {
$.each(jsonData.results, function(i, tweet){
process(tweet);
})
});
}
function process(tweet) {
$.getJSON(apiUrl,
{ api_key: $("#apiKey").val(), text: tweet.text},
function(jsonData) {
var annotations = jsonData.document.annotations;
...
});
}
...
</pre>
</div> <!-- /container -->
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery-1.7.2.min.js"></script>
<script>
var apiUrl = "http://beta.conveyapi.com/analysis-engine/process?callback=?";
var searchUrl = "http://search.twitter.com/search.json?callback=?";
var positiveCount = 0;
var negativeCount = 0;
var neutralCount = 0;
var totalCount = 0;
$("#submit").click(function () {
search($("#terms").val());
});
$("#clear").click(function () {
clear();
});
function search(terms) {
clear();
$.getJSON(searchUrl,
{ q: terms, rpp: 100, result_type: "recent", lang: "en"},
function(jsonData) {
$.each(jsonData.results, function(i, tweet){
process(tweet);
})
});
}
function process(tweet) {
$.getJSON(apiUrl,
{ api_key: $("#apiKey").val(), text: tweet.text},
function(jsonData) {
var annotations = jsonData.document.annotations;
var annotationList = $("<dl class='dl-horizontal'/>");
appendAnnotation(annotationList, "polarity", annotations.polarity);
appendAnnotation(annotationList, "emotion", annotations.emotion);
appendAnnotation(annotationList, "intensity", annotations.intensity);
$("#tweets").prepend(
$("<blockquote/>").append(
$("<p/>").html(tweet.text),
$("<small/>").append($("<strong/>").text(tweet.from_user_name), " @" + tweet.from_user),
annotationList));
updateTotals(annotations.polarity.value);
});
}
function updateTotals(polarity){
switch(polarity) {
case "positive":
positiveCount++;
break;
case "neutral":
neutralCount++;
break;
case "negative":
negativeCount++;
break;
}
totalCount++;
$("#positiveCount").text(positiveCount + " (" + (100 * positiveCount / totalCount).toFixed(0) + "%)");
$("#neutralCount").text(neutralCount + " (" + (100 * neutralCount / totalCount).toFixed(0) + ")%");
$("#negativeCount").text(negativeCount + " (" + (100 * negativeCount / totalCount).toFixed(0) + "%)");
}
function appendAnnotation(list, name, annotation) {
var span = $("<span class='label'/>");
switch(annotation.value) {
case "positive":
span.addClass("label-success");
break;
case "neutral":
span.addClass("label-warning");
break;
case "negative":
span.addClass("label-important");
break;
}
list.append(
$("<dt/>").text(name + ":"),
$("<dd/>").append(span.text(annotation.value +
" (" + annotation.confidence.toFixed(2) + ")")));
}
function clear() {
positiveCount = 0;
negativeCount = 0;
neutralCount = 0;
totalCount = 0;
updateTotals("");
$("#tweets").empty();
}
</script>
</body>
</html>