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-linearize" -> out =<< lin # tree % to
"c-translate" -> out =<< join (trans # input % from % to % start % limit % trie) "c-translate" -> out =<< join (trans # input % from % to % start % limit % trie)
"c-flush" -> out =<< flush "c-flush" -> out =<< flush
"c-grammar" -> out grammar
_ -> badRequest "Unknown command" command _ -> badRequest "Unknown command" command
where where
flush = liftIO $ do modifyMVar_ pc $ const $ return Map.empty flush = liftIO $ do modifyMVar_ pc $ const $ return Map.empty
performGC performGC
return $ showJSON () 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 = parse input (from,concr) start mlimit trie =
do trees <- parse' input (from,concr) start mlimit 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 -- :addTrie trie trees
where
tp (tree,prob) = makeObj ["tree".=tree,"prob".=prob]
parse' input (from,concr) start mlimit = parse' input (from,concr) start mlimit =
liftIO $ do t <- getCurrentTime liftIO $ do t <- getCurrentTime
(map fst . maybe id take mlimit . drop start) (maybe id take mlimit . drop start)
# modifyMVar pc (parse'' t) # modifyMVar pc (parse'' t)
where where
key = (from,input) 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] lin' tree tos = [makeObj ["to".=to,"text".=C.linearize c tree]|(to,c)<-tos]
trans input (from,concr) tos start mlimit trie = 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 $ return $
showJSON [ makeObj ["from".=from, showJSON [ makeObj ["from".=from,
"translations".= "translations".=
[makeObj ["tree".=tree, [makeObj ["tree".=tree,
"prob".=prob,
"linearizations".=lin' tree tos] "linearizations".=lin' tree tos]
| tree <- trees]]] | (tree,prob) <- parses]]]
from = maybe (missing "from") return =<< getLang "from" from = maybe (missing "from") return =<< getLang "from"

View File

@@ -5,30 +5,36 @@ var gftranslate = {}
gftranslate.jsonurl="/robust/Translate8.pgf" gftranslate.jsonurl="/robust/Translate8.pgf"
gftranslate.grammar="Translate" // the name of the grammar 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) { gftranslate.call=function(querystring,cont) {
http_get_json(gftranslate.jsonurl+querystring,cont) http_get_json(gftranslate.jsonurl+querystring,cont)
} }
// Translate a sentence to the given target language // Translate a sentence
gftranslate.translate=function(source,from,to,cont) { gftranslate.translate=function(source,from,to,start,limit,cont) {
var encsrc=encodeURIComponent(source) var encsrc=encodeURIComponent(source)
var g=gftranslate.grammar var g=gftranslate.grammar
function extract(result) { function extract(result) { cont(result[0].translations) }
cont(result[0].translations[0].linearizations[0].text)
}
if(encsrc.length<200) // match limit in runtime/c/utils/pgf-server.c if(encsrc.length<200) // match limit in runtime/c/utils/pgf-server.c
gftranslate.call("?command=c-translate&input="+encsrc gftranslate.call("?command=c-translate&input="+encsrc
+"&from="+g+from+"&to="+g+to+"&limit=1",extract) +"&from="+g+from+"&to="+g+to
else cont("[GF robust parser: sentence too long]") +"&start="+start+"&limit="+limit,extract)
else cont([{error:"sentence too long"}])
} }
// Get functions to test which source and target langauges are supported // Get functions to test which source and target langauges are supported
gftranslate.get_support=function(cont) { gftranslate.get_support=function(cont) {
if(!gftranslate.targets) gftranslate.targets=toSet(gftranslate.languages) function support(code) { return gftranslate.targets[code] }
function ssupport(code) { return gftranslate.targets[code] } function init2(grammar_info) {
function tsupport(code) { return gftranslate.targets[code] } var ls=grammar_info.languages
cont(ssupport,tsupport) 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)+"]"]) upd3(["[Apertium does not support "+show_translation(o)+"]"])
} }
function update_gfrobust_translation() { function update_gfrobust_translation() {
function upd3s(txt) { update_segment("GFRobust",[txt]) } function upd3(txts) { update_segment("GFRobust",txts) }
function upd2(ts,punct) { function upd3s(txt) { upd3([txt]) }
switch(ts.length) { function upd2(trans,punct) {
case 0: upd3s("[no translation]");break; if(trans.length==0) upd3s("[no translation]")
default: else if(trans[0].error)
if(punct) ts=ts+" "+punct upd3s("[GF robust translation: "+trans[0].error+"]")
unlextext(ts,upd3s); 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) { function upd0(source,punct) {
@@ -217,7 +223,7 @@ Translator.prototype.update_translation=function(i) {
//console.log(translate_output) //console.log(translate_output)
upd2(translate_output,punct) 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) if(!window.gftranslate)
upd3s("[GF robust parser is not available]") 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; } table.center { margin-left: auto; margin-right: auto; }
textarea { margin: 0; } textarea { margin: 0; }
body:target h1, body:target div.modtime { display: none; } body:target h1, body:target div.modtime { display: none; }
#extra { color: #666; }
</style> </style>
</head> </head>
@@ -49,12 +50,14 @@ body:target h1, body:target div.modtime { display: none; }
  <button onclick="wcswap()">↑↓</button>   <button onclick="wcswap()">↑↓</button>
<br> <br>
<textarea name=output rows=3 style="width: 100%" readonly></textarea> <textarea name=output rows=3 style="width: 100%" readonly></textarea>
<br>
</form> </form>
</table> </table>
<div class=center><small id=extra></small></div>
<p> <p>
<div class=modtime><small> <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> </small></div>
<script src="js/support.js"></script> <script src="js/support.js"></script>
<script src="js/gftranslate.js"></script> <script src="js/gftranslate.js"></script>
@@ -64,16 +67,21 @@ body:target h1, body:target div.modtime { display: none; }
<script> <script>
function wctranslate() { function wctranslate() {
var f=document.forms[0] var f=document.forms[0]
var e=element("extra")
f.translate.disabled=true f.translate.disabled=true
f.output.value="" f.output.value=""
clear(e)
function showit(result) { function showit(result) {
f.output.value=result f.output.value=result
f.translate.disabled=false 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) { 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) lextext(f.input.value,step2)
return false; return false;