diff options
Diffstat (limited to 'lfpe.js')
-rw-r--r-- | lfpe.js | 325 |
1 files changed, 173 insertions, 152 deletions
@@ -29,70 +29,6 @@ var Timer = { } /* - * init site - * - */ -function init() -{ - document.getElementById("target_textarea").value = ""; - document.getElementById("raw_source_textarea").value = ""; - document.getElementById("source").value = ""; - document.getElementById("current_seg_id").value = ""; - document.getElementById("paused").value = ""; - document.getElementById("oov_correct").value = false; - document.getElementById("displayed_oov_hint").value = false; - document.getElementById("init").value = ""; - document.getElementById("target_textarea").setAttribute("disabled", "disabled"); - document.getElementById("next").removeAttribute("disabled"); - document.getElementById("pause_button").removeAttribute("disabled"); - - return false; -} - -/* - * cross-site request - * - */ -function CreateCORSRequest(method, url) -{ - var xhr = new XMLHttpRequest(); - if ("withCredentials" in xhr) { - xhr.open(method, url, true); - } else { - xhr = null; - } - - return xhr; -} - -/* - * no newline on return in textarea - * - */ -function catch_return(e) -{ - if (e.keyCode == 13) { - e.preventDefault(); - Next(); - } - - return false; -} - -/* - * check oov correction input - * - */ -function check_oov_correction() -{ - var need = trim(document.getElementById("raw_source_textarea").value).split(";").length; - var a = trim(document.getElementById("target_textarea").value).split(";"); - a = a.filter(function(i){ return i!=""; }) - - return need==a.length; -} - -/* * pause/unpause timer * */ @@ -103,6 +39,7 @@ function pause() var next_button = document.getElementById("next"); var target_textarea = document.getElementById("target_textarea") var initialized = document.getElementById("init"); + if (paused.value == 0) { button.innerHTML = "Unpause"; paused.value = 1; @@ -121,15 +58,15 @@ function pause() } /* - * hacky way to remove class from node + * no newline on return in textarea * */ -function removeClass(node, className) +function catch_return(e) { - node.className = - node.className.replace(" "+className,''); - node.className = - node.className.replace(" "+className,''); // ??? + if (e.keyCode == 13) { + e.preventDefault(); + Next(); + } return false; } @@ -138,24 +75,51 @@ function removeClass(node, className) * * */ -function toggleDisplay(node) +function working() { - if (node.style.display=='none') { - node.style.display = 'block'; - } else { - node.style.display = 'none'; - } + // elements + var button = document.getElementById("next"); + var pause_button = document.getElementById("pause_button"); + var target_textarea = document.getElementById("target_textarea") + var raw_source_textarea = document.getElementById("raw_source_textarea"); + var current_seg_id = document.getElementById("current_seg_id"); + var source = document.getElementById("source"); + var status = document.getElementById("status"); + var oov_correct = document.getElementById("oov_correct"); + var last_post_edit = document.getElementById("last_post_edit"); - return false; + // show 'working' message + status.style.display = "block"; + + // disable button and textarea + button.setAttribute("disabled", "disabled"); + pause_button.setAttribute("disabled", "disabled"); + target_textarea.setAttribute("disabled", "disabled"); } /* - * trim string + * * */ -function trim(s) +function not_working() { - return s.replace(/^\s+|\s+$/g, ''); + // elements + var button = document.getElementById("next"); + var pause_button = document.getElementById("pause_button"); + var target_textarea = document.getElementById("target_textarea") + var raw_source_textarea = document.getElementById("raw_source_textarea"); + var current_seg_id = document.getElementById("current_seg_id"); + var source = document.getElementById("source"); + var status = document.getElementById("status"); + var oov_correct = document.getElementById("oov_correct"); + var last_post_edit = document.getElementById("last_post_edit"); + + // hide 'working' message + status.style.display = "none"; + + // enable buttons + document.getElementById("next").removeAttribute("disabled"); + document.getElementById("pause_button").removeAttribute("disabled"); } /* @@ -175,19 +139,28 @@ function Next() var oov_correct = document.getElementById("oov_correct"); var last_post_edit = document.getElementById("last_post_edit"); - // disable button and textarea - button.setAttribute("disabled", "disabled"); - pause_button.setAttribute("disabled", "disabled"); - target_textarea.setAttribute("disabled", "disabled"); + working(); // get metadata stored in DOM + var base_url = "http://coltrane.cl.uni-heidelberg.de"; var port = document.getElementById("port").value; - var base_url = "http://coltrane.cl.uni-heidelberg.de:"+port; var key = document.getElementById("key").value; - next_url = base_url+"/next?key="+key; + // url + next_url = base_url+":"+port+"/next?key="+key; - var post_edit = trim(target_textarea.value); + // post edit + var post_edit = ''; + + // extract data from interfaces + if (ui_type == 'g') { + post_edit = JSON.parse(extract_data())["target"].join(" ") + } else { + post_edit = trim(target_textarea.value); + } + + // send data + // ??? if (oov_correct.value=="false" && post_edit != "") { // compose request next_url += "&example="+encodeURIComponent(source.value)+"%20%7C%7C%7C%20"+encodeURIComponent(post_edit)+"&duration="+Timer.get(); @@ -197,15 +170,28 @@ function Next() } // update document overview document.getElementById("seg_"+(current_seg_id.value)+"_t").innerHTML=post_edit; + // OOV correction mode } else if (oov_correct.value=="true") { - if (!check_oov_correction()) { - alert("Please provide translations for each word in the 'Source' text area, separated by ';'."); - target_textarea.removeAttribute("disabled", "disabled"); - pause_button.removeAttribute("disabled", "disabled"); - button.removeAttribute("disabled", "disabled"); - return; - } - next_url += "&correct="+encodeURIComponent(raw_source_textarea.value)+"%20%7C%7C%7C%20"+encodeURIComponent(post_edit) + var l = document.getElementById("oov_fields").children.length; + var src = []; + var tgt = []; + for (var i=0; i<l/2; i++) { + src.push(trim(document.getElementById("oov_src"+i).value)); + tgt.push(trim(document.getElementById("oov_tgt"+i).value)); + if (tgt[tgt.length-1] == "") { // empty correction + alert("Please provide translations for all OOV words."); + not_working(); + + return; + } + } + var l = document.getElementById("oov_fields").children.length; + for (var i=0; i<l; i++) + { document.getElementById("oov_fields").children[0].remove(); } + $("#oov_form").css("display", "none"); + next_url += "&correct="+encodeURIComponent(src.join("\t")) + +"%20%7C%7C%7C%20"+encodeURIComponent(tgt.join("\t")) + // ??? } else { if (source.value != "") { alert("Please provide a post-edit."); @@ -216,12 +202,9 @@ function Next() } } - // show 'working' message - status.style.display = "block"; - // confirm to server if (document.getElementById("init").value != "") { - var xhr_confirm = CreateCORSRequest('get', base_url+"/confirm"); + var xhr_confirm = CreateCORSRequest('get', base_url+":"+port+"/confirm"); xhr_confirm.send(); // FIXME: handle errors } @@ -236,62 +219,55 @@ function Next() document.getElementById("init").value = 1; // for pause() // translation system is currently handling a request // FIXME: maybe poll server for result? - if (xhr.responseText == "locked") { - alert("Translation system is locked, try again in a moment (reload page and click 'Start/Continue' again)."); - status.style.display = "none"; + if (xhr.responseText == "locked") { + alert("Translation system is locked, try again in a moment (reload page and click 'Start/Continue' again)."); + not_working(); - return; - } - var x = xhr.responseText.split("\t"); - if (x == "fi") { // done -> hide/disable functional elements + return; + } + + data = JSON.parse(xhr.responseText) + document.getElementById("data").value = xhr.responseText; + + // done, disable interface + if (data["fin"]) { raw_source_textarea.setAttribute("disabled", "disabled"); target_textarea.setAttribute("disabled", "disabled"); status.style.display = "none"; button.innerHTML = "Session finished, thank you!"; button.setAttribute("disabled", "disabled"); pause_button.setAttribute("disabled", "disabled"); - removeClass(document.getElementById("seg_"+current_seg_id.value), "bold"); - } else { - // got response: OOV\tseg id\ttoken_1\ttoken_2\t... - // 0 1 2 3 ... - if (x[0] == "OOV") { - var s = ""; - for (var i=2; i < x.length; i++) { - s += x[i].substr(1,x[i].length-2); - if (i+1 < x.length) { - s += "; "; - } - raw_source_textarea.value = s; - } - // update interface - status.style.display = "none"; - button.innerHTML = "Correct"; - button.removeAttribute("disabled"); - target_textarea.removeAttribute("disabled", "disabled"); - pause_button.removeAttribute("disabled", "disabled"); - target_textarea.value = ""; - target_textarea.focus(); - target_textarea.selectionStart = 0; - target_textarea.selectionEnd = 0; - oov_correct.value = true; - var id = x[1]; - document.getElementById("seg_"+id).className += " bold"; - if (id > 0) { - removeClass(document.getElementById("seg_"+(id-1)), "bold"); - } - if (document.getElementById("displayed_oov_hint").value == "false") { - alert("Please translate the following words (separated by semicolons) to enable translation of the next sentence. Source words are always in lower case. Use correct casing for suggested translation."); - document.getElementById("displayed_oov_hint").value = true; - } - - return; + if (current_seg_id.value) + removeClass(document.getElementById("seg_"+current_seg_id.value), "bold"); + + return; + + // enter OOV correct mode + } else if (data["oovs"]) { + var append_to = document.getElementById("oov_fields"); + for (var i=0; i<data["oovs"].length; i++) { + var node_src = document.createElement("input"); + var node_tgt = document.createElement("input"); + node_src.type = "text"; + node_tgt.type = "text"; + node_src.id = "oov_src"+i; + node_tgt.id = "oov_tgt"+i; + node_src.value = data["oovs"][i]; + node_src.setAttribute("disabled", "disabled"); + append_to.appendChild(node_src); + append_to.appendChild(node_tgt); } - // got response: seg id\tsource\ttranslation\traw source - // 0 1 2 3 - var id = x[0]; - var src = x[1]; - var translation = x[2]; - var raw_source = x[3]; + oov_correct.value = true; + + $("#oov_form").css("display", "block"); + not_working(); + + // translation mode + } else { + var id = data["progress"]; + var src = data["source"]; + var translation = data["transl_detok"]; + var raw_source = data["raw_source"]; // update interface oov_correct.value = false; @@ -318,19 +294,64 @@ function Next() last_post_edit.value = translation; // confirm to server - //var xhr_confirm = CreateCORSRequest('get', base_url+"/confirm"); - //xhr_confirm.send(); // FIXME: handle errors + var xhr_confirm = CreateCORSRequest('get', base_url+":"+port+"/confirm"); + xhr_confirm.send(); // FIXME: handle errors + // load data into graphical UI + if (ui_type == "g") { + load_data(); + } + + // start timer Timer.start(); } }; - xhr.onerror = function() { - // FIXME: do something reasonable - }; + xhr.onerror = function() {}; // FIXME: do something reasonable xhr.send(); // send 'next' request return; } +/* + * init text interface + * + */ +function init_text_editor() +{ + document.getElementById("target_textarea").value = ""; + document.getElementById("raw_source_textarea").value = ""; + document.getElementById("target_textarea").setAttribute("disabled", "disabled"); + + return false; +} + +/* + * init site + * + */ +window.onload = function () +{ + // reset vars + document.getElementById("source").value = ""; + document.getElementById("current_seg_id").value = ""; + document.getElementById("paused").value = ""; + document.getElementById("oov_correct").value = false; + document.getElementById("displayed_oov_hint").value = false; + document.getElementById("init").value = ""; + + not_working(); + + ui_type = document.getElementById("ui_type").value; + + // graphical derivation editor + if (ui_type == "g") { + document.getElementById("derivation_editor").style.display = "block"; + // text based editor + } else { + init_text_editor(); + document.getElementById("textboxes").style.display = "block"; + } +}; + |