-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sidebar.html
515 lines (465 loc) · 16.7 KB
/
Sidebar.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<!-- The CSS package above applies Google styling to buttons and other elements. -->
<style>
.branding-below {
bottom: 56px;
top: 0;
}
.branding-text {
left: 7px;
position: relative;
top: 3px;
}
h5.PERSON{
color: #E93824;
}
h5.LOCATION {
color: #2CAAE2;
}
h5.ORGANISATION {
color: #FEC238;
}
.col-contain {
overflow: hidden;
}
.col-one {
float: left;
width: 50%;
}
.logo {
vertical-align: middle;
}
.radio-spacer {
height: 20px;
}
.width-100 {
width: 100%;
}
</style>
</head>
<body>
<div class="sidebar branding-below">
<img src="https://s3.amazonaws.com/rosette-api/rosette-logo.jpg" style="width:250px;"/>
<div class="block">
<input type="checkbox" id="save-prefs">
<label for="save-prefs">Save selections</label>
</div>
<div class="block col-contain">
<div>
<label for="key"> API Key</label>
<input type="text" name="key" id="key">
</div>
</div>
<br>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#entities" aria-controls="entities" role="tab" data-toggle="tab">Entities</a></li>
<li role="presentation"><a href="#wiki" aria-controls="wiki" role="tab" data-toggle="tab">Wiki</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content"> <div role="tabpanel" class="tab-pane active" id="entities">
<br>
<div class="block">
<div>
Before running, <a href = "https://support.google.com/docs/answer/65166?hl=en">ensure text direction is correct</a>
</div>
<br>
<label for="sourceLang">Source Language (optional)</label>
<div>
<select id="sourceLang" name="sourceLang" class="form-control col-sm-2">
<option value="auto">Auto</option>
<option value="ara">Arabic</option>
<option value="zho">Chinese</option>
<option value="nld">Dutch</option>
<option value="eng">English</option>
<option value="fra">French</option>
<option value="deu">German</option>
<option value="heb">Hebrew</option>
<option value="ind">Indonesian</option>
<option value="ita">Italian</option>
<option value="jpn">Japanese</option>
<option value="kor">Korean</option>
<option value="pus">Pashto</option>
<option value="fas">Persian</option>
<option value="por">Portugese</option>
<option value="rus">Russian</option>
<option value="spa">Spanish</option>
<option value="urd">Urdu</option>
</select>
</div>
<br>
<label for="targetLang">Target Language</label>
<div>
<select id="targetLang" name="targetLang" class="form-control">
<option value="ara">Arabic</option>
<option value="zho">Chinese</option>
<option value="nl">Dutch</option>
<option value="eng">English</option>
<option value="fr">French</option>
<option value="de">German</option>
<option value="he">Hebrew</option>
<option value="id">Indonesian</option>
<option value="it">Italian</option>
<option value="ja">Japanese</option>
<option value="kor">Korean</option>
<option value="pus">Pashto</option>
<option value="fas">Persian</option>
<option value="pt">Portugese</option>
<option value="rus">Russian</option>
<option value="es">Spanish</option>
<option value="ur">Urdu</option>
</select>
</div>
</div>
<br>
<label>Insert translation</label>
<div class="block">
<input type="checkbox" id="insertPerson">
<label style="color:#E93824;" for="insertPerson">Person</label>
<div style="font-size:12px;" id="peopleDiv">
</div>
</div>
<div class="block">
<input type="checkbox" id="insertLocation">
<label style="color:#2CAAE2;" for="insertLocation">Location</label>
<div style="font-size:12px;" id="locDiv">
</div>
</div>
<div class="block">
<input type="checkbox" id="insertOrganisation">
<label style="color:#FEC238;" for="insertOrganisation">Organization</label>
<div style="font-size:12px;" id="orgDiv">
</div>
</div>
<br>
<div class="block" id="button-bar">
<button class="blue" id="run-analysis">Analyze</button>
</div>
<div class="block form-group">
<p id="loading"></p>
</div>
<div class="block form-group">
<textarea style="display:none;" class="width-100" id="translated-text" rows="10"></textarea>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="wiki">
<br>
<div class="block">
<label for="sourceLangWiki">Source Language (optional)</label>
<div>
<select id="sourceLangWiki" name="sourceLangWiki" class="form-control col-sm-2">
<option value="auto">Auto</option>
<option value="ara">Arabic</option>
<option value="zho">Chinese</option>
<option value="eng">English</option>
<option value="spa">Spanish</option>
</select>
</div>
</div>
<br>
<div class="block" id="button-bar">
<button class="blue" id="get-wiki">Get wiki data</button>
<button class="blue" id="clear-wiki">Clear</button>
</div>
<br>
<div id="titlepanel">
</div>
<div id="datapanel">
</div>
<div id="paragraphpanel">
</div>
<br>
<div id="titlepanel2">
</div>
<div id="datapanel2">
</div>
<div id="paragraphpanel2">
</div>
<br>
<div class="block" id="wiki-error">
</div>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
/**
* On document load, assign click handlers to each button and try to load the
* user's origin and destination language preferences if previously set.
*/
$(function() {
$('#get-wiki').click(getWikiData);
$('#run-analysis').click(runEntityExtraction);
$('#run-analysis').click(orientDocument);
$('#clear-wiki').click(clearWikiSpace);
});
/**
* Callback function that populates the origin and destination selection
* boxes with user preferences from the server.
*
* @param {Object} languagePrefs The saved origin and destination languages.
*/
function loadNameTranslationPreferences(userPrefs) {
$('#key').val(userPrefs.key);
$('#sourceLang').val(userPrefs.sourceLanguage);
$('#targetLang').val(userPrefs.targetLanguage);
$('#insertPerson').prop("checked", userPrefs.insertPerson);
$('#insertLocation').prop("checked", userPrefs.insertLocation);
$('#insertOrganisation').prop("checked", userPrefs.insertOrganisation);
$('#sourceLangWiki').val(userPrefs.sourceLangWiki);
}
function orientDocument(){
this.disabled = true;
var sourceLang = $('#sourceLang').val();
google.script.run.documentOrientation(sourceLang);
this.disabled = false;}
/**
* Runs a server-side function to translate the user-selected text and update
* the sidebar UI with the resulting translation.
*/
function admResults() {
this.disabled = true;
$('#error').remove();
var key = $('#key').val();
var savePrefs = $('#save-prefs').is(':checked');
google.script.run
.withSuccessHandler(
function(translatedText, element) {
$('#translated-text').val(translatedText);
var JSONobj = JSON.parse(translatedText);
try{
var entitiesArray = JSONobj["attributes"]["entityMentions"]["items"];
for (var entCount = 0; entCount < entitiesArray.length; entCount++) {
google.script.run.colorText(entitiesArray[entCount]["normalized"], entitiesArray[entCount]["entityType"]);
}
nameInsertion();
google.script.run.withSuccessHandler(loadNameTranslationPreferences).withFailureHandler(showError).getPreferences();
}
catch(err){
$('#translated-text').val(null);
notLinkedInsertion();
}
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.admResults(key, savePrefs);
}
/**
* Runs a server-side function to insert the translated text into the document
* at the user's cursor or selection.
*/
function nameInsertion() {
this.disabled = true;
$('#error').remove();
var key = $('#key').val();
var sourceLanguage = $('#sourceLang').val();
var targetLanguage = $('#targetLang').val();
var insertPers = $('#insertPerson').is(':checked');
var insertLoc = $('#insertLocation').is(':checked');
var insertOrg = $('#insertOrganisation').is(':checked');
var savePrefs = $('#save-prefs').is(':checked');
google.script.run
.withSuccessHandler(
function(returnSuccess, element) {
$('#loading').text('');
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
$('#loading').text('');
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.nameInsertion(key, $('#translated-text').val(), sourceLanguage, targetLanguage, insertPers, insertLoc, insertOrg, savePrefs,true);
}
function notLinkedInsertion() {
this.disabled = true;
$('#error').remove();
var key = $('#key').val();
var sourceLanguage = $('#sourceLang').val();
var targetLanguage = $('#targetLang').val();
var insertPers = $('#insertPerson').is(':checked');
var insertLoc = $('#insertLocation').is(':checked');
var insertOrg = $('#insertOrganisation').is(':checked');
var savePrefs = $('#save-prefs').is(':checked');
google.script.run
.withSuccessHandler(
function(entities, element) {
$('#loading').text('');
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
$('#loading').text('');
showError(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.notLinkedInsertion(key, sourceLanguage, targetLanguage, insertPers, insertLoc, insertOrg, savePrefs);
}
/**
* Runs a server-side function to translate the user-selected text and update
* the sidebar UI with the resulting translation.
*/
function runEntityExtraction() {
$('#loading').text('Processing...');
$('#peopleDiv').html("");
$('#locDiv').html("");
$('#orgDiv').html("");
$('#error').remove();
google.script.run
.withSuccessHandler(
function(anotatedText) {
var JSONobj = JSON.parse(anotatedText);
var entitiesArray = JSONobj.entities;
console.log(entitiesArray);
var orgString = "";
var natString = "";
var titleString = "";
var insertPers = $('#insertPerson').is(':checked');
var insertLoc = $('#insertLocation').is(':checked');
var insertOrg = $('#insertOrganisation').is(':checked');
var targetLanguage = $('#targetLang').val();
var sourceLanguage = $('#sourceLang').val();
for (var entCount = 0; entCount < entitiesArray.length; entCount++)
{
google.script.run.colorText (entitiesArray[entCount].mention, entitiesArray[entCount].type);
var originalNameObj = new Object;
originalNameObj["name"] = entitiesArray[entCount].mention;
switch (entitiesArray[entCount].type)
{
case "PERSON":
google.script.run
.withSuccessHandler(function(NAMEobj) {
var peopleString = $('#peopleDiv').html();
//peopleString = peopleString + NAMEobj.originalName + ' (' + NAMEobj.translation+")<br/> ";
peopleString = peopleString + NAMEobj+"<br/>";
$('#peopleDiv').html(peopleString);
}
)
.withFailureHandler(function(msg, element) {
var peopleString = $('#peopleDiv').html();
peopleString = peopleString + element.name + ' (no_translation)' + '<br/>';
$('#peopleDiv').html(peopleString);
}
)
.withUserObject(originalNameObj)
.runNameTranslationGS (entitiesArray[entCount].mention, entitiesArray[entCount].type, $('#key').val(), sourceLanguage, targetLanguage);
break;
case "LOCATION":
google.script.run
.withSuccessHandler(function(NAMEobj) {
var locString = $('#locDiv').html();
//locString = locString + NAMEobj.originalName + ' (' + NAMEobj.translation+")<br/> ";
locString = locString + NAMEobj+"<br/>";
$('#locDiv').html(locString);
}
)
.withFailureHandler(function(msg, element) {
var locString = $('#locDiv').html();
locString = locString + element.name + ' (no_translation)' + '<br/>';
$('#locDiv').html(locString);
}
)
.withUserObject(originalNameObj)
.runNameTranslationGS (entitiesArray[entCount].mention, entitiesArray[entCount].type, $('#key').val(), sourceLanguage, targetLanguage);
break;
case "ORGANIZATION":
google.script.run
.withSuccessHandler(function(NAMEobj) {
var orgString = $('#orgDiv').html();
//orgString = orgString + NAMEobj.originalName + ' (' + NAMEobj.translation+")<br/> ";
orgString = orgString + NAMEobj+"<br/>";
$('#orgDiv').html(orgString);
}
)
.withFailureHandler(function(msg, element) {
var orgString = $('#orgDiv').html();
orgString = orgString + element.name + ' (no_translation)'+ '<br/>';
$('#orgDiv').html(orgString);
}
)
.withUserObject(originalNameObj)
.runNameTranslationGS (entitiesArray[entCount].mention, entitiesArray[entCount].type, $('#key').val(), sourceLanguage, targetLanguage);
break;
}
}
admResults();
//$('#loading').text('');
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#error-box'));
this.disabled = false;
$('#loading').text('');
}).withUserObject(this)
.runEntityExtractionGS($('#key').val());
}
/**
* Inserts a div that contains an error message after a given element.
*
* @param msg The error message to display.
* @param element The element after which to display the error.
*/
function showError(msg, element) {
var div = $('<div id="error" class="error">' + msg + '</div>');
$(element).after(div);
$('#loading').text('');
}
/**
* Runs a server-side function to translate the user-selected text and update
* the sidebar UI with the resulting translation.
*/
function getWikiData() {
this.disabled = true;
$('#error').remove();
var sourceLangWiki = $('#sourceLangWiki').val();
google.script.run
.withSuccessHandler(
function(wikiText, element) {
clearWikiSpace();
$('#titlepanel').html(wikiText[0][0]);
$('#datapanel').html(wikiText[1][0]);
$('#paragraphpanel').html(wikiText[2][0]);
$('#titlepanel2').html(wikiText[3][0]);
$('#datapanel2').html(wikiText[4][0]);
$('#paragraphpanel2').html(wikiText[5][0]);
element.disabled = false;
}
)
.withFailureHandler(
function(msg, element) {
showError(msg, $('#wiki-error'));
element.disabled = false;
}
)
.withUserObject(this)
.getWikiData($('#key').val(), sourceLangWiki);
}
function clearWikiSpace(){
$('#datapanel').html('');
$('#paragraphpanel').html('');
$('#titlepanel').html('');
$('#datapanel2').html('');
$('#paragraphpanel2').html('');
$('#titlepanel2').html('');
}
</script>
</body>
</html>