forked from GitHub/gf-core
Wide coverage translation demo: show an informative error message for sentences that exceed the length limit
This commit is contained in:
@@ -50,6 +50,11 @@ function length_limit(lang) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function check_limit(lang,encsrc) {
|
||||||
|
var len=encsrc.length, limit=length_limit(lang)
|
||||||
|
return len<=limit ? null : "sentense too long, "+len+">"+limit
|
||||||
|
}
|
||||||
|
|
||||||
// Translate a sentence
|
// Translate a sentence
|
||||||
gftranslate.translate=function(source,from,to,start,limit,cont) {
|
gftranslate.translate=function(source,from,to,start,limit,cont) {
|
||||||
var g=gftranslate.grammar
|
var g=gftranslate.grammar
|
||||||
@@ -60,11 +65,12 @@ gftranslate.translate=function(source,from,to,start,limit,cont) {
|
|||||||
function extract(result) {
|
function extract(result) {
|
||||||
cont(unspace_translations(g,result[0].translations))
|
cont(unspace_translations(g,result[0].translations))
|
||||||
}
|
}
|
||||||
if(encsrc.length<length_limit(from))
|
var too_long=check_limit(from,encsrc)
|
||||||
|
if(too_long) cont([{error:too_long}])
|
||||||
|
else
|
||||||
gftranslate.call("?command=c-translate&jsontree=true&input="+encsrc
|
gftranslate.call("?command=c-translate&jsontree=true&input="+encsrc
|
||||||
+lexer+"&unlexer=text&from="+g+from+"&to="+enc_langs(g,to)
|
+lexer+"&unlexer=text&from="+g+from+"&to="+enc_langs(g,to)
|
||||||
+"&start="+start+"&limit="+limit,extract,errcont)
|
+"&start="+start+"&limit="+limit,extract,errcont)
|
||||||
else cont([{error:"sentence too long"}])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate a sentence word for word (if all else fails...)
|
// Translate a sentence word for word (if all else fails...)
|
||||||
@@ -78,11 +84,12 @@ gftranslate.wordforword=function(source,from,to,cont) {
|
|||||||
cont(unspace_translations(g,result[0].translations))
|
cont(unspace_translations(g,result[0].translations))
|
||||||
}
|
}
|
||||||
var enc_to = enc_langs(g,to)
|
var enc_to = enc_langs(g,to)
|
||||||
if(encsrc.length<length_limit(from))
|
var too_long=check_limit(from,encsrc)
|
||||||
|
if(too_long) cont([{error:too_long}])
|
||||||
|
else
|
||||||
gftranslate.call("?command=c-wordforword&input="+encsrc
|
gftranslate.call("?command=c-wordforword&input="+encsrc
|
||||||
+lexer+"&unlexer=text&from="+g+from+"&to="+enc_to
|
+lexer+"&unlexer=text&from="+g+from+"&to="+enc_to
|
||||||
,extract,errcont)
|
,extract,errcont)
|
||||||
else cont([{error:"sentence too long"}])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get list of supported languages
|
// Get list of supported languages
|
||||||
|
|||||||
@@ -197,6 +197,8 @@ wc.translate=function() {
|
|||||||
var r=tra[0]
|
var r=tra[0]
|
||||||
r.prob = -1
|
r.prob = -1
|
||||||
if(r.linearizations) showit(r,gftranslate.grammar)
|
if(r.linearizations) showit(r,gftranslate.grammar)
|
||||||
|
else if(r.error!=undefined)
|
||||||
|
show_error(r.error)
|
||||||
}
|
}
|
||||||
else if(so.rs.length==0)
|
else if(so.rs.length==0)
|
||||||
show_error("Unable to translate")
|
show_error("Unable to translate")
|
||||||
@@ -210,7 +212,7 @@ wc.translate=function() {
|
|||||||
var r=tra[0]
|
var r=tra[0]
|
||||||
if(r.error!=undefined) {
|
if(r.error!=undefined) {
|
||||||
if(i==0 && so.rs.length==0) {
|
if(i==0 && so.rs.length==0) {
|
||||||
//show_error(tra[0].error)
|
//show_error(r.error)
|
||||||
word_for_word(text)
|
word_for_word(text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user