-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisutils.js
568 lines (531 loc) · 21.3 KB
/
visutils.js
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Generated by IcedCoffeeScript 1.3.3f
(function() {
var addFakePOSTags, addIdsToHierarchy, addSentence, addSentences, arrayToObj, callOnceElementAvailable, deserializeArray, getChildrenOfId, getMaxDepth, getUrlParameters, hierarchyToTerminals, hierarchyWithIdToTerminals, initializeHover, initializePopup, makeDivs, objToArray, openTranslationPopup, renderSentence, root, serializeArray, submitTranslation, synthesizeSpeech, updateTranslation;
root = typeof exports !== "undefined" && exports !== null ? exports : this;
(function($) {
var depthToColor;
depthToColor = function(depth) {
var colors, i;
colors = '00000000000012345678ABCDEF';
return '#' + ((function() {
var _i, _results;
_results = [];
for (i = _i = 0; _i <= 5; i = ++_i) {
_results.push(colors[colors.length - depth - 1]);
}
return _results;
})()).join('');
};
$.fn.borderStuff = function(depth, maxdepth, color) {
var fontSize, lang, margin, padding, posTag, width;
width = 3;
/*
depth = $(this).parents().attr('depth')
console.log this.parent()
if depth?
depth = parseInt(depth) + 1
else
depth = 0
*/
padding = 8;
fontSize = 18;
lang = this.attr('foreignLang');
if (lang === 'zh' || lang === 'ja') fontSize = 32;
margin = 0;
if (color === 'white') {
margin = (maxdepth - depth + 1) * padding + (maxdepth - depth);
}
if (!(color != null)) color = depthToColor(depth);
posTag = deserializeArray(this.attr('contenthierarchy')).pos;
if ((posTag != null) && (posTag === 'N' || posTag === 'NN')) {
color = 'lightgreen';
}
if ((posTag != null) && (posTag === 'V' || posTag === 'VC')) color = 'pink';
this.addClass('bordered').css('position', 'relative').css('padding', padding + 'px').css('font-size', fontSize).attr('color', color).css('background-color', color).css('border-width', 1).css('border-style', 'solid').css('float', 'left').attr('depth', depth).css('border-color', 'black').css('border-radius', '10px').css('margin-top', margin).css('margin-bottom', margin);
if (this.attr('id') && this.attr('id').indexOf('_') === -1) {
this.css('margin-top', $('#H' + this.attr('id')).height());
}
return this;
};
$.fn.showAsSibling = function(color) {
var setWidth, siblingToShow,
_this = this;
if (color != null) this.css('background-color', color);
siblingToShow = $('#H' + this.attr('id'));
siblingToShow.show();
this.addClass('hovered');
setWidth = function() {
var left, ownHalfWidth, siblingHalfWidth, top;
ownHalfWidth = _this.width() / 2;
siblingHalfWidth = siblingToShow.width() / 2;
left = Math.max(0, ownHalfWidth - siblingHalfWidth);
top = -siblingToShow.height();
siblingToShow.css('left', left);
return siblingToShow.css('top', top);
};
setWidth();
siblingToShow.mouseover(function() {
return false;
});
return this;
};
return $.fn.hoverId = function() {
var idNum, shortTranslation, shortTranslationDiv, text, textAsHtml, x, _i, _len, _ref,
_this = this;
text = this.attr('translation');
if (!(text != null)) return this;
if (text.indexOf('/EntL') !== -1) {
text = text.slice(0, text.indexOf('/EntL'));
}
idNum = this.attr('id');
textAsHtml = $('<div>');
_ref = text.split('\n');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
textAsHtml.append($('<span>').text(x)).append('<br>');
}
if (text.indexOf('\n') !== -1) {
this.attr('title', text);
this.attr('hovertext', text);
this.tooltip({
track: true,
show: false,
hide: false,
content: textAsHtml.html()
});
}
shortTranslation = text;
if (shortTranslation.indexOf('\n') !== -1) {
shortTranslation = shortTranslation.slice(0, shortTranslation.indexOf('\n'));
}
shortTranslationDiv = $('<div>').addClass('Hovertips').attr('id', 'H' + idNum).text(shortTranslation).css('position', 'absolute').css('top', 0).css('zIndex', 100).css('color', 'white').css('background-color', 'black').css('border-top-left-radius', 5).css('border-top-right-radius', 5).css('font-size', 18).hide();
shortTranslationDiv.css('text-align', 'center').css('word-wrap', 'break-word');
this.append(shortTranslationDiv);
this.mouseover(function() {
var currentId, parent, sibling, siblings, _j, _k, _len1, _len2, _ref1;
console.log(_this.attr('foreigntext'));
synthesizeSpeech(_this.attr('foreigntext'), _this.attr('foreignlang'));
_this.css('background-color', 'yellow');
_ref1 = $('.bordered');
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
x = _ref1[_j];
$(x).css('background-color', $(x).attr('color'));
}
_this.addClass('hovered');
currentId = idNum;
$('.Hovertips').hide();
while (currentId.indexOf('_') !== -1) {
parent = currentId.split('_').slice(0, -1).join('_');
siblings = (function() {
var _k, _len2, _ref2, _results;
_ref2 = getChildrenOfId(parent);
_results = [];
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
x = _ref2[_k];
if (x !== currentId) _results.push(x);
}
return _results;
})();
for (_k = 0, _len2 = siblings.length; _k < _len2; _k++) {
sibling = siblings[_k];
$('#' + sibling).showAsSibling();
}
currentId = parent;
}
_this.showAsSibling('yellow');
_this.css('background-color', 'yellow');
return false;
});
this.mouseleave(function() {
var myId, rootId, _j, _len1, _ref1;
if ($('audio')[0] != null) $('audio')[0].pause();
_ref1 = $('.hovered');
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
x = _ref1[_j];
$(x).css('background-color', $(x).attr('color'));
}
$('.hovered').removeClass('hovered');
$('.Hovertips').hide();
myId = _this.attr('id');
rootId = myId;
if (myId.indexOf('_') !== -1) rootId = myId.slice(0, myId.indexOf('_'));
_this.css('background-color', _this.attr('color'));
return $('#' + rootId).showAsSibling();
});
return this;
};
})($);
synthesizeSpeech = root.synthesizeSpeech = function(sentence, lang, isloop) {
var audioTag;
audioTag = $('audio')[0];
if (!audioTag) {
$('body').append($('<audio>').attr('autoplay', true).attr('loop', isloop));
audioTag = $('audio')[0];
}
audioTag.src = 'http://geza.csail.mit.edu:1357/synthesize?sentence=' + sentence + '&lang=' + lang;
if (isloop || !(isloop != null)) {
$('audio').attr('loop', true);
} else {
$('audio').attr('loop', false);
}
return audioTag.play();
};
'ref_hierarchy = [[\'私\', \'の\', \'猫\'], \'が\', [[\'家\', \'で\'], [ [[\'五\', \'匹\'], \'の\', \'鼠\'], \'を\', \'食べた\']]]\ntranslations = {\n\'私の猫が家で五匹の鼠を食べた\': \'my cat ate 5 mice in the house\',\n\'私の猫\': \'my cat\',\n\'私\': \'me\',\n\'の\': \'of\',\n\'猫\': \'cat\',\n\'が\': \'subject marker\',\n\'家で五匹の鼠を食べた\': \'ate 5 mice in the house\',\n\'家で\': \'in the house\',\n\'家\': \'house\',\n\'で\': \'in\',\n\'五匹の鼠を食べた\': \'ate 5 mice\',\n\'五匹の鼠\': \'5 mice\',\n\'五匹\': \'5 small animals\',\n\'五\': \'5\',\n\'匹\': \'counter for small animals\',\n\'鼠\': \'mouse\',\n\'を\': \'object marker\',\n\'食べた\': \'ate\',\n}';
'getMaxDepth = root.getMaxDepth = (id) ->\n maxval = 0\n for child in getChildrenOfId(id)\n maxval = Math.max(maxval, getMaxDepth(child)+1)\n return maxval';
getMaxDepth = root.getMaxDepth = function(subtree) {
var child, maxval, _i, _len;
if (typeof subtree !== typeof []) return 0;
maxval = 0;
for (_i = 0, _len = subtree.length; _i < _len; _i++) {
child = subtree[_i];
maxval = Math.max(maxval, getMaxDepth(child) + 1);
}
return maxval;
};
'getMaxDepth = root.getMaxDepth = (id) ->\n if not id?\n id = \'R\'\n maxval = 0\n for childId in getChildrenOfId(id)\n maxval = Math.max(maxval, getMaxDepth(childId) + 1)\n return maxval';
getChildrenOfId = root.getChildrenOfId = function(id) {
var x;
return (function() {
var _i, _len, _ref, _results;
_ref = $('#' + id).children('.textRegion');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
_results.push($(x).attr('id'));
}
return _results;
})();
};
hierarchyToTerminals = function(hierarchy, lang) {
var children, x;
if (typeof hierarchy === typeof []) {
children = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = hierarchy.length; _i < _len; _i++) {
x = hierarchy[_i];
_results.push(hierarchyToTerminals(x, lang));
}
return _results;
})();
if (!(lang != null) || lang === 'zh' || lang === 'ja') {
return children.join('');
} else {
return children.join(' ');
}
} else {
return hierarchy;
}
};
hierarchyWithIdToTerminals = function(hierarchy, lang) {
var children, contents, id, x;
if (typeof hierarchy === typeof '') return hierarchy;
id = hierarchy.id;
contents = hierarchy.slice(0);
if (contents.length === 1) {
return hierarchyWithIdToTerminals(contents[0]);
} else {
}
if (true) {
children = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = contents.length; _i < _len; _i++) {
x = contents[_i];
_results.push(hierarchyWithIdToTerminals(x, lang));
}
return _results;
})();
if (!(lang != null) || lang === 'zh' || lang === 'ja') {
return children.join('');
} else {
return children.join(' ');
}
}
};
initializeHover = function(basediv) {
var contentHierarchy, depth, maxdepth;
contentHierarchy = deserializeArray(basediv.attr('contentHierarchy'));
depth = basediv.attr('depth');
maxdepth = basediv.attr('maxdepth');
basediv.hoverId();
if (contentHierarchy.length > 1) {
return basediv.borderStuff(depth, maxdepth);
} else if (contentHierarchy.length === 1) {
if (typeof contentHierarchy[0] === typeof '') {
return basediv.borderStuff(depth, maxdepth, 'white').text(contentHierarchy[0]).hoverId();
} else {
return basediv.borderStuff(depth, maxdepth, 'white').text(contentHierarchy[0]).hoverId();
}
}
};
makeDivs = function(subHierarchy, lang, translations, maxdepth, depth) {
var basediv, child, contentHierarchy, foreignText, id, translation, _i, _len;
if (depth == null) depth = 1;
basediv = $('<div>');
id = subHierarchy.id;
contentHierarchy = subHierarchy;
basediv.addClass('hovertext').attr('id', id);
basediv.addClass('hovertext').addClass('textRegion');
foreignText = hierarchyWithIdToTerminals(subHierarchy, lang);
basediv.attr('foreignText', foreignText);
basediv.attr('foreignLang', lang);
translation = translations[foreignText];
basediv.attr('translation', translation);
basediv.attr('depth', depth);
basediv.attr('maxdepth', maxdepth);
basediv.attr('contentHierarchy', serializeArray(contentHierarchy));
basediv.hoverId();
(function(id) {
return basediv.click(function() {
var lForeignText, lTranslation, shortTranslation;
lForeignText = $('#' + id).attr('foreignText');
lTranslation = $('#' + id).attr('translation');
console.log('clicked:');
console.log(lForeignText);
console.log('translation:');
shortTranslation = lTranslation;
if (shortTranslation.indexOf('\n') !== -1) {
shortTranslation = shortTranslation.split('\n')[0];
}
console.log(shortTranslation);
openTranslationPopup(foreignText, shortTranslation, lang, id);
return false;
});
})(id);
initializeHover(basediv);
if (contentHierarchy.length > 1) {
basediv.borderStuff(depth, maxdepth);
for (_i = 0, _len = contentHierarchy.length; _i < _len; _i++) {
child = contentHierarchy[_i];
basediv.append(makeDivs(child, lang, translations, maxdepth, depth + 1));
}
} else if (contentHierarchy.length === 1) {
if (typeof contentHierarchy[0] === typeof '') {
basediv.borderStuff(depth, maxdepth, 'white').text(contentHierarchy[0]).hoverId();
}
}
return basediv;
};
addIdsToHierarchy = function(hierarchy, myId) {
var i, output, x, _i, _len;
if (myId == null) myId = 'R0';
if (typeof hierarchy !== typeof '') {
output = [];
for (i = _i = 0, _len = hierarchy.length; _i < _len; i = ++_i) {
x = hierarchy[i];
console.log(x);
if (typeof x !== typeof '') {
output.push(addIdsToHierarchy(x, myId + '_' + i));
} else {
output.push(x);
}
}
output.id = myId;
if (hierarchy.pos != null) output.pos = hierarchy.pos;
return output;
} else {
return hierarchy;
}
};
addFakePOSTags = function(hierarchy) {
var i, output, x, _i, _len;
output = [];
if (typeof hierarchy === typeof []) {
for (i = _i = 0, _len = hierarchy.length; _i < _len; i = ++_i) {
x = hierarchy[i];
output.push(addFakePOSTags(x));
}
output.pos = 'fakePOS';
return output;
} else {
output = [hierarchy];
output.pos = 'fakePOS';
return output;
}
};
getUrlParameters = root.getUrlParameters = function() {
var map, parts;
map = {};
parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
return map[key] = decodeURI(value);
});
return map;
};
callOnceElementAvailable = function(element, callback) {
if ($(element).length > 0) {
return callback();
} else {
return setTimeout(function() {
return callOnceElementAvailable(element, callback);
}, 10);
}
};
renderSentence = function(sentence, ref_hierarchy, translations, lang, renderTarget) {
var idnum, ref_hierarchy_with_ids, rootBaseDiv;
console.log(ref_hierarchy);
console.log(translations);
idnum = 0;
while ($('#R' + idnum).length > 0) {
idnum += 1;
}
if (lang === 'ja') ref_hierarchy = addFakePOSTags(ref_hierarchy);
ref_hierarchy_with_ids = addIdsToHierarchy(ref_hierarchy, 'R' + idnum);
console.log(ref_hierarchy_with_ids);
rootBaseDiv = makeDivs(ref_hierarchy_with_ids, lang, translations, getMaxDepth(ref_hierarchy_with_ids) - 1);
renderTarget.append(rootBaseDiv).append('<br>');
rootBaseDiv.showAsSibling();
return callOnceElementAvailable('#HR' + idnum, function() {
var currentTopMargin;
currentTopMargin = 0;
return rootBaseDiv.css('margin-top', currentTopMargin + $('#HR' + idnum).height());
}, 10);
};
addSentence = root.addSentence = function(sentence, lang, renderTarget, clearExisting, callback) {
if (clearExisting == null) clearExisting = false;
return addSentences([sentence], lang, renderTarget, clearExisting, callback);
};
if (!(root.serverLocation != null)) root.serverLocation = '';
submitTranslation = root.submitTranslation = function(origPhrase, translation, lang) {
var reqParams;
console.log('translation submitted');
console.log(origPhrase);
console.log(translation);
reqParams = {
'sentence': origPhrase,
'lang': lang,
'targetlang': 'en',
'translation': translation
};
console.log(root.serverLocation + '/submitTranslation?' + $.param(reqParams));
if (!(root.isMTurk != null)) {
return $.get(root.serverLocation + '/submitTranslation?' + $.param(reqParams));
}
};
updateTranslation = function(id, translation) {
var basediv, fullTranslation, parentId, parentdiv;
console.log('updateTranslation for:' + id);
basediv = $('#' + id);
fullTranslation = basediv.attr('translation').split('\n');
fullTranslation[0] = translation;
basediv.attr('translation', fullTranslation.join('\n'));
basediv.hoverId();
$('#H' + id).text(translation);
if (id.indexOf('_') !== -1) {
parentId = id.split('_').slice(0, -1).join('_');
return parentdiv = $('#' + parentId);
} else {
return basediv.css('margin-top', $('#H' + id).height());
}
};
openTranslationPopup = root.openTranslationPopup = function(sentenceToTranslate, translation, lang, id) {
initializePopup();
$('#sentenceToTranslate').text(sentenceToTranslate);
$('#translationInput').val(translation);
$('#popupTranslateDisplay').attr('translationForId', id);
$('#popupTranslateDisplay').attr('translationForLang', lang);
return $('#popupTranslateDisplay').dialog('open');
};
root.popupInitialized = false;
initializePopup = root.initializePopup = function() {
var popupTranslateDisplay;
if (root.popupInitialized) return;
root.popupInitialized = true;
popupTranslateDisplay = $('<div id="popupTranslateDisplay">Translation for <span id="sentenceToTranslate"></span><form action="javascript:void(0)" id="translationForm"><input type="text" id="translationInput" /><input type="hidden" value="submit" /></form></div>');
return popupTranslateDisplay.dialog({
'autoOpen': false,
'modal': false,
'title': '',
'zIndex': 99,
'create': function() {
$(this).css("maxHeight", 500);
return $('#translationInput').keypress(function(e) {
var inputtedText, lang, origPhrase, translation;
if (e.keyCode === 13) {
inputtedText = $('#translationInput').val();
$('#popupTranslateDisplay').dialog('close');
if (inputtedText === '') {
return false;
} else {
origPhrase = $('#sentenceToTranslate').text();
translation = $('#translationInput').val();
lang = $('#popupTranslateDisplay').attr('translationForLang');
submitTranslation(origPhrase, translation, lang);
updateTranslation($('#popupTranslateDisplay').attr('translationForId'), translation);
return false;
}
}
});
}
}).css('max-height', '500px');
};
arrayToObj = function(arr) {
var k, obj, v;
if ((typeof arr !== typeof []) && (typeof arr !== typeof {})) return arr;
obj = {};
for (k in arr) {
v = arr[k];
if ((typeof v === typeof []) || (typeof v === typeof {})) v = arrayToObj(v);
obj[k] = v;
}
return obj;
};
objToArray = function(obj) {
var arr, k, v;
if (typeof obj !== typeof {}) return obj;
arr = [];
for (k in obj) {
v = obj[k];
if (typeof v === typeof {}) v = objToArray(v);
if (!isNaN(k)) k = parseInt(k);
arr[k] = v;
}
return arr;
};
serializeArray = root.serializeArray = function(arr) {
var serializable;
serializable = arrayToObj(arr);
return JSON.stringify(serializable);
};
deserializeArray = root.deserializeArray = function(s) {
var obj;
obj = JSON.parse(s);
return objToArray(obj);
};
addSentences = root.addSentences = function(sentences, lang, renderTarget, clearExisting, doneCallback) {
var parseHierarchyAndTranslationsForLang, _ref;
if (clearExisting == null) clearExisting = false;
if (!(lang != null) && !(renderTarget != null)) {
lang = (_ref = getUrlParameters()['lang']) != null ? _ref : 'en';
renderTarget = $('#sentenceDisplay');
}
if (!(renderTarget != null)) renderTarget = $('#sentenceDisplay');
parseHierarchyAndTranslationsForLang = function(sentence, callback) {
var currentPair, resultData;
if (!(root.isMTurk != null)) {
return $.get(root.serverLocation + '/getParseHierarchyAndTranslations?sentence=' + encodeURI(sentence) + '&lang=' + encodeURI(lang), function(resultData, resultStatus) {
var currentPair;
resultData = deserializeArray(resultData);
currentPair = [resultData.hierarchy, resultData.translations];
return callback(null, currentPair);
});
} else {
resultData = deserializeArray(root.cachedParseHierarchyAndTranslations[lang][sentence]);
currentPair = [resultData.hierarchy, resultData.translations];
return callback(null, currentPair);
}
};
return async.mapSeries(sentences, parseHierarchyAndTranslationsForLang, function(err, results) {
var i, ref_hierarchy, sentence, translations, _i, _ref1, _ref2;
if (clearExisting) renderTarget.html('');
for (i = _i = 0, _ref1 = results.length; 0 <= _ref1 ? _i < _ref1 : _i > _ref1; i = 0 <= _ref1 ? ++_i : --_i) {
sentence = sentences[i];
_ref2 = results[i], ref_hierarchy = _ref2[0], translations = _ref2[1];
renderSentence(sentence, ref_hierarchy, translations, lang, renderTarget);
}
if (doneCallback != null) return doneCallback();
});
};
console.log('visutils loaded');
root.addSentence = addSentence;
}).call(this);