minibar: improve pgf_online.js

pgf_online.js has been simplified and generalized to support the full
GF Web Service API. The changes are backwards incompatible, unfortunately.
The documentation and minibar.js have been updated accordingly.
This commit is contained in:
hallgren
2011-08-03 15:21:38 +00:00
parent a0ec59ccda
commit ab8ec58e08
4 changed files with 69 additions and 58 deletions

View File

@@ -1,21 +1,25 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <!DOCTYPE html>
<html> <head> <html> <head>
<title>PGF online server example</title> <title>PGF online server example</title>
<style type="text/css"> <style type="text/css">
body { background: #ddd; } body { background: #ddd; }
h1, h2, h3, small, th { font-family: sans-serif; }
div.modtime { float: right; }
.modtime { color: #666; white-space: nowrap; }
</style> </style>
<script type="text/JavaScript" src="http://www.grammaticalframework.org/src/runtime/javascript/minibar/support.js"></script> <script type="text/JavaScript" src="support.js"></script>
<script type="text/JavaScript" src="http://www.grammaticalframework.org/src/runtime/javascript/minibar/pgf_online.js"></script> <script type="text/JavaScript" src="pgf_online.js"></script>
<script type="text/JavaScript"> <script type="text/JavaScript">
var server_options={ var server_options={
grammars_url: "http://www.grammaticalframework.org/grammars/", grammars_url: "http://www.grammaticalframework.org/grammars/",
grammar_list: ["Syllogism.pgf"] grammar_list: ["Foods.pgf"]
} }
var pgf_server = pgf_online(server_options); var pgf_server = pgf_online(server_options);
function call_server() { function call_server() {
pgf_server.parse("SyllogismEng",document.forms[0].input.value,show_output) pgf_server.parse({from:"FoodsEng",input:document.forms[0].input.value},
show_output)
} }
function show_output(parsed) { function show_output(parsed) {
@@ -31,7 +35,7 @@ function show_output(parsed) {
<form onsubmit="call_server(); return false"> <form onsubmit="call_server(); return false">
Input: Input:
<input name=input size=50 value="some humans are not human"> <input name=input size=50 value="this cheese is expensive">
<input type=submit value=Parse> <input type=submit value=Parse>
<p> <p>
@@ -46,6 +50,8 @@ Output:
</ul> </ul>
<hr> <hr>
<address></address> <div class=modtime><small>
<!-- hhmts start --> Last modified: Mon Apr 11 14:53:21 CEST 2011 <!-- hhmts end --> <!-- hhmts start --> Last modified: Wed Aug 3 16:52:51 CEST 2011 <!-- hhmts end -->
</small></div>
<address><a href="http://www.cse.chalmers.se/~hallgren/">TH</a></address>
</body> </html> </body> </html>

View File

@@ -8,9 +8,10 @@
body { background: #ddd; } body { background: #ddd; }
h1, h2, h3, small, th { font-family: sans-serif; } h1, h2, h3, small, th { font-family: sans-serif; }
dt { background: #ffc; } dt { background: #cef; }
dt.js { background: #cef; margin-bottom: 1ex; } dt.js { background: white; margin-bottom: 1ex; }
dd { background: white; margin-top: 1ex; margin-bottom: 1ex; } dt.js em { color: #36f; }
dd { background: #ffc; margin-top: 1ex; margin-bottom: 1ex; }
dl.apiexamples>dt, dl.apiexamples>dd { font-family: monospace; } dl.apiexamples>dt, dl.apiexamples>dd { font-family: monospace; }
dl.apiexamples>dd { white-space: pre; } dl.apiexamples>dd { white-space: pre; }
@@ -32,16 +33,17 @@ The
Web Service API page</a> describes the calls supported by the GF web service Web Service API page</a> describes the calls supported by the GF web service
API. Below, we illustrate these calls by examples, and also show API. Below, we illustrate these calls by examples, and also show
how to make these calls from JavaScript using the API defined in how to make these calls from JavaScript using the API defined in
<a href="http://www.grammaticalframework.org/src/runtime/javascript/minibar/pgf_online.js">pgf_online.js</a>. <a href="pgf_online.js"><code>pgf_online.js</code></a>.
<p> <p>
<em>Note</em> that pgf_online.js was developed with <strong>Note</strong> that <code>pgf_online.js</code> was initially developed
one particular web application in mind, so the server API provided is with one particular web application in mind (the minibar), so the server API was
incomplete and might be made more complete in a future version... incomplete. It was simplified and generalized in August 2011 to support the
full API.
<dl> <dl>
<dt class=js>These boxes show what the calls look like in the JavaScript <dt class=js>These boxes show what the calls look like in the JavaScript
API defined in pgf_online.js. API defined in <code>pgf_online.js</code>.
<dt>These boxes show the corresponding URLs sent to the PGF server. <dt>These boxes show the corresponding URLs sent to the PGF server.
<dd>These boxes show the JSON (JavaScript data structures) returned by the PGF <dd>These boxes show the JSON (JavaScript data structures) returned by the PGF
server. This will be passed to the callback function supplied in the server. This will be passed to the callback function supplied in the
@@ -51,10 +53,10 @@ incomplete and might be made more complete in a future version...
<h2>Initialization</h2> <h2>Initialization</h2>
<dl class=apiexamples> <dl class=apiexamples>
<dt class=js> <dt class=js>
// Select which server and grammars to use: <em>// Select which server and grammars to use:</em>
<br>var server_options = { <br>var server_options = {
<br>&nbsp;&nbsp;grammars_url: "http://www.grammaticalframework.org/grammars/", <br>&nbsp;&nbsp;grammars_url: "http://www.grammaticalframework.org/grammars/",
<br>&nbsp;&nbsp;grammar_list: ["Foods.pgf"] // It's ok to skip this <br>&nbsp;&nbsp;grammar_list: ["Foods.pgf"] <em>// It's ok to skip this</em>
<br>} <br>}
<br>var server = pgf_online(server_options); <br>var server = pgf_online(server_options);
</dl> </dl>
@@ -62,15 +64,14 @@ incomplete and might be made more complete in a future version...
<h2>Examples</h2> <h2>Examples</h2>
<dl class=apiexamples> <dl class=apiexamples>
<dt class=js> // Get the list of available grammars <dt class=js> <em>// Get the list of available grammars</em>
<br>server.get_grammarlist(callback) <br>server.get_grammarlist(callback)
<dt>http://localhost:41296/grammars/grammars.cgi <dt>http://localhost:41296/grammars/grammars.cgi
<dd>["Foods.pgf","Phrasebook.pgf"] <dd>["Foods.pgf","Phrasebook.pgf"]
<dt class=js> // Select which grammar to use <dt class=js> <em>// Select which grammar to use</em>
<br>server.switch_grammar("Foods.pgf") <br>server.switch_grammar("Foods.pgf")
<dt class=js>// Get concrete language list and other grammar info <dt class=js><em>// Get list of concrete languages and other grammar info</em>
(this method could be renamed) <br>server.grammar_info(callback)
<br>server.get_languages(callback)
<dt>http://localhost:41296/grammars/Foods.pgf <dt>http://localhost:41296/grammars/Foods.pgf
<dd>{"name":"Foods", <dd>{"name":"Foods",
"userLanguage":"FoodsEng", "userLanguage":"FoodsEng",
@@ -84,14 +85,15 @@ incomplete and might be made more complete in a future version...
{"name":"FoodsSwe","languageCode":"sv-SE"}, {"name":"FoodsSwe","languageCode":"sv-SE"},
...] ...]
} }
<dt class=js>// Get a random syntax tree <dt class=js><em>// Get a random syntax tree</em>
<br>server.get_random(callback) <br>server.get_random({},callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=random <dt>http://localhost:41296/grammars/Foods.pgf?command=random
<dd>[{"tree":"Pred (That Pizza) (Very Boring)"}] <dd>[{"tree":"Pred (That Pizza) (Very Boring)"}]
<dt class=js>// Linearize a syntax tree <dt class=js><em>// Linearize a syntax tree</em>
<br>server.linearize("Pred (That Pizza) (Very Boring)","FoodsEng",callback) <br>server.linearize({tree:"Pred (That Pizza) (Very Boring)",to:"FoodsEng"},callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=linearize&tree=Pred+(That+Pizza)+(Very+Boring)&to=FoodsEng <dt>http://localhost:41296/grammars/Foods.pgf?command=linearize&tree=Pred+(That+Pizza)+(Very+Boring)&to=FoodsEng
<dd>[{"to":"FoodsEng","text":"that pizza is very boring"}] <dd>[{"to":"FoodsEng","text":"that pizza is very boring"}]
<dt class=js>server.linearize({tree:"Pred (That Pizza) (Very Boring)"},callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=linearize&tree=Pred+(That+Pizza)+(Very+Boring) <dt>http://localhost:41296/grammars/Foods.pgf?command=linearize&tree=Pred+(That+Pizza)+(Very+Boring)
<dd>[{"to":"FoodsBul","text":"онази пица е много еднообразна"}, <dd>[{"to":"FoodsBul","text":"онази пица е много еднообразна"},
{"to":"FoodsEng","text":"that pizza is very boring"}, {"to":"FoodsEng","text":"that pizza is very boring"},
@@ -99,16 +101,17 @@ incomplete and might be made more complete in a future version...
{"to":"FoodsSwe","text":"den där pizzan är mycket tråkig"}, {"to":"FoodsSwe","text":"den där pizzan är mycket tråkig"},
... ...
] ]
<dt class=js>// Parse a string <dt class=js><em>// Parse a string</em>
<br>server.parse("FoodEng","that pizza is very boring",callback) <br>server.parse({from:"FoodEng",input:"that pizza is very boring"},callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=parse&input=that+pizza+is+very+boring&from=FoodsEng <dt>http://localhost:41296/grammars/Foods.pgf?command=parse&input=that+pizza+is+very+boring&from=FoodsEng
<dd>[{"from":"FoodsEng", <dd>[{"from":"FoodsEng",
"brackets":{"cat":"Comment","fid":10,"index":0,"children":[{"cat":"Item","fid":7,"index":0,"children":[{"token":"that"},{"cat":"Kind","fid":6,"index":0,"children":[{"token":"pizza"}]}]},{"token":"is"},{"cat":"Quality","fid":9,"index":0,"children":[{"token":"very"},{"cat":"Quality","fid":8,"index":0,"children":[{"token":"boring"}]}]}]}, "brackets":{"cat":"Comment","fid":10,"index":0,"children":[{"cat":"Item","fid":7,"index":0,"children":[{"token":"that"},{"cat":"Kind","fid":6,"index":0,"children":[{"token":"pizza"}]}]},{"token":"is"},{"cat":"Quality","fid":9,"index":0,"children":[{"token":"very"},{"cat":"Quality","fid":8,"index":0,"children":[{"token":"boring"}]}]}]},
"trees":["Pred (That Pizza) (Very Boring)"]}] "trees":["Pred (That Pizza) (Very Boring)"]}]
<dt class=js>// Translate to all available langauges <dt class=js><em>// Translate to all available languages</em>
<br>server.translate("FoodEng","that pizza is very boring",callback) <br>server.translate({from:"FoodEng",input:"that pizza is very boring"},callback)
<dd>... <dd>...
<dt class=js>// Translate to one language <dt class=js><em>// Translate to one language</em>
<br>server.translate({input:"that pizza is very boring", from:"FoodEng", to:"FoodSwe"}, callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=translate&input=that+pizza+is+very+boring&from=FoodsEng&to=FoodsSwe <dt>http://localhost:41296/grammars/Foods.pgf?command=translate&input=that+pizza+is+very+boring&from=FoodsEng&to=FoodsSwe
<dd>[{"from":"FoodsEng", <dd>[{"from":"FoodsEng",
"brackets":{"cat":"Comment","fid":10,"index":0,"children":[{"cat":"Item","fid":7,"index":0,"children":[{"token":"that"},{"cat":"Kind","fid":6,"index":0,"children":[{"token":"pizza"}]}]},{"token":"is"},{"cat":"Quality","fid":9,"index":0,"children":[{"token":"very"},{"cat":"Quality","fid":8,"index":0,"children":[{"token":"boring"}]}]}]}, "brackets":{"cat":"Comment","fid":10,"index":0,"children":[{"cat":"Item","fid":7,"index":0,"children":[{"token":"that"},{"cat":"Kind","fid":6,"index":0,"children":[{"token":"pizza"}]}]},{"token":"is"},{"cat":"Quality","fid":9,"index":0,"children":[{"token":"very"},{"cat":"Quality","fid":8,"index":0,"children":[{"token":"boring"}]}]}]},
@@ -117,8 +120,8 @@ incomplete and might be made more complete in a future version...
"linearizations": "linearizations":
[{"to":"FoodsSwe", [{"to":"FoodsSwe",
"text":"den där pizzan är mycket tråkig"}]}]}] "text":"den där pizzan är mycket tråkig"}]}]}]
<dt class=js>// Get completions (what words could come next) <dt class=js><em>// Get completions (what words could come next)</em>
<br>server.complete("FoodEng","that pizza is very ",callback) <br>server.complete({from:"FoodEng",input:"that pizza is very "},callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=complete&input=that+pizza+is+very+&from=FoodsEng <dt>http://localhost:41296/grammars/Foods.pgf?command=complete&input=that+pizza+is+very+&from=FoodsEng
<dd>[{"from":"FoodsEng", <dd>[{"from":"FoodsEng",
"brackets":{"cat":"_","fid":0,"index":0,"children":[{"cat":"Item","fid":7,"index":0,"children":[{"token":"that"},{"cat":"Kind","fid":6,"index":0,"children":[{"token":"pizza"}]}]},{"token":"is"},{"token":"very"}]}, "brackets":{"cat":"_","fid":0,"index":0,"children":[{"cat":"Item","fid":7,"index":0,"children":[{"token":"that"},{"cat":"Kind","fid":6,"index":0,"children":[{"token":"pizza"}]}]},{"token":"is"},{"token":"very"}]},
@@ -127,8 +130,7 @@ incomplete and might be made more complete in a future version...
</dl> </dl>
<hr> <hr>
<div class=modtime><small> <div class=modtime><small>
<!-- hhmts start --> Last modified: Wed Aug 3 15:33:17 CEST 2011 <!-- hhmts end --> <!-- hhmts start --> Last modified: Wed Aug 3 17:19:59 CEST 2011 <!-- hhmts end -->
</small></div> </small></div>
<address> <address><a href="http://www.cse.chalmers.se/~hallgren/">TH</a></address>
<a href="http://www.cse.chalmers.se/~hallgren/">TH</a>
</address>

View File

@@ -157,7 +157,8 @@ Minibar.prototype.get_completions=function() {
with(this) { with(this) {
//debug("get_completions "); //debug("get_completions ");
words.innerHTML="..."; words.innerHTML="...";
server.complete(current.from,current.input,bind(show_completions,this)); server.complete({from:current.from,input:current.input},
bind(show_completions,this));
} }
} }
@@ -232,9 +233,11 @@ Minibar.prototype.get_translations=function() {
with(this) { with(this) {
var c=current; var c=current;
if(options.show_grouped_translations) if(options.show_grouped_translations)
server.translategroup(c.from,c.input,bind(show_groupedtranslations,this)); server.translategroup({from:c.from,input:c.input},
bind(show_groupedtranslations,this));
else else
server.translate(c.from,c.input,bind(show_translations,this)); server.translate({from:c.from,input:c.input},
bind(show_translations,this));
} }
} }
@@ -291,9 +294,9 @@ Minibar.prototype.generate_random=function() {
} }
function lin_random(abs) { function lin_random(abs) {
t.server.linearize(abs[0].tree,t.current.from,show_random); t.server.linearize({tree:abs[0].tree,to:t.current.from},show_random);
} }
t.server.get_random(lin_random); t.server.get_random({},lin_random);
} }
Minibar.prototype.add_words=function(s) { Minibar.prototype.add_words=function(s) {

View File

@@ -24,27 +24,27 @@ function pgf_online(options) {
http_get_json(url,cont); http_get_json(url,cont);
}, },
get_languages: function(cont) { get_languages: function(cont) { this.pgf_call("grammar",{},cont); },
this.pgf_call("grammar",{},cont); grammar_info: function(cont) { this.pgf_call("grammar",{},cont); },
},
get_random: function(cont) { // cat, limit get_random: function(args,cont) { // cat, limit
this.pgf_call("random",{random:Math.random()},cont); args.random=Math.random(); // side effect!!
this.pgf_call("random",args,cont);
}, },
linearize: function(tree,to,cont) { // tree, to linearize: function(args,cont) { // tree, to
this.pgf_call("linearize",{tree:tree,to:to},cont); this.pgf_call("linearize",args,cont);
}, },
complete: function(from,input,cont) { // from, input, cat, limit complete: function(args,cont) { // from, input, cat, limit
this.pgf_call("complete",{from:from,input:input},cont); this.pgf_call("complete",args,cont);
}, },
parse: function(from,input,cont) { // from, input cat parse: function(args,cont) { // from, input, cat
this.pgf_call("parse",{from:from,input:input},cont); this.pgf_call("parse",args,cont);
}, },
translate: function(from,input,cont) { // from, input, cat, to translate: function(args,cont) { // from, input, cat, to
this.pgf_call("translate",{from:from,input:input},cont); this.pgf_call("translate",args,cont);
}, },
translategroup: function(from,input,cont) { // from, input, cat, to translategroup: function(args,cont) { // from, input, cat, to
this.pgf_call("translategroup",{from:from,input:input},cont); this.pgf_call("translategroup",args,cont);
} }
}; };