Work on web api & apps based on the C run-time system

+ PGFService.hs: add command c-grammar, include probability in parse results
+ js/gftranslation.js: add start position and limit parameters, return more
  info to applications
+ Simple Translator: show two wide coverage translations
+ Wide coverage demo: show parse tree and probability (intended as grammar
  debugging aids)
This commit is contained in:
hallgren
2014-02-11 16:43:29 +00:00
parent d99f3e19d7
commit 15bf881e45
4 changed files with 59 additions and 29 deletions

View File

@@ -89,21 +89,30 @@ cpgfMain command (pgf,pc) =
"c-linearize" -> out =<< lin # tree % to
"c-translate" -> out =<< join (trans # input % from % to % start % limit % trie)
"c-flush" -> out =<< flush
"c-grammar" -> out grammar
_ -> badRequest "Unknown command" command
where
flush = liftIO $ do modifyMVar_ pc $ const $ return Map.empty
performGC
return $ showJSON ()
grammar = showJSON $ makeObj
["name".=C.abstractName pgf,
"startcat".=C.startCat pgf,
"languages".=languages]
where
languages = [makeObj ["name".= l] | (l,_)<-Map.toList (C.languages pgf)]
parse input (from,concr) start mlimit trie =
do trees <- parse' input (from,concr) start mlimit
return $ showJSON [makeObj ("from".=from:"trees".=trees :[])]
return $ showJSON [makeObj ("from".=from:"trees".=map tp trees :[])]
-- :addTrie trie trees
where
tp (tree,prob) = makeObj ["tree".=tree,"prob".=prob]
parse' input (from,concr) start mlimit =
liftIO $ do t <- getCurrentTime
(map fst . maybe id take mlimit . drop start)
(maybe id take mlimit . drop start)
# modifyMVar pc (parse'' t)
where
key = (from,input)
@@ -120,13 +129,14 @@ cpgfMain command (pgf,pc) =
lin' tree tos = [makeObj ["to".=to,"text".=C.linearize c tree]|(to,c)<-tos]
trans input (from,concr) tos start mlimit trie =
do trees <- parse' input (from,concr) start mlimit
do parses <- parse' input (from,concr) start mlimit
return $
showJSON [ makeObj ["from".=from,
"translations".=
[makeObj ["tree".=tree,
"prob".=prob,
"linearizations".=lin' tree tos]
| tree <- trees]]]
| (tree,prob) <- parses]]]
from = maybe (missing "from") return =<< getLang "from"

View File

@@ -5,30 +5,36 @@ var gftranslate = {}
gftranslate.jsonurl="/robust/Translate8.pgf"
gftranslate.grammar="Translate" // the name of the grammar
gftranslate.languages="Bul Chi Eng Fin Fre Ger Hin Swe".split(" ")
// hardwired for now
gftranslate.call=function(querystring,cont) {
http_get_json(gftranslate.jsonurl+querystring,cont)
}
// Translate a sentence to the given target language
gftranslate.translate=function(source,from,to,cont) {
// Translate a sentence
gftranslate.translate=function(source,from,to,start,limit,cont) {
var encsrc=encodeURIComponent(source)
var g=gftranslate.grammar
function extract(result) {
cont(result[0].translations[0].linearizations[0].text)
}
function extract(result) { cont(result[0].translations) }
if(encsrc.length<200) // match limit in runtime/c/utils/pgf-server.c
gftranslate.call("?command=c-translate&input="+encsrc
+"&from="+g+from+"&to="+g+to+"&limit=1",extract)
else cont("[GF robust parser: sentence too long]")
+"&from="+g+from+"&to="+g+to
+"&start="+start+"&limit="+limit,extract)
else cont([{error:"sentence too long"}])
}
// Get functions to test which source and target langauges are supported
gftranslate.get_support=function(cont) {
if(!gftranslate.targets) gftranslate.targets=toSet(gftranslate.languages)
function ssupport(code) { return gftranslate.targets[code] }
function tsupport(code) { return gftranslate.targets[code] }
cont(ssupport,tsupport)
function support(code) { return gftranslate.targets[code] }
function init2(grammar_info) {
var ls=grammar_info.languages
gftranslate.grammar=grammar_info.name
var langs=[], pre=gftranslate.grammar, n=pre.length
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
gftranslate.targets=toSet(langs)
cont(support,support)
}
if(gftranslate.targets) cont(support,support)
else gftranslate.call("?command=c-grammar",init2)
}

View File

@@ -203,13 +203,19 @@ Translator.prototype.update_translation=function(i) {
upd3(["[Apertium does not support "+show_translation(o)+"]"])
}
function update_gfrobust_translation() {
function upd3s(txt) { update_segment("GFRobust",[txt]) }
function upd2(ts,punct) {
switch(ts.length) {
case 0: upd3s("[no translation]");break;
default:
if(punct) ts=ts+" "+punct
unlextext(ts,upd3s);
function upd3(txts) { update_segment("GFRobust",txts) }
function upd3s(txt) { upd3([txt]) }
function upd2(trans,punct) {
if(trans.length==0) upd3s("[no translation]")
else if(trans[0].error)
upd3s("[GF robust translation: "+trans[0].error+"]")
else {
var ts=[]
for(var i=0;i<trans.length;i++) {
ts[i]=trans[i].linearizations[0].text
if(punct) ts[i]=ts[i]+" "+punct
}
mapc(unlextext,ts,upd3)
}
}
function upd0(source,punct) {
@@ -217,7 +223,7 @@ Translator.prototype.update_translation=function(i) {
//console.log(translate_output)
upd2(translate_output,punct)
}
gftranslate.translate(source,o.from,o.to,upd1)
gftranslate.translate(source,o.from,o.to,0,2,upd1)
}
if(!window.gftranslate)
upd3s("[GF robust parser is not available]")

View File

@@ -10,6 +10,7 @@ div.center { text-align: center; }
table.center { margin-left: auto; margin-right: auto; }
textarea { margin: 0; }
body:target h1, body:target div.modtime { display: none; }
#extra { color: #666; }
</style>
</head>
@@ -49,12 +50,14 @@ body:target h1, body:target div.modtime { display: none; }
  <button onclick="wcswap()">↑↓</button>
<br>
<textarea name=output rows=3 style="width: 100%" readonly></textarea>
<br>
</form>
</table>
<div class=center><small id=extra></small></div>
<p>
<div class=modtime><small>
<!-- hhmts start -->Last modified: Tue Jan 28 17:38:32 CET 2014 <!-- hhmts end -->
<!-- hhmts start -->Last modified: Tue Feb 11 17:37:40 CET 2014 <!-- hhmts end -->
</small></div>
<script src="js/support.js"></script>
<script src="js/gftranslate.js"></script>
@@ -64,16 +67,21 @@ body:target h1, body:target div.modtime { display: none; }
<script>
function wctranslate() {
var f=document.forms[0]
var e=element("extra")
f.translate.disabled=true
f.output.value=""
clear(e)
function showit(result) {
f.output.value=result
f.translate.disabled=false
}
function step3(text) { unlextext(text,showit) }
function step3(trans) {
if(e) e.innerHTML=trans[0].prob+"<br>"+trans[0].tree
unlextext(trans[0].linearizations[0].text,showit)
}
function step2(text) {
gftranslate.translate(text,f.from.value,f.to.value,step3)
gftranslate.translate(text,f.from.value,f.to.value,0,1,step3)
}
lextext(f.input.value,step2)
return false;