mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Cloud service & apps: better error handling in wide coverage translation
The wide coverage demo apps now shows a "please wait" message while the grammar is loading, and a red error message if the grammar is not found on the server. The Simple Translation Tool also show red error messages if the grammar is not found.
This commit is contained in:
@@ -6,10 +6,7 @@ var gftranslate = {}
|
||||
gftranslate.jsonurl="/robust/Translate11.pgf"
|
||||
gftranslate.grammar="Translate" // the name of the grammar
|
||||
|
||||
gftranslate.call=function(querystring,cont) {
|
||||
function errcont(text,code) {
|
||||
cont([{translations:[{error:code+" "+text}]}])
|
||||
}
|
||||
gftranslate.call=function(querystring,cont,errcont) {
|
||||
http_get_json(gftranslate.jsonurl+querystring,cont,errcont)
|
||||
}
|
||||
|
||||
@@ -25,11 +22,12 @@ gftranslate.translate=function(source,from,to,start,limit,cont) {
|
||||
var lexer="&lexer=text"
|
||||
if(from=="Chi") lexer="",source=source.split("").join(" ")
|
||||
var encsrc=encodeURIComponent(source)
|
||||
function errcont(text,code) { cont([{error:code+" "+text}]) }
|
||||
function extract(result) { cont(result[0].translations) }
|
||||
if(encsrc.length<500)
|
||||
gftranslate.call("?command=c-translate&input="+encsrc
|
||||
+lexer+"&unlexer=text&from="+g+from+"&to="+enc_langs(g,to)
|
||||
+"&start="+start+"&limit="+limit,extract)
|
||||
+"&start="+start+"&limit="+limit,extract,errcont)
|
||||
else cont([{error:"sentence too long"}])
|
||||
}
|
||||
|
||||
@@ -39,17 +37,19 @@ gftranslate.wordforword=function(source,from,to,cont) {
|
||||
var lexer="&lexer=text"
|
||||
if(from=="Chi") lexer="",source=source.split("").join(" ")
|
||||
var encsrc=encodeURIComponent(source)
|
||||
function errcont(text,code) { cont([{error:code+" "+text}]) }
|
||||
function extract(result) { cont(result[0].translations) }
|
||||
var enc_to = enc_langs(g,to)
|
||||
if(encsrc.length<500)
|
||||
gftranslate.call("?command=c-wordforword&input="+encsrc
|
||||
+lexer+"&unlexer=text&from="+g+from+"&to="+enc_to
|
||||
,extract)
|
||||
,extract,errcont)
|
||||
else cont([{error:"sentence too long"}])
|
||||
}
|
||||
|
||||
// Get list of supported languages
|
||||
gftranslate.get_languages=function(cont) {
|
||||
gftranslate.waiting=[]
|
||||
gftranslate.get_languages=function(cont,errcont) {
|
||||
function init2(grammar_info) {
|
||||
var ls=grammar_info.languages
|
||||
gftranslate.grammar=grammar_info.name
|
||||
@@ -57,21 +57,35 @@ gftranslate.get_languages=function(cont) {
|
||||
for(var i=0;i<ls.length;i++)
|
||||
if(ls[i].name.substr(0,n)==pre) langs.push(ls[i].name.substr(n))
|
||||
gftranslate.targetlist=langs
|
||||
cont(langs)
|
||||
var w=gftranslate.waiting
|
||||
for (var i=0;i<w.length;i++) w[i].cont(langs)
|
||||
gftranslate.waiting=[]
|
||||
}
|
||||
function init2error(text,status,ct) {
|
||||
var w=gftranslate.waiting
|
||||
for (var i=0;i<w.length;i++) {
|
||||
var e=w[i].errcont
|
||||
if(e) e(text,status,ct)
|
||||
}
|
||||
gftranslate.waiting=[]
|
||||
}
|
||||
if(gftranslate.targetlist) cont(gftranslate.targetlist)
|
||||
else gftranslate.call("?command=c-grammar",init2)
|
||||
else {
|
||||
gftranslate.waiting.push({cont:cont,errcont:errcont})
|
||||
if(gftranslate.waiting.length<2)
|
||||
gftranslate.call("?command=c-grammar",init2,init2error)
|
||||
}
|
||||
}
|
||||
|
||||
// Get functions to test which source and target langauges are supported
|
||||
gftranslate.get_support=function(cont) {
|
||||
gftranslate.get_support=function(cont,errcont) {
|
||||
function support(code) { return gftranslate.targets[code] }
|
||||
function init2(langs) {
|
||||
gftranslate.targets=toSet(langs)
|
||||
cont(support,support)
|
||||
}
|
||||
if(gftranslate.targets) cont(support,support)
|
||||
else gftranslate.get_languages(init2)
|
||||
else gftranslate.get_languages(init2,errcont)
|
||||
}
|
||||
|
||||
// trans_text_quality : String -> {quality:String, text:String}
|
||||
|
||||
@@ -3,10 +3,12 @@ wc.cnl="Phrasebook" // try this controlled natural language first
|
||||
wc.f=document.forms[0]
|
||||
wc.o=element("output")
|
||||
wc.e=element("extra")
|
||||
wc.i=element("grammarinfo")
|
||||
wc.p=element("pick")
|
||||
wc.os=[] // output segment list
|
||||
wc.cache={} // output segment cache, indexed by source text
|
||||
wc.local=appLocalStorage("gf.wc.")
|
||||
wc.translating=""
|
||||
|
||||
wc.delayed_translate=function() {
|
||||
function restart(){ if(wc.f.input.value!=wc.translating) wc.translate() }
|
||||
@@ -298,6 +300,7 @@ wc.try_google=function() {
|
||||
// Update language selection menus with the languages supported by the grammar
|
||||
function init_languages() {
|
||||
function init2(langs) {
|
||||
replaceInnerHTML(wc.i,"Enter text to translate above")
|
||||
wc.languages=langs
|
||||
var langset=toSet(langs)
|
||||
function update_menu(m) {
|
||||
@@ -310,7 +313,13 @@ function init_languages() {
|
||||
update_menu(wc.f.from)
|
||||
update_menu(wc.f.to)
|
||||
}
|
||||
gftranslate.get_languages(init2)
|
||||
function initerror(errortext,status,ct) {
|
||||
var msg = status==404 ? "The wide cover translation grammar was not found on the server" : "Server problem "+status
|
||||
replaceChildren(wc.i,text(msg))
|
||||
if(wc.i) wc.i.className="error"
|
||||
}
|
||||
replaceInnerHTML(wc.i,"Loading the wide coverage translation grammar, please wait...")
|
||||
gftranslate.get_languages(init2,initerror)
|
||||
}
|
||||
|
||||
function init_speech() {
|
||||
|
||||
@@ -274,7 +274,10 @@ Translator.prototype.update_translation=function(i) {
|
||||
upd3s("["+msg+"]")
|
||||
}
|
||||
}
|
||||
gftranslate.get_support(check_support)
|
||||
function no_support(text,status,ct) {
|
||||
upd3s("[GF Robust translation service error: "+status+"]")
|
||||
}
|
||||
gftranslate.get_support(check_support,no_support)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ small { color: #666; }
|
||||
|
||||
<div id=output></div>
|
||||
<tr><td colspan=2>
|
||||
<small>Enter text to translate above.</small>
|
||||
<small id=grammarinfo>Enter text to translate above.</small>
|
||||
<td colspan=2>
|
||||
<!--<small id=speak><input name=speak type=checkbox> Enable speech synthesis</small>-->
|
||||
|
||||
@@ -87,10 +87,11 @@ small { color: #666; }
|
||||
<div id=pick></div>
|
||||
<small id=extra></small>
|
||||
</div>
|
||||
<p>
|
||||
<hr>
|
||||
<div class=modtime><small>
|
||||
<!-- hhmts start -->Last modified: Thu May 8 01:37:42 CEST 2014 <!-- hhmts end -->
|
||||
<!-- hhmts start -->Last modified: Fri Jun 20 20:21:52 CEST 2014 <!-- hhmts end -->
|
||||
</small></div>
|
||||
<a href="http://www.grammaticalframework.org/demos/translation.html">About</a>
|
||||
<script src="js/support.js"></script>
|
||||
<script src="js/gftranslate.js"></script>
|
||||
<script src="js/localstorage.js"></script>
|
||||
|
||||
Reference in New Issue
Block a user