1
0
forked from GitHub/gf-core

translator: code cleanup and bug fixes

This commit is contained in:
hallgren
2012-07-27 15:06:02 +00:00
parent 50c1635e34
commit 314662dd09

View File

@@ -119,15 +119,20 @@ Translator.prototype.gf_supported=function(grammar,langcode) {
} }
Translator.prototype.update_translations=function() { Translator.prototype.update_translations=function() {
var t=this
for(var i in t.document.segments) t.update_translation(i)
}
Translator.prototype.update_translation=function(i) {
var t=this var t=this
var doc=t.document var doc=t.document
var o=doc.options var o=doc.options
var ss=doc.segments var ss=doc.segments
var ds=t.drawing.segments var ds=t.drawing.segments
var ts=t.drawing.targets var ts=t.drawing.targets
function replace(i) {
var segment=ss[i] var segment=ss[i]
function draw_update() {
if(ds) { if(ds) {
var sd=t.draw_segment(segment,i) var sd=t.draw_segment(segment,i)
var old=ds[i] var old=ds[i]
@@ -139,23 +144,26 @@ Translator.prototype.update_translations=function() {
ts[i].appendChild(text(segment.target+" ")) ts[i].appendChild(text(segment.target+" "))
} }
} }
function update_segment(m,i,txts) { function update_segment(m,txts) {
var segment=ss[i]
segment.target=txts[0]; segment.target=txts[0];
segment.options={method:m,from:o.from,to:o.to} // no sharing! segment.options={method:m,from:o.from,to:o.to} // no sharing!
if(txts.length>1) segment.choices=txts if(txts.length>1) segment.choices=txts
else delete segment.choices else delete segment.choices
replace(i) draw_update()
} }
function update_apertium_translation(i,afrom,ato) { function update_apertium_translation() {
var segment=ss[i] var afrom=alangcode(o.from), ato=alangcode(o.to)
function upd3(txts) { update_segment("Apertium",i,txts) }
function upd3(txts) { update_segment("Apertium",txts) }
function upd1(res) { function upd1(res) {
//console.log(translate_output) //console.log(translate_output)
if(res.translation) upd3([res.translation]) if(res.translation) upd3([res.translation])
else upd3(["["+res.error.message+"]"]) else upd3(["["+res.error.message+"]"])
} }
function upd0(source) { apertium.translate(source,afrom,ato,upd1) } function upd0(source) { apertium.translate(source,afrom,ato,upd1) }
if(apertium.isTranslatablePair(afrom,ato)) { if(apertium.isTranslatablePair(afrom,ato)) {
if(!eq_options(segment.options,o)) upd0(segment.source) if(!eq_options(segment.options,o)) upd0(segment.source)
} }
@@ -163,10 +171,9 @@ Translator.prototype.update_translations=function() {
upd3(["[Apertium does not support "+show_translation(o)+"]"]) upd3(["[Apertium does not support "+show_translation(o)+"]"])
} }
function update_gf_translation(i,grammar,gfrom,gto) { function update_gf_translation(grammar,gfrom,gto) {
var segment=ss[i]
var server=t.servers[grammar] var server=t.servers[grammar]
function upd3(txts) { update_segment(grammar,i,txts) } function upd3(txts) { update_segment(grammar,txts) }
function upd2(ts) { function upd2(ts) {
function unlex(txt,cont) { gfshell('ps -unlextext "'+txt+'"',cont) } function unlex(txt,cont) { gfshell('ps -unlextext "'+txt+'"',cont) }
@@ -195,41 +202,29 @@ Translator.prototype.update_translations=function() {
else { else {
var fn=concname(o.from) var fn=concname(o.from)
var tn=concname(o.to) var tn=concname(o.to)
var unsup=" is not supported by the grammar" var sup="supported by the grammar"
var sup=" is supported by the grammar" var isnot=" is not "+sup
var msg= fls ? tn+unsup : tls ? fn+unsup : var is=" is "+sup
"Neither "+fn+" nor "+tn+sup var msg= fls ? tn+isnot : tls ? fn+isnot :
"Neither "+fn+" nor "+tn+is
upd3(["["+msg+"]"]) upd3(["["+msg+"]"])
} }
} }
for(var i in ss) {
var m= ss[i].options.method || doc.options.method var m= ss[i].options.method || doc.options.method
var d=ss[i].use_default var d=ss[i].use_default
if(d || d==null) m=doc.options.method if(d || d==null) m=doc.options.method
switch(m) { switch(m) {
case "Manual": case "Manual": /* Nothing to do */ break;
//console.log("Manual "+i) case "Apertium": update_apertium_translation(); break;
break;
case "Apertium":
var afrom=alangcode(o.from)
var ato=alangcode(o.to)
update_apertium_translation(i,afrom,ato)
break;
default: // GF default: // GF
function capture(i,m) { // Capture current values of loop variables
function upd00(grammar_info) { function upd00(grammar_info) {
var gname=grammar_info.name var gname=grammar_info.name
//console.log("Got grammar info "+gname+" for "+i) var gfrom=gname+o.from, gto=gname+o.to
var gfrom=gname+o.from update_gf_translation(m,gfrom,gto)
var gto=gname+o.to
update_gf_translation(i,m,gfrom,gto)
} }
t.switch_grammar(m,upd00) t.switch_grammar(m,upd00)
} }
capture(i,m)
}
}
} }
Translator.prototype.add_apertium=function() { Translator.prototype.add_apertium=function() {
@@ -542,7 +537,7 @@ Translator.prototype.replace_segment=function(i,sd) {
Translator.prototype.pick_translation=function(i,txt) { Translator.prototype.pick_translation=function(i,txt) {
var t=this var t=this
var s=t.document.segments[i] var s=t.document.segments[i]
s.options.method="Manual" set_manual(s)
s.target=txt // side effect, updating the document in-place s.target=txt // side effect, updating the document in-place
t.replace_segment(i,t.draw_segment(s,i)) t.replace_segment(i,t.draw_segment(s,i))
} }
@@ -557,7 +552,7 @@ Translator.prototype.edit_source=function(source,i) {
restore(); restore();
if(s.options.method!="Manual") { if(s.options.method!="Manual") {
s.options.to="" // hack to force an update s.options.to="" // hack to force an update
t.update_translations() t.update_translation(i)
} }
return false; return false;
} }
@@ -576,7 +571,7 @@ Translator.prototype.edit_translation=function(i) {
function restore() { t.replace_segment(i,t.draw_segment(s,i)) } function restore() { t.replace_segment(i,t.draw_segment(s,i)) }
function done() { function done() {
s.options.method="Manual" set_manual(s)
s.options.from=t.document.options.from s.options.from=t.document.options.from
s.options.to=t.document.options.to s.options.to=t.document.options.to
s.target=inp.value // side effect, updating the document in-place s.target=inp.value // side effect, updating the document in-place
@@ -688,7 +683,7 @@ Translator.prototype.draw_segment_given_target=function(s,target,i) {
o.method=m // side effect, updating the document in-place o.method=m // side effect, updating the document in-place
o.to="" // hack to force an update o.to="" // hack to force an update
//console.log("Method changed to "+m) //console.log("Method changed to "+m)
t.update_translations() t.update_translation(i)
} }
} }
var autoB=radiobutton("method","Default","Default",change) var autoB=radiobutton("method","Default","Default",change)
@@ -723,16 +718,27 @@ Translator.prototype.draw_segment_given_target=function(s,target,i) {
return node("tr",{"class":"segment",id:i},[actions,source,options,target]) return node("tr",{"class":"segment",id:i},[actions,source,options,target])
} }
/* --- Document constructor ------------------------------------------------- */
function empty_document() { function empty_document() {
return { name:"Unnamed", return { name:"Unnamed",
options: {from:"Eng", to:"Swe", method:"Manual"}, options: {from:"Eng", to:"Swe", method:"Manual"},
segments:[] } segments:[] }
} }
/* --- Segments ------------------------------------------------------------- */
function new_segment(source) { function new_segment(source) {
return { source:source, target:"", options:{} } // leave options empty return { source:source, target:"", options:{} } // leave options empty
} }
function set_manual(segment) {
segment.options.method="Manual"
segment.use_default=false
}
/* -------------------------------------------------------------------------- */
function draw_translation(o) { return text(show_translation(o)) } function draw_translation(o) { return text(show_translation(o)) }
function show_translation(o) { function show_translation(o) {