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 1a25f04303
commit b77e95abf5
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>
<title>PGF online server example</title>
<style type="text/css">
body { background: #ddd; }
h1, h2, h3, small, th { font-family: sans-serif; }
div.modtime { float: right; }
.modtime { color: #666; white-space: nowrap; }
</style>
<script type="text/JavaScript" src="http://www.grammaticalframework.org/src/runtime/javascript/minibar/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="support.js"></script>
<script type="text/JavaScript" src="pgf_online.js"></script>
<script type="text/JavaScript">
var server_options={
grammars_url: "http://www.grammaticalframework.org/grammars/",
grammar_list: ["Syllogism.pgf"]
grammar_list: ["Foods.pgf"]
}
var pgf_server = pgf_online(server_options);
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) {
@@ -31,7 +35,7 @@ function show_output(parsed) {
<form onsubmit="call_server(); return false">
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>
<p>
@@ -46,6 +50,8 @@ Output:
</ul>
<hr>
<address></address>
<!-- hhmts start --> Last modified: Mon Apr 11 14:53:21 CEST 2011 <!-- hhmts end -->
<div class=modtime><small>
<!-- 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>

View File

@@ -8,9 +8,10 @@
body { background: #ddd; }
h1, h2, h3, small, th { font-family: sans-serif; }
dt { background: #ffc; }
dt.js { background: #cef; margin-bottom: 1ex; }
dd { background: white; margin-top: 1ex; margin-bottom: 1ex; }
dt { background: #cef; }
dt.js { background: white; 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>dd { white-space: pre; }
@@ -32,16 +33,17 @@ The
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
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>
<em>Note</em> that pgf_online.js was developed with
one particular web application in mind, so the server API provided is
incomplete and might be made more complete in a future version...
<strong>Note</strong> that <code>pgf_online.js</code> was initially developed
with one particular web application in mind (the minibar), so the server API was
incomplete. It was simplified and generalized in August 2011 to support the
full API.
<dl>
<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.
<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
@@ -51,10 +53,10 @@ incomplete and might be made more complete in a future version...
<h2>Initialization</h2>
<dl class=apiexamples>
<dt class=js>
// Select which server and grammars to use:
<em>// Select which server and grammars to use:</em>
<br>var server_options = {
<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>var server = pgf_online(server_options);
</dl>
@@ -62,15 +64,14 @@ incomplete and might be made more complete in a future version...
<h2>Examples</h2>
<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)
<dt>http://localhost:41296/grammars/grammars.cgi
<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")
<dt class=js>// Get concrete language list and other grammar info
(this method could be renamed)
<br>server.get_languages(callback)
<dt class=js><em>// Get list of concrete languages and other grammar info</em>
<br>server.grammar_info(callback)
<dt>http://localhost:41296/grammars/Foods.pgf
<dd>{"name":"Foods",
"userLanguage":"FoodsEng",
@@ -84,14 +85,15 @@ incomplete and might be made more complete in a future version...
{"name":"FoodsSwe","languageCode":"sv-SE"},
...]
}
<dt class=js>// Get a random syntax tree
<br>server.get_random(callback)
<dt class=js><em>// Get a random syntax tree</em>
<br>server.get_random({},callback)
<dt>http://localhost:41296/grammars/Foods.pgf?command=random
<dd>[{"tree":"Pred (That Pizza) (Very Boring)"}]
<dt class=js>// Linearize a syntax tree
<br>server.linearize("Pred (That Pizza) (Very Boring)","FoodsEng",callback)
<dt class=js><em>// Linearize a syntax tree</em>
<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
<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)
<dd>[{"to":"FoodsBul","text":"онази пица е много еднообразна"},
{"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"},
...
]
<dt class=js>// Parse a string
<br>server.parse("FoodEng","that pizza is very boring",callback)
<dt class=js><em>// Parse a string</em>
<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
<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"}]}]}]},
"trees":["Pred (That Pizza) (Very Boring)"]}]
<dt class=js>// Translate to all available langauges
<br>server.translate("FoodEng","that pizza is very boring",callback)
<dt class=js><em>// Translate to all available languages</em>
<br>server.translate({from:"FoodEng",input:"that pizza is very boring"},callback)
<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
<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"}]}]}]},
@@ -117,8 +120,8 @@ incomplete and might be made more complete in a future version...
"linearizations":
[{"to":"FoodsSwe",
"text":"den där pizzan är mycket tråkig"}]}]}]
<dt class=js>// Get completions (what words could come next)
<br>server.complete("FoodEng","that pizza is very ",callback)
<dt class=js><em>// Get completions (what words could come next)</em>
<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
<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"}]},
@@ -127,8 +130,7 @@ incomplete and might be made more complete in a future version...
</dl>
<hr>
<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>
<address>
<a href="http://www.cse.chalmers.se/~hallgren/">TH</a>
</address>
<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) {
//debug("get_completions ");
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) {
var c=current;
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
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) {
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) {

View File

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