-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiObjects.js
More file actions
244 lines (223 loc) · 10.5 KB
/
apiObjects.js
File metadata and controls
244 lines (223 loc) · 10.5 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
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
// object used to store all info about the Alchemy API: http://www.alchemyapi.com/api/sentiment/textc.html
var alchemyObject = {
apikey : '&apikey='+sentAnalysisKey,
textPrefix : 'text=',
textURI : "null",
outputMode : '&outputMode=json',
showSourceText : '&showSourceText=0',
//all info specific to Sentiment-Analysis Call
sentimentAnalysis : {
endpoint : 'http://gateway-a.watsonplatform.net/calls/text/TextGetTextSentiment?',
baseQuery : "Null"
},
//all info about the Keyword-Level Sentiment Analysis Call - NOT USED AT THIS POINT
// keywordSentimentAnalysis : {
// endpoint : 'http://gateway-a.watsonplatform.net/calls/url/URLGetTextSentiment?'
// },
makeAjaxRequest : function(){
var alchemyQueryString = alchemyObject.textPrefix+alchemyObject.textURI+alchemyObject.sentimentAnalysis.baseQuery;
ajaxObject.ajaxCall(alchemyObject.sentimentAnalysis.endpoint,alchemyQueryString,DEMO.ms_Ocean.setOceanValue);
}
};
// sets base query in sentiment Analysis Call
(function (){ return alchemyObject.sentimentAnalysis.baseQuery = alchemyObject.apikey + alchemyObject.outputMode+ alchemyObject.showSourceText;}());
controllerHackObject = {
elWindX : document.querySelector('ul li:nth-child(3) .slider'),
WindXPos : 'null',
WindYPos : 'null',
getWindXPos : function(){
controllerHackObject.WindXPos = controllerHackObject.elWindX.getBoundingClientRect();
// console.log(controllerHackObject.WindXPos.top, controllerHackObject.WindXPos.right, controllerHackObject.WindXPos.bottom, controllerHackObject.WindXPos.left);
},
getWindYPos : function(){
controllerHackObject.WindYPos = controllerHackObject.elWindX.getBoundingClientRect();
},
windXClick : function(score){
//defining click positions in x and y
var x = score+controllerHackObject.WindXPos.left;
var y = (controllerHackObject.WindXPos.top + controllerHackObject.WindXPos.bottom)/2;
// simulating click
simulate(document.querySelector('ul li:nth-child(3) .slider-fg'), "mousedown", { pointerX: x , pointerY: y });
simulate(document.querySelector('ul li:nth-child(3) .slider-fg'), "mouseup", { pointerX: x , pointerY: y });
},
windYClick : function(score){
//defining click positions in x and y
var x = score+controllerHackObject.WindYPos.left;
var y = (controllerHackObject.WindYPos.top + controllerHackObject.WindYPos.bottom)/2;
// simulating click
simulate(document.querySelector('ul li:nth-child(4) .slider-fg'), "mousedown", { pointerX: x , pointerY: y });
simulate(document.querySelector('ul li:nth-child(4) .slider-fg'), "mouseup", { pointerX: x , pointerY: y });
},
setToNeutral : function(){
//getting middle coordinates
var xX = (controllerHackObject.WindXPos.left+controllerHackObject.WindXPos.right)/2;
var yX = (controllerHackObject.WindXPos.top + controllerHackObject.WindXPos.bottom)/2;
var xY = (controllerHackObject.WindYPos.left+controllerHackObject.WindYPos.right)/2;
var yY = (controllerHackObject.WindYPos.top + controllerHackObject.WindYPos.bottom)/2;
// clicking on correct points
//for windX
simulate(document.querySelector('ul li:nth-child(3) .slider-fg'), "mousedown", { pointerX: xX , pointerY: yX });
simulate(document.querySelector('ul li:nth-child(3) .slider-fg'), "mouseup", { pointerX: xX , pointerY: yX });
//for windY
simulate(document.querySelector('ul li:nth-child(4) .slider-fg'), "mousedown", { pointerX: xY , pointerY: yY });
simulate(document.querySelector('ul li:nth-child(4) .slider-fg'), "mouseup", { pointerX: xY , pointerY: yY });
}
};
// expanding on the existing DEMO.ms_Ocean object set in Index and defined in Three.js example
DEMO.ms_Ocean.setOceanValue = function(alchemyResponse){
// checking if alchemy returns an error
try {
if (alchemyResponse.docSentiment.type){
}
} catch(error) {
// its not json
return $('.neutral-result').text('Keyword(s) chosen did not yield a response.');
}
//warning user if neutral result + set sea to neutral
if (alchemyResponse.docSentiment.type === 'neutral' || (alchemyResponse.docSentiment.score <0.1 && alchemyResponse.docSentiment.score>-0.1)
){
DEMO.ms_Ocean.choppiness = 0.1;
DEMO.ms_Ocean.exposure = 0.45;
// getting wind x and y controller box position
controllerHackObject.getWindXPos();
controllerHackObject.getWindYPos();
// clicking on controller
controllerHackObject.setToNeutral();
$('.neutral-result').text('result: neutral sentiment');
} else {
// console.log('going in else');
var score = alchemyResponse.docSentiment.score;
console.log('SENTIMENT ANALYSIS SCORES: ',score);
DEMO.ms_Ocean.choppiness = DEMO.ms_Ocean.valueConvert(1,-1,4,0.1,score);
DEMO.ms_Ocean.exposure = DEMO.ms_Ocean.valueConvert(1,-1,0.5,0,score);
// getting wind x and y controller box position
controllerHackObject.getWindXPos();
controllerHackObject.getWindYPos();
// converting it to math scale - proper % of controller width
var scaledXtoControl = DEMO.ms_Ocean.valueConvert(1,-1, controllerHackObject.WindXPos.width,0,score);
// clicking on controller
controllerHackObject.windXClick(scaledXtoControl);
controllerHackObject.windYClick(scaledXtoControl);
}
};
DEMO.ms_Ocean.valueConvert = function(OldMax, OldMin, NewMax, NewMin, OldValue){
// console.log('returning value,',(((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin);
return ((((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin);
};
// object used to store all info about the Alchemy API: http://www.alchemyapi.com/api/sentiment/textc.html
var nyTimesObject = {
apikey : '&api-key='+nyTimesArticleKey,
endpoint : 'http://api.nytimes.com/svc/search/v2/articlesearch.json?',
fl : '&fl=abstract,snippet',
textToSearch : 'null',
searchPrefix : 'q=',
baseQuery : "null",
constructStringForSentimentAnalysis : function(nyArticles){
var stringArticles = "";
// building string with abstract, and if abstract === null, using snippet instead
for (var i=0; i<nyArticles.response.docs.length; i++){
if (nyArticles.response.docs[i].abstract !== null){
stringArticles += nyArticles.response.docs[i].abstract;
} else {
stringArticles += nyArticles.response.docs[i].snippet;
}
}
// storing encoded URI of aggregate articles into alchemy object
alchemyObject.textURI = encodeURI(stringArticles);
// making ajax Call to Sentiment Analysis API
alchemyObject.makeAjaxRequest();
}
};
// sets base query in sentiment Analysis Call
(function (){ return nyTimesObject.baseQuery = nyTimesObject.fl + nyTimesObject.apikey;}());
// object used to store all info about the wikiMedia API:
var wikiObject = {
endpoint: "http://en.wikipedia.org/w/api.php?" +
"action=query&" +
"prop=revisions&" +
"rvprop=content&" +
"rvexpandtemplates&" +
"format=json&" +
"callback=callback&" +
"indexpageids&" +
"redirects&" +
"titles=",
textToSearch : 'null',
};
var guardianObject = {
endpoint: 'http://content.guardianapis.com/search?api-key='+guardianKey+'&q=',
textToSearch : 'null',
constructStringForSentimentAnalysis : function(guardianArticles){
var stringArticles = "";
// building string with abstract, and if abstract === null, using snippet instead
for (var i=0; i<guardianArticles.response.results.length; i++){
stringArticles += guardianArticles.response.results[i].webTitle;
}
// storing encoded URI of aggregate articles into alchemy object
alchemyObject.textURI = encodeURI(stringArticles);
// making ajax Call to Sentiment Analysis API
alchemyObject.makeAjaxRequest();
}
};
// anythin to do with the Die Zeit Api
var zeitObject = {
textToSearch : 'null',
endpoint: 'http://api.zeit.de/content?api_key='+zeitKey+'&limit=60&q=',
constructStringForSentimentAnalysis : function(zeitArticles){
var stringArticles = "";
// building string with abstract, and if abstract === null, using snippet instead
for (var i=0; i<zeitArticles.matches.length; i++){
stringArticles += zeitArticles.matches[i].snippet;
}
// making sure stringArticles not more than 3500 (exceed limit for alchemyAPI)
if (stringArticles.length>3500){
stringArticles = stringArticles.slice(0, 3500);
}
// storing encoded URI of aggregate articles into alchemy object
alchemyObject.textURI = encodeURI(stringArticles);
// making ajax Call to Sentiment Analysis API
alchemyObject.makeAjaxRequest();
}
};
// aything to do with the Yandex API (translation api)
var yandexObject = {
textToTranslate : 'null',
endpoint: 'https://translate.yandex.net/api/v1.5/tr.json/translate?key='+yandexKey+'&lang=en-de&format=plain&text=',
makeAjaxCalltoZeit : function(translatedData){
zeitObject.textToSearch = translatedData.text[0];
ajaxObject.ajaxCall(zeitObject.endpoint,zeitObject.textToSearch,zeitObject.constructStringForSentimentAnalysis,"" );
}
};
// anything to do with API's in general
var allApiObject = {
// function that selects which API's to store the submitted values
selectApiToCall : function(apiToSearch){
switch (apiToSearch) {
case 'NY Times':
// storing the keyword to search in nyTimes object
nyTimesObject.textToSearch = encodeURIComponent($('#keyword').val());
// building specific query
var specificQuery = nyTimesObject.searchPrefix+nyTimesObject.textToSearch+nyTimesObject.baseQuery;
// making ajax call
ajaxObject.ajaxCall(nyTimesObject.endpoint,specificQuery,nyTimesObject.constructStringForSentimentAnalysis, "");
break;
case 'Wikipedia':
// storing the keyword to search in wikipedia object
wikiObject.textToSearch = encodeURIComponent($('#keyword').val());
// making ajax calls. ps: callback is a function defined in query.js
ajaxObject.ajaxCall(wikiObject.endpoint,wikiObject.textToSearch,callback, 'JSONP');
break;
case 'Guardian':
// storing the keyword to search in guardian object
guardianObject.textToSearch = encodeURIComponent($('#keyword').val());
// making ajax call
ajaxObject.ajaxCall(guardianObject.endpoint,guardianObject.textToSearch,guardianObject.constructStringForSentimentAnalysis, '');
break;
case 'Die Zeit':
yandexObject.textToTranslate = encodeURIComponent($('#keyword').val());
// making ajax call to yandex api to translate word in german
ajaxObject.ajaxCall(yandexObject.endpoint, yandexObject.textToTranslate, yandexObject.makeAjaxCalltoZeit, "");
break;
}
}
};