-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbleu.html
1164 lines (1047 loc) · 46 KB
/
bleu.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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel=stylesheet type="text/css" href="bleu.css">
<title>iBLEU: Interactive BLEU Scoring</title>
<script src="js/bleu.js" type="text/javascript"></script>
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/utils.js" type="text/javascript"></script>
<script src="js/lcs.js" type="text/javascript"></script>
<script src="js/highcharts.js" type="text/javascript"></script>
<script src="js/gray.js" type="text/javascript"></script>
</head>
<body>
<br/>
<div align="center" id="title" style="font-size: 28px;">Interactive Bleu Scorer</div>
<br/>
<div align="center" id="formContainer">
<form id="fileForm" name="fileForm">
<p id="srcFileStuff">
<span class="fileLabel">Step 0: Pick source file (Optional)</span><br/><br/>
<!-- <span style="font-size: 14px; line-height: 160%">[If you want Google Translate integration]</span> -->
<input type="file" class="filepicker" name="srcFile" id="srcFile" size="60">
</p>
<p id="tstFileStuff">
<span class="fileLabel">Step 1: Pick hypothesis file</span><br/><br/>
<input type="file" class="filepicker" name="tstFile" id="tstFile" size="60"><br/>
<a id="secondHypLink" href="#" style="font-size:12px; display:none;">Compare to another system?</a>
<div id="secondHypDiv" name="secondHypDiv" style="display: none;">
<span class="fileLabel">Pick second hypothesis file</span><br/><br/>
<input type="file" class="filepicker" name="tstFile2" id="tstFile2" size="60"><br/>
</div>
</p>
<p id="refFileStuff">
<span class="fileLabel">Step 2: Pick reference file</span><br/><br/>
<input type="file" class="filepicker" name="refFile" id="refFile" size="60">
</p>
<br/>
<table border="0">
<tr>
<td align="center">
<input type="checkbox" value="cased" name="caseCheckBox" id="caseCheckBox"/> Preserve case information
<input type="checkbox" value="tokenized" name="tokenizeCheckBox" id="tokenizeCheckBox"/> Do not tokenize
</td></tr>
<tr><td> </td></tr>
<tr><td>Compare to:
<input type="radio" name="comparisonEngine" value="Google" id="comparisonEngine1" /> Google Translate
<input type="radio" name="comparisonEngine" value="Bing" id="comparisonEngine2"/> Bing Translator
<input type="radio" name="comparisonEngine" value="none" id="comparisonEngine3" /> None</td>
</tr>
</table>
<br/>
<input type="button" value="Score" name="scoreButton" id="scoreButton"/>
</form>
<div align="center" id="scoreSpinnerDiv"></div>
<div class="hider">
<a id="formHider" class="jslink" href="#">Hide Form</a>
</div>
</div><br/>
<div align="center" id="bleuDetailContainer">
<div align="center" id="bleuScoreDiv"> </div><br/>
<div align="center" id="bleuTableDiv"> </div><br/>
</div>
<br/>
<div align="center" id="bleuDocPlotsContainer"></div>
<br/>
<div align="center" id="bleuSegPlotContainer">
<div align="center" id="bleuSegPlot" style="width:700px; height:300px"></div>
</div>
<br/>
<div align="center" id="segDetailContainer"></div>
<br/>
</body>
<script type="text/javascript">
var tstSets = [];
var refSets = [];
var srcSets = [];
var maxN = 4;
var smoothed = true;
var scontainer;
var scontainer2;
var allDocPlots = [];
var segmentScoreTuples = {};
var docidpat = /DocID: (.*) \((.*)\)/;
var docidpat2 = /Segment (.*), Document "(.*)"/;
// Create a hash that will give us the two letter code for any language
var languageNameHash = new Array();
languageNameHash['French'] = 'fr';
languageNameHash['Chinese'] = 'zh';
languageNameHash['German'] = 'de';
languageNameHash['Spanish'] = 'es';
languageNameHash['Arabic'] = 'ar';
languageNameHash['Czech'] = 'cz';
languageNameHash['Dutch'] = 'nl';
languageNameHash['Danish'] = 'da';
languageNameHash['Hungarian'] = 'hu';
languageNameHash['English'] = 'en';
// If you want the tool to remember your Google Translate API key
// across sesions, please save it here
var google_translate_api_key = "";
// If you want the tool to remember your Microsoft Translator API key
// across sessions, please save it here
var bing_translate_api_key = "";
// Show the given bleu score in the appropriate place
function showScore(score, prec, brevity, sysid, srclang, numRefSets) {
var elt = document.getElementById('bleuScoreDiv');
var scorehtml = "<span style=\"font-size: 40px;\">" + (score*100).toFixed(2) + "</span><br/>" +
prec.toFixed(4) + " * " + brevity.toFixed(4) + "<br/>" +
"<span style=\"font-size:12px;\">";
if (srclang != undefined) {
scorehtml += "[Source Language: " + srclang + "]<br/>";
}
scorehtml += "[System: " + sysid + "]<br/>[" + numRefSets + " reference(s)]</span>";
elt.innerHTML = scorehtml;
}
function lcsColorize(sentence, indices_to_color, color) {
var colorhtml = '';
var words = sentence.split(' ');
for (var i=0; i<words.length; i++) {
if (indices_to_color.indexOf(i) != -1) {
colorhtml += words[i] + ' ';
}
else {
colorhtml += '<span style="color: ' + color + '; font-weight: bold;">' + words[i] + '</span> ';
}
}
return colorhtml;
}
// Show two BLEU scores side by side when we are comparing two systems
function showSideBySideScores(scores, precisions, brevities, sysids, srclang, numRefSets) {
var elt = document.getElementById('bleuScoreDiv');
var tr1 = "<tr><td style=\"font-size: 40px; text-align: center;\">" + (scores[0]*100).toFixed(2) + "</td>";
tr1 += "<td> vs </td>";
tr1 += "<td style=\"font-size: 40px; text-align: center;\">" + (scores[1]*100).toFixed(2) + "</td></tr>";
var tr2 = "<tr><td style=\"text-align: center;\">" + precisions[0].toFixed(4) + " * " + brevities[0].toFixed(4) + "</td>";
tr2 += "<td></td>";
tr2 += "<td style=\"text-align: center;\">" + precisions[1].toFixed(4) + " * " + brevities[1].toFixed(4) + "</td></tr>";
var tr3 = "<tr><td style=\"font-size: 12px; text-align: center;\">" + "[System: " + sysids[0] + "]</td>";
tr3 += "<td></td>";
tr3 += "<td style=\"font-size: 12px; text-align: center;\">" + "[System: " + sysids[1] + "]</td></tr>";
trblank = "<tr><td colspan=\"3\"> </td></tr>";
tr4 = "<tr><td colspan=\"3\" style=\"text-align: center;\">";
if (srclang != undefined) {
tr4 += "[Source Language: " + srclang + "]<br/>";
}
tr4 += "[" + numRefSets + " reference(s)]</tr></td>";
elt.innerHTML = "<table>" + tr1 + tr2 + tr3 + trblank + tr4 + "</table>";
}
// Show the details (individual and cumulative) precisions in a table
function showTable(individualNgramScores, cumulativeNgramScores) {
var elt = document.getElementById('bleuTableDiv');
elt.innerHTML = "<span style=\"font-size:12px;\">Breakdown of scores (<a href=\"\" class=\"jslink\" id=\"tableHider\">Show</a>)</span><br/>" +
"<table id=\"bleuTable\" border=1>" +
"<tr align=\"center\">" +
"<th>Type</th>" +
"<th>1-gram</th>" +
"<th>2-gram</th>" +
"<th>3-gram</th>" +
"<th>4-gram</th>" +
"</tr>" +
"<tr>" +
"<td>Individual</td>" +
"<td>" + individualNgramScores[1].toFixed(4) + "</td>" +
"<td>" + individualNgramScores[2].toFixed(4) + "</td>" +
"<td>" + individualNgramScores[3].toFixed(4) + "</td>" +
"<td>" + individualNgramScores[4].toFixed(4) + "</td>" +
"</tr>" +
"<tr>" +
"<td>Cumulative</td>" +
"<td>" + cumulativeNgramScores[1].toFixed(4) + "</td>" +
"<td>" + cumulativeNgramScores[2].toFixed(4) + "</td>" +
"<td>" + cumulativeNgramScores[3].toFixed(4) + "</td>" +
"<td>" + cumulativeNgramScores[4].toFixed(4) + "</td>" +
"</tr>" +
"</table>";
}
function showSegDetails(event) {
var segnum = event.point.x;
var docid = this.name.match(docidpat)[1];
var bleu = event.point.y.toFixed(2);
var lengths = [tstSets[0].documents[docid][segnum].split(" ").length];
// Compute average reference length
var reflens = refSets.map(function(rset) { return rset.documents[docid][segnum].split(" ").length; });
lengths = lengths.concat(reflens);
var avgreflen = reflens.reduce(function(a,b) { return a + b; })/reflens.length;
// Compute the length ratio of the segment length to the average reference length
var lenratio = lengths[0]/avgreflen;
$('#segDetailContainer').show();
var elt = document.getElementById('segDetailContainer');
var reflens = lengths.slice(1);
// Compute the longest common subsequence (LCS) between the appropriate
// items: when there is a single system, then between the hyp and the ref(s)
// and when there are two systems, then between the the hyps
if (tstSets[1] != undefined) {
var lcslist;
var tokhyp1 = $('#tokenizeCheckBox').attr('checked') ? tstSets[0].untokenizedDocuments[docid][segnum] : tstSets[0].documents[docid][segnum];
var tokhyp2 = $('#tokenizeCheckBox').attr('checked') ? tstSets[1].untokenizedDocuments[docid][segnum] : tstSets[1].documents[docid][segnum];
lcslist = lcs(tokhyp1.split(' '), tokhyp2.split(' '));
}
else {
var hypLCSList = [];
var refLCSLists = [];
// var tokhyp = tstSets[0].documents[docid][segnum];
var tokhyp = $('#tokenizeCheckBox').attr('checked') ? tstSets[0].untokenizedDocuments[docid][segnum] : tstSets[0].documents[docid][segnum];
for (var i=0; i<refSets.length; i++) {
var tokref = $('#tokenizeCheckBox').attr('checked') ? refSets[i].untokenizedDocuments[docid][segnum] : refSets[i].documents[docid][segnum];
var lcslist = lcs(tokhyp.split(' '), tokref.split(' '));
hypLCSList.push.apply(hypLCSList, lcslist.xindices);
refLCSLists.push(lcslist.yindices);
}
}
// show the header for the table
if (tstSets[1] != undefined) {
var tablehtml = '<table border="1" style="margin: 0px auto; width: 700px;">' +
'<tr>' +
'<td align="center" id="segDetailHeader" width="20%">ID</td>' +
'<td align="left" id="segDetailID">Segment ' + (segnum+1) + ', Document "' + docid + '"' +
' [Δ<sub>BLEU</sub>=' + bleu + ']</td>' +
'</tr>';
}
else {
var tablehtml = '<table border="1" style="margin: 0px auto; width: 700px;">' +
'<tr>' +
'<td align="center" id="segDetailHeader" width="20%">ID</td>' +
'<td align="left" id="segDetailID">Segment ' + (segnum+1) + ', Document "' + docid + '"' +
' [' + bleu + ' BLEU, '+ lenratio.toFixed(3) + ' Length Ratio]</td>' +
'</tr>';
}
// Add the source sentence, if availble
if (srcSets.length > 0) {
var src = srcSets[0].documents[docid][segnum];
tablehtml += '<tr>' +
'<td align="center" id="segDetailHeader">Source</td>' +
'<td id="segDetailSrc">' + src + '</td>' +
'</tr>';
}
// Add all the references
for(var i=0; i < refSets.length; i++) {
if (tstSets[1] != undefined) {
var ref = $('#tokenizeCheckBox').attr('checked') ? refSets[i].untokenizedDocuments[docid][segnum] : refSets[i].documents[docid][segnum];
ref = $('#caseCheckBox').attr('checked') ? ref : ref.toLowerCase();
tablehtml += '<tr>' +
'<td align="center" id="segDetailHeader">Reference<br/>(' + refSets[i].refid + ')</td>' +
'<td id="segDetailRef' + i + '">' + ref + '</td>' +
'</tr>';
}
else {
var tokref = $('#tokenizeCheckBox').attr('checked') ? refSets[i].untokenizedDocuments[docid][segnum] : refSets[i].documents[docid][segnum];
tokref = $('#caseCheckBox').attr('checked') ? tokref : tokref.toLowerCase();
ref_html = lcsColorize(tokref, refLCSLists[i], '#CC3333');
tablehtml += '<tr>' +
'<td align="center" id="segDetailHeader">Reference<br/>(' + refSets[i].refid + ')</td>' +
'<td id="segDetailRef' + i + '">' + ref_html + '</td>' +
'</tr>';
}
}
// Add the hypothesis or hypotheses as the case may be
if (tstSets[1] != undefined) {
tokhyp1 = $('#caseCheckBox').attr('checked') ? tokhyp1 : tokhyp1.toLowerCase();
hyp1_html = lcsColorize(tokhyp1, lcslist.xindices, 'blue');
tablehtml += '<tr>' +
'<td align="center" id="segDetailHeader">Hypothesis<br/>(' + tstSets[0].sysid + ')</td>' +
'<td id="segDetailHyp">' + hyp1_html + '<span style="font-size: 11px;"> [' + segmentScoreTuples[docid][segnum][0].toFixed(2) + ']</span></td>' +
'</tr>';
// hyp = tstSets[1].untokenizedDocuments[docid][segnum];
tokhyp2 = $('#caseCheckBox').attr('checked') ? tokhyp2 : tokhyp2.toLowerCase();
hyp2_html = lcsColorize(tokhyp2, lcslist.yindices, 'blue');
tablehtml += '<tr>' +
'<td align="center" id="segDetailHeader">Hypothesis<br/>(' + tstSets[1].sysid + ')</td>' +
'<td id="segDetailHyp">' + hyp2_html + '<span style="font-size: 11px;"> [' + segmentScoreTuples[docid][segnum][1].toFixed(2) + ']</span></td>' +
'</tr>';
}
else {
var hyp = $('#tokenizeCheckBox').attr('checked') ? tstSets[0].untokenizedDocuments[docid][segnum] : tstSets[0].documents[docid][segnum];
tokhyp = $('#caseCheckBox').attr('checked') ? tokhyp : tokhyp.toLowerCase();
hyp_html = lcsColorize(tokhyp, hypLCSList, '#CC3333');
tablehtml += '<tr>' +
'<td align="center" id="segDetailHeader">Hypothesis<br/>(' + tstSets[0].sysid + ')</td>' +
'<td id="segDetailHyp">' + hyp_html + '</td>' +
'</tr>';
}
// Show a google/bing translate link if there is a source file
if (srcSets.length > 0) {
var engineName = $('input[name=comparisonEngine]:checked').val();
if (engineName != "none") {
tablehtml += '<tr id="segDetailEngine"><td style="text-align:center; font-size: 14px;" colspan=2><a href="#" class="jslink" id="engineTranslate">What does ' + engineName + ' say?</a></td></tr>';
}
}
tablehtml += '</table>';
elt.innerHTML = tablehtml;
$('html, body').animate({
scrollTop: $("#segDetailContainer").offset().top
}, 500);
}
function googleTranslate(text) {
var newScript = document.createElement('script');
var source = "https://www.googleapis.com/language/translate/v2?key=" + window.google_translate_api_key + "&target=en&callback=showGoogleTranslation&q=" + text;
newScript.src = source;
document.getElementsByTagName('head')[0].appendChild(newScript);
document.getElementsByTagName('head')[0].removeChild(newScript);
newScript = null;
}
function bingTranslate(srctext, srclang) {
// if we don't know the language then try to automatically detect it
if (srclang == "unknown") {
var p = {};
p.appid = window.bing_translate_api_key;
p.text = srctext;
srclang = $.ajax({
url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Detect',
data: p,
type: 'GET',
cache: true,
dataType: 'jsonp',
jsonp: 'oncomplete',
success: function(data, status) {
var newScript = document.createElement('script');
var source = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=showBingTranslation&contentType=text/html&appId=" + window.bing_translate_api_key + "&from=" + data + "&to=en&text=" + p.text;
// alert(source);
newScript.src = source;
document.getElementsByTagName('head')[0].appendChild(newScript);
document.getElementsByTagName('head')[0].removeChild(newScript);
newScript = null;
}
});
}
else {
var newScript = document.createElement('script');
var source = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=showBingTranslation&contentType=text/html&appId=" + window.bing_translate_api_key + "&from=" + languageNameHash[srclang] + "&to=en&text=" + srctext;
newScript.src = source;
document.getElementsByTagName('head')[0].appendChild(newScript);
document.getElementsByTagName('head')[0].removeChild(newScript);
newScript = null;
}
}
function showBingTranslation(response) {
var translation = response;
var finalTranslation = $('#caseCheckBox').attr('checked') ? translation : translation.toLowerCase();
var tdhtml = '<td width="20%" id="segDetailHeader">Bing</td>' +
'<td id="segDetailEngineTrans">' + finalTranslation + '</td>';
$('tr#segDetailEngine').html(tdhtml);
}
function showGoogleTranslation(response) {
var translation = response.data.translations[0].translatedText;
var finalTranslation = $('#caseCheckBox').attr('checked') ? translation : translation.toLowerCase();
var tdhtml = '<td width="20%" id="segDetailHeader">Google</td>' +
'<td id="segDetailEngineTrans">' + finalTranslation + '</td>';
$('tr#segDetailEngine').html(tdhtml);
}
function makeColumnChart(plotdiv, yscores, xticks, label, smallOrBig, ymin, ymax) {
var chart;
var showToolTips;
var pointwidth;
var showLegend;
var showAnimation;
var clickCallBack;
var showXTicks;
var YTickInterval;
var XTickWidth;
var pointClickCallback;
var pointCursor;
if (smallOrBig == "small") {
showToolTips = false;
showLegend = false;
showAnimation = false;
pointwidth = 2;
clickCallback = { click: showLargerPlot };
showXTicks = false;
showXLabels = false;
YTickInterval = 0.5;
XTickWidth = 0;
pointClickCallback = { legendItemClick : function(event) { return false; }, };
pointCursor = 'auto';
}
else {
showToolTips = true;
showLegend = true;
showAnimation = true;
pointwidth = null;
clickCallback = { };
showXTicks = false;
showXLabels = true;
YTickInterval = 0.2;
XTickWidth = 0;
pointClickCallback = { legendItemClick : function(event) { return false; }, click : showSegDetails};
pointCursor = 'pointer';
}
chart = new Highcharts.Chart({
chart: {
renderTo: plotdiv,
defaultSeriesType: 'column',
animation: showAnimation,
borderRadius: 5,
events: clickCallback
},
title: {
text: ''
},
xAxis: {
tickWidth: XTickWidth,
categories: xticks,
labels: {
enabled: showXLabels
}
},
yAxis: {
min: ymin,
max: ymax,
tickInterval: YTickInterval,
title: {
text: ''
}
},
tooltip: {
enabled: showToolTips,
formatter: function() {
return this.y.toFixed(2);
}
},
legend: {
enabled: showLegend
},
plotOptions: {
column: {
cursor : pointCursor,
pointWidth: pointwidth,
borderWidth: 0,
enableMouseTracking: showToolTips,
events: pointClickCallback
}
},
series: [{
animation: showAnimation,
shadow: false,
name: label,
data: yscores,
}],
credits: {
enabled: false
}
});
return chart;
}
// Create the plot data for all the documents
function createDocPlotsData(documentScores, segmentScores) {
var alldScores = [];
var alldTicks = [];
var d = [];
var docScoreLabels = [];
var docIdNumHash = {};
// Create the labels for each plot (the document scores)
var i = 0;
for (docid in documentScores) {
docScoreLabels[i] = [docid, documentScores[docid].toFixed(2)];
docIdNumHash[docid] = i;
alldScores[i] = [];
i += 1;
}
// Create the data for segment scores for each document plot
for (var j=0; j<segmentScores.length; j++) {
var so = segmentScores[j];
alldScores[docIdNumHash[so.docid]].push(so.score);
}
// Now go over each array in alld and add the x axis ticks
for (var k=0; k<alldScores.length; k++) {
alldTicks[k] = [];
for (var l=0; l<alldScores[k].length; l++) {
alldTicks[k].push(l);
}
}
// return everything
return [alldScores, alldTicks, docScoreLabels];
}
// Create the plot data for all the documents
function createDocPlotsDataForComparison(documentScores1, segmentScores1, documentScores2, segmentScores2) {
var documentScoreDiffs = {};
var segmentScoreDiffs = [];
var alldScores = [];
var alldTicks = [];
var docScoreLabels = [];
var docIdNumHash = {};
// Compute the differences in the document scores ...
for (docid in documentScores1) {
documentScoreDiffs[docid] = documentScores1[docid] - documentScores2[docid];
}
// ... and the segment scores
for (var j=0; j<segmentScores1.length; j++) {
var sd = {};
var score1 = segmentScores1[j].score;
var score2 = segmentScores2[j].score;
var diff = score1 - score2;
sd.diff = diff;
sd.docid = segmentScores1[j].docid;
sd.segnum = segmentScores1[j].segnum;
segmentScoreDiffs.push(sd);
if (!segmentScoreTuples[sd.docid]) {
segmentScoreTuples[sd.docid] = []
}
segmentScoreTuples[sd.docid].push([score1, score2]);
}
// Sort the document score diffs by magnitude so that the document with the largest
// BLEU delta (negative or positive) shows up first
var tuples = [];
var sortedDocIDs = [];
for (var docid in documentScoreDiffs) tuples.push([docid, documentScoreDiffs[docid]]);
tuples.sort(function(a, b) {
a = a[1];
b = b[1];
return Math.abs(a) < Math.abs(b) ? 1 : (Math.abs(a) > Math.abs(b) ? -1 : 0);
});
for (var i=0; i<tuples.length; i++) {
sortedDocIDs.push(tuples[i][0]);
}
// Create the labels for each plot (the document score differences)
var i = 0;
var label;
// for (docid in documentScoreDiffs) {
for (var j=0; j<sortedDocIDs.length; j++) {
var docid = sortedDocIDs[j];
var diff = documentScoreDiffs[docid];
label = diff < 0 ? diff.toFixed(2) : '+' + diff.toFixed(2);
docScoreLabels[i] = [docid, label];
docIdNumHash[docid] = i;
alldScores[i] = [];
i += 1;
}
// Create the data for segment scores for each document plot
for (var j=0; j<segmentScoreDiffs.length; j++) {
var sd = segmentScoreDiffs[j];
alldScores[docIdNumHash[sd.docid]].push(sd.diff);
}
// Now go over each array in alld and add the x axis ticks
for (var k=0; k<alldScores.length; k++) {
alldTicks[k] = [];
for (var l=0; l<alldScores[k].length; l++) {
alldTicks[k].push(l);
}
}
// return everything
return [alldScores, alldTicks, docScoreLabels];
}
function showLargerPlot(event) {
var plotNumber = $(this.container).parent().attr('id').slice(-3);
// get the data from the smaller plot
var series = this.series[0];
var data = series.data;
var label = series.name;
var yscores = [];
var xticks = [];
for (var i=0; i<data.length; i++) {
yscores.push(data[i].y);
xticks.push(data[i].x+1);
}
var ymin = series.yAxis.getExtremes().min;
var ymax = series.yAxis.getExtremes().max;
$('#bleuSegPlotContainer').show();
var largerPlot = makeColumnChart('bleuSegPlot', yscores, xticks, label, "big", ymin, ymax);
// clear any segment details
$('#segDetailContainer').html("");
$('html, body').animate({
scrollTop: $("#bleuSegPlotContainer").offset().top
}, 500);
}
$(document).ready(function() {
// Clear the file fields on load
$(':input','#fileForm')
.not(':button')
.not(':radio')
.val('');
// Clear the checkboxes
$('#caseCheckBox').attr('checked', false);
$('#tokenizeCheckBox').attr('checked', false);
// Set the radio buttons to 'none' by default and disable the other two
$('input[name=comparisonEngine]').filter('#comparisonEngine1').attr('disabled', 'disabled')
$('input[name=comparisonEngine]').filter('#comparisonEngine2').attr('disabled', 'disabled')
$('input[name=comparisonEngine]').filter('#comparisonEngine3').attr('checked', 'checked')
// Hide the bleu details
$('#bleuScoreDiv').html("");
$('#bleuTableDiv').html("");
$('bleuDetailContainer').hide();
// Hide the bleu plots
$('#bleuSegPlot').html("");
$('#bleuDocPlotsContainer').hide();
$('#bleuSegPlotContainer').hide();
// Hide any segment details
$('#segDetailContainer').html("");
// Initialize the score containers
scontainer = null;
scontainer2 = null;
// Process the source xml or plaintext file
// We make the decision based on the extension right now which is a bit hacky
$('#srcFile').change(function (e) {
srcSets = [];
var srcfile = e.target.files[0];
var reader = new FileReader();
reader.onload = function() {
var extension = srcfile.name.split('.').pop();
// if the extension is ".xml" then process the xml file
if (extension == 'xml') {
var xml = reader.result;
var sset = $('srcset', xml);
// Get the set and system ids
var srcSet = {};
srcSet.setid = sset.attr("setid");
srcSet.srclang = sset.attr("srclang");
srcSet.documents = {};
var srcDocs = {};
$('doc', xml).each(function (doc) {
var docid = $(this).attr("docid");
var segs = [];
$(this).find("seg").each(function (seg) {
//var segid = $(this).attr("id");
segs.push($(this).text().trim());
// segs.push(tokenize($(this).text(), $('#caseCheckBox').attr('checked')));
});
srcSet.documents[docid] = segs;
});
}
else if (extension == "txt") {
var txt = reader.result;
var srcSet = {};
srcSet.setid = "fakeset";
// Since we don't have the XML to tell us what the source language is, we'll set the
// source language to 'unknown' for now and then automatically detect it later if
// we are asked to compare to Bing or Google and have the API key
srcSet.srclang = "unknown";
srcSet.documents = {};
srcSet.untokenizedDocuments = {};
var docid = "fakedoc";
var segs = [];
var originalSegs = txt.split('\n').slice(0, -1);
for (var i=0; i < originalSegs.length; i++) {
segs.push(originalSegs[i].trim());
}
srcSet.documents[docid] = segs;
}
srcSets.push(srcSet);
// Enable the Bing/Google radio buttons if the source file has been parsed successfully
if (srcSets.length > 0) {
$('input[name=comparisonEngine]').filter('#comparisonEngine1').removeAttr('disabled');
$('input[name=comparisonEngine]').filter('#comparisonEngine2').removeAttr('disabled');
}
}
reader.readAsText(srcfile);
});
// Process the system xml or plaintext file
// We make the decision based on the extension right now which is a bit hacky
$('#tstFile').change(function (e) {
var tstfile = e.target.files[0];
var reader = new FileReader();
reader.onload = function() {
var extension = tstfile.name.split('.').pop();
// if the extension is ".xml" then process the xml file
if (extension == 'xml') {
var xml = reader.result;
var tset = $('tstset', xml);
// Get the set and system ids
var tstSet = {};
tstSet.setid = tset.attr("setid");
tstSet.sysid = tset.attr("sysid");
tstSet.documents = {};
tstSet.untokenizedDocuments = {};
$('doc', xml).each(function (doc) {
var docid = $(this).attr("docid");
var segs = [];
var originalSegs = [];
$(this).find("seg").each(function (seg) {
//var segid = $(this).attr("id");
if ($('#tokenizeCheckBox').attr('checked')) {
segs.push($(this).text().trim());
}
else {
segs.push(tokenize($(this).text().trim(), $('#caseCheckBox').attr('checked')));
}
originalSegs.push($(this).text().trim());
});
tstSet.documents[docid] = segs;
tstSet.untokenizedDocuments[docid] = originalSegs;
});
}
else if (extension == "txt") {
var txt = reader.result;
var tstSet = {};
tstSet.setid = "fakeset";
tstSet.sysid = "fakesys";
tstSet.documents = {};
tstSet.untokenizedDocuments = {};
var docid = "fakedoc";
var segs = [];
var originalSegs = txt.split('\n').slice(0, -1);
for (var i=0; i < originalSegs.length; i++) {
if ($('#tokenizeCheckBox').attr('checked')) {
segs.push(originalSegs[i].trim());
}
else {
segs.push(tokenize(originalSegs[i].trim(), $('#caseCheckBox').attr('checked')));
}
}
tstSet.documents[docid] = segs;
tstSet.untokenizedDocuments[docid] = originalSegs;
}
tstSets[0] = tstSet;
}
reader.readAsText(tstfile);
$('#secondHypLink').css('display', 'inline');
});
$('#tstFile2').change(function (e) {
var tstfile = e.target.files[0];
var reader = new FileReader();
reader.onload = function() {
var extension = tstfile.name.split('.').pop();
if (extension == "xml") {
var xml = reader.result;
var tset = $('tstset', xml);
// Get the set and system ids
var tstSet = {};
tstSet.setid = tset.attr("setid");
tstSet.sysid = tset.attr("sysid");
tstSet.documents = {};
tstSet.untokenizedDocuments = {};
var tstDocs = {};
$('doc', xml).each(function (doc) {
var docid = $(this).attr("docid");
var segs = [];
var originalSegs = [];
$(this).find("seg").each(function (seg) {
//var segid = $(this).attr("id");
if ($('#tokenizeCheckBox').attr('checked')) {
segs.push($(this).text().trim());
}
else {
segs.push(tokenize($(this).text().trim(), $('#caseCheckBox').attr('checked')));
}
originalSegs.push($(this).text().trim());
});
tstSet.documents[docid] = segs;
tstSet.untokenizedDocuments[docid] = originalSegs;
});
}
else if (extension == "txt") {
var txt = reader.result;
var tstSet = {};
tstSet.setid = "fakeset";
tstSet.sysid = "fakesys2";
tstSet.documents = {};
tstSet.untokenizedDocuments = {};
var docid = "fakedoc";
var segs = [];
var originalSegs = txt.split('\n').slice(0, -1);
for (var i=0; i < originalSegs.length; i++) {
if ($('#tokenizeCheckBox').attr('checked')) {
segs.push(originalSegs[i].trim());
}
else {
segs.push(tokenize(originalSegs[i].trim(), $('#caseCheckBox').attr('checked')));
}
}
tstSet.documents[docid] = segs;
tstSet.untokenizedDocuments[docid] = originalSegs;
}
tstSets[1] = tstSet;
}
reader.readAsText(tstfile);
});
// Process the system xml or plaintext file
// We make the decision based on the extension right now which is a bit hacky
$('#refFile').change(function(e) {
refSets = [];
var reffile = e.target.files[0];
var reader = new FileReader();
reader.onload = function() {
var extension = reffile.name.split('.').pop();
// if the extension is ".xml" then process the xml file
if (extension == "xml") {
var xml = reader.result;
// Process each refset
$('refset', xml).each(function(refset) {
var rset = {};
// Get the refid and setid for this refset
rset.setid = $(this).attr("setid");
rset.refid = $(this).attr("refid");
rset.documents = {};
rset.untokenizedDocuments = {};
// Process all docs in each refset
$(this).find("doc").each(function(doc) {
var docid = $(this).attr("docid");
var segs = [];
var originalSegs = [];
$(this).find("seg").each(function(seg) {
//var segid = $(this).attr("id");
if ($('#tokenizeCheckBox').attr('checked')) {
segs.push($(this).text().trim());
}
else {
segs.push(tokenize($(this).text().trim(), $('#caseCheckBox').attr('checked')));
}
originalSegs.push($(this).text().trim());
});
rset.documents[docid] = segs;
rset.untokenizedDocuments[docid] = originalSegs;
});
refSets.push(rset);
});
}
else if (extension == "txt") {
var txt = reader.result;
var rset = {};
rset.setid = "fakeset";
rset.refid = "fakeref";
rset.documents = {};
rset.untokenizedDocuments = {};
var docid = "fakedoc";
var segs = [];
var originalSegs = txt.split('\n').slice(0, -1);
for (var i=0; i < originalSegs.length; i++) {
if ($('#tokenizeCheckBox').attr('checked')) {
segs.push(originalSegs[i].trim());
}
else {
segs.push(tokenize(originalSegs[i].trim(), $('#caseCheckBox').attr('checked')));
}
}
rset.documents[docid] = segs;
rset.untokenizedDocuments[docid] = originalSegs;
refSets.push(rset);
}
}
reader.readAsText(reffile);
});
$('a#secondHypLink').click(function(e) {
e.preventDefault();
$('#secondHypDiv').slideToggle();
$(this).css('display', 'none');
});
$('#scoreButton').click(function(e) {
if (tstSets.length > 0 && refSets.length > 0) {
// validate each test set against the source set, if the latter is present
if (srcSets.length > 0) {
for (var i=0; i<tstSets.length; i++) {
var tset = tstSets[i];
if (tset != undefined) {
if (!validateTstAndSrc(tset, srcSets[0])) {
alert("Error: the hypothesis file (sysid: " + tset.sysid + ") and the source file do not match up");
return false;
}
}
}
}
// validate each tet set against the reference set
for (var i=0; i<tstSets.length; i++) {
var tset = tstSets[i];
if (tset != undefined) {
if (!validateTstAndRefs(tset, refSets)) {
alert("Error: the hypothesis file (sysid: " + tset.sysid + ") and one (or more) of the reference files do not match up");
return false;
}
}
}
// also validate the two test sets against each other
if (tstSets[1] != undefined) {
if (!validateTstAndOtherTst(tstSets[0], tstSets[1])) {
alert("Error: the hypothesis files do not match up (should have same document IDs, same set IDs but different sys IDs.")
return false;
}
}
// clear everything else
$('#bleuSegPlot').html("");
$('#bleuDocPlotsContainer').html("");
$('#bleuDocPlotsContainer').hide();
$('#bleuSegPlotContainer').hide();
$('#segDetailContainer').html("");
allDocPlots = [];
var scores = [];
var brevities = [];
var precisions = [];
var sysids = [];
var srclang = srcSets.length > 0 ? srcSets[0].srclang : undefined;
// Compute the bleu scores for the first system ...
scontainer = scoreSystem(tstSets[0], refSets);
for (sysid in scontainer.bleuObjects) {
var bo = scontainer.bleuObjects[sysid];
scores.push(bo.computeBLEU());
brevities.push(bo.brevity);
precisions.push(bo.precisions[4]);
sysids.push(sysid);
}
// .. and the second system, if available
if (tstSets[1] != undefined) {
scontainer2 = scoreSystem(tstSets[1], refSets);
for (sysid in scontainer2.bleuObjects) {
var bo = scontainer2.bleuObjects[sysid];
scores.push(bo.computeBLEU());
brevities.push(bo.brevity);
precisions.push(bo.precisions[4]);
sysids.push(sysid);
}
}
// Show BLEU details (with details table hidden) if there is
// only one system but if there are two, then show the scores
// side by side and NO table.
if (tstSets[1] != undefined) {
showSideBySideScores(scores, precisions, brevities, sysids, srclang, refSets.length);