From a4522746175985997d47a7c40f303e8e8b47a26f Mon Sep 17 00:00:00 2001 From: hallgren Date: Fri, 21 Oct 2011 15:43:57 +0000 Subject: [PATCH] Work on example-based grammar writing + Preparations to support more target languages. Tested with Swedish. + Don't enable example-based editing if there is less that two concrete languages in the grammar. + Don't show the "By example" button until the required lincat has been defined. + Remember the chosen language for examples. --- src/example-based/ExampleService.hs | 4 +++- src/www/gfse/cloud2.js | 30 ++++++++++++++++++-------- src/www/gfse/example_based.js | 33 ++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/example-based/ExampleService.hs b/src/example-based/ExampleService.hs index ecead5425..0065fd4dd 100644 --- a/src/example-based/ExampleService.hs +++ b/src/example-based/ExampleService.hs @@ -72,7 +72,9 @@ getLimit = maybe err return =<< readInput "limit" where err = throwCGIError 400 "Missing/bad limit" [] -readParsePGF cwd cache = liftIO $ readCache cache (cwd"ParseEngAbs.pgf") +readParsePGF cwd cache = + do parsepgf <- getInp "parser" + liftIO $ readCache cache (cwdparsepgf) parseEnviron s = do state <- liftIO $ readIO s return $ environ state diff --git a/src/www/gfse/cloud2.js b/src/www/gfse/cloud2.js index e32749dc1..71de22387 100644 --- a/src/www/gfse/cloud2.js +++ b/src/www/gfse/cloud2.js @@ -42,16 +42,28 @@ function upload(g) { // Upload the grammar to store it in the cloud function upload_json(cont) { - function upload3(resptext,status) { - local.put("json_uploaded",Date.now()); - //debug("Upload complete") - if(cont) cont(); - else { - var sharing=element("sharing"); - if(sharing) sharing.innerHTML=resptext; - } - } function upload2(dir) { + function upload3(resptext,status) { + local.put("json_uploaded",Date.now()); + //debug("Upload complete") + if(cont) cont(); + else { + var sharing=element("sharing"); + if(sharing) { + if(status==204) { + var a=empty("a"); + a.href="share.html#"+dir.substr(5) // skip "/tmp/" + a.innerHTML=a.href; + sharing.innerHTML=""; + sharing.appendChild(text("Use the following link for shared access to your grammars from multiple devices: ")) + sharing.appendChild(a) + } + else + sharing.innerHTML=resptext; + } + } + } + var prefix=dir.substr(10)+"-" // skip "/tmp/gfse." //debug("New form data"); //var form=new FormData(); // !!! Doesn't work on Android 2.2! diff --git a/src/www/gfse/example_based.js b/src/www/gfse/example_based.js index 5922b60ea..adb9fb7e6 100644 --- a/src/www/gfse/example_based.js +++ b/src/www/gfse/example_based.js @@ -48,13 +48,17 @@ function ask_possibilities(g,ci) { for(var i in poss[1]) testable[poss[1][i]]=true; example_based[ci]={exready:exready,testable:testable} conc.example_based=true; - conc.example_lang=g.concretes[0].langcode; + if(!conc.example_lang) conc.example_lang=g.concretes[0].langcode; reload_grammar(g); } exb_call(g,ci,"possibilities",{},show_poss) } +var parser = { Eng: "ParseEngAbs.pgf", + Swe: "AllSweAbs.pgf" + } + function exb_extra(g,ci) { var conc=g.concretes[ci]; function stop_exb() { @@ -66,14 +70,15 @@ function exb_extra(g,ci) { function opt(conc) { return option(conc.langcode,conc.langcode); } // skip target language var m =node("select",{},map(opt,g.concretes)); - m.onchange=function() { conc.example_lang=m.value } + if(conc.example_lang) m.value=conc.example_lang; + m.onchange=function() { conc.example_lang=m.value; save_grammar(g); } return m } function ask_poss() { ask_possibilities(g,ci) } - if(navigator.onLine && conc.example_based && !example_based[ci]) ask_poss(); - return conc.langcode=="Eng" + if(navigator.onLine && g.concretes.length>1 && conc.example_based && !example_based[ci]) ask_poss(); + return parser[conc.langcode] && g.concretes.length>1 ? indent([text("Example based editing: "), conc.example_based ? node("span",{},[button("Stop",stop_exb), @@ -84,6 +89,12 @@ function exb_extra(g,ci) { : text("") } +function fun_lincat(g,conc,fun) { + var t=function_type(g,fun); + var abscat=t[t.length-1] + return cat_lincat(conc,abscat) +} + function exb_linbuttons(g,ci,f) { var conc=g.concretes[ci]; var fun=f.fun; @@ -107,15 +118,14 @@ function exb_linbuttons(g,ci,f) { exb_output.innerHTML=""; var s=prompt(example[1]); if(s) { - var t=function_type(g,fun); - var abscat=t[t.length-1] - var cat=cat_lincat(conc,abscat) + var cat=fun_lincat(g,conc,fun) exb_output.innerHTML="..."; //server.parse({from:"ParseEng",cat:cat,input:s},fill_example) exb_call(g,ci,"abstract_example", {cat:cat,input:s, + parser:parser[conc.langcode], params:"["+f.args.join(",")+"]", - abstract:example[0]}, + "abstract":example[0]}, fill_example) } } @@ -126,6 +136,7 @@ function exb_linbuttons(g,ci,f) { exb_output.innerHTML="..."; exb_call(g,ci,"provide_example", {lang:g.basename+conc.example_lang, + parser:parser[conc.langcode], fun:fun, grammar:dir+"/"+g.basename+".pgf"}, show_example) @@ -140,12 +151,14 @@ function exb_linbuttons(g,ci,f) { function test_it(b) { if(exb_output) { exb_output.innerHTML="..."; - exb_call(g,ci,"test_function",{fun:fun},show_test) + exb_call(g,ci,"test_function", + {fun:fun,parser:parser[conc.langcode]}, + show_test) } } var buttons=[]; if(conc.example_based && eb) { - if(eb.exready[fun]) + if(eb.exready[fun] && fun_lincat(g,conc,fun)) buttons.push(button("By example",by_example)) if(eb.testable[fun] && f.eb_lin) { var b=button("Test it",test_it);