mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
change Food to Words in Phrasebook, since it's unpractical to have many small modules; added syntactic forms and words
This commit is contained in:
37
examples/phrasebook/www/phrasebook.css
Normal file
37
examples/phrasebook/www/phrasebook.css
Normal file
@@ -0,0 +1,37 @@
|
||||
body {
|
||||
background: #ccc url("http://www.grammaticalframework.org:41296/fridge/se.chalmers.cs.gf.gwt.FridgeApp/brushed-metal.png");
|
||||
}
|
||||
|
||||
h1, h2, h3, small, th { font-family: sans-serif; }
|
||||
|
||||
th, td { vertical-align: baseline; text-align: left; }
|
||||
|
||||
div#surface {
|
||||
min-height: 3ex;
|
||||
margin: 5px;
|
||||
padding: 5px;
|
||||
border: 3px dashed #e0e0e0;
|
||||
}
|
||||
|
||||
div#words {
|
||||
min-height: 3ex;
|
||||
margin: 5px;
|
||||
padding: 6px;
|
||||
border: 3px solid #e0e0e0;
|
||||
}
|
||||
|
||||
div.word, span.word, div#words div {
|
||||
display: inline-block;
|
||||
font-family: sans-serif;
|
||||
background-color: white;
|
||||
border: 1px solid black;
|
||||
padding: 3px;
|
||||
margin: 3px;
|
||||
}
|
||||
|
||||
.invalid { color: red; }
|
||||
|
||||
div.modtime { float: right; }
|
||||
.modtime { color: #444; white-space: nowrap; }
|
||||
|
||||
ul.space>li { margin-top: 0.5ex; }
|
||||
28
examples/phrasebook/www/phrasebook.html
Normal file
28
examples/phrasebook/www/phrasebook.html
Normal file
@@ -0,0 +1,28 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html> <head>
|
||||
<title>Phrasebook</title>
|
||||
<link rel=stylesheet type="text/css" href="phrasebook.css">
|
||||
<script type="text/JavaScript" src="support.js"></script>
|
||||
<script type="text/JavaScript" src="phrasebook.js"></script>
|
||||
</head>
|
||||
|
||||
<body onload="start_minibar()">
|
||||
|
||||
<div id=minibar>
|
||||
</div>
|
||||
<hr>
|
||||
|
||||
<small>
|
||||
|
||||
Based on
|
||||
<a href="http://www.cs.chalmers.se/~hallgren/minibar/minibar.html">minibar</a>,
|
||||
powered by
|
||||
<a href="http://www.grammaticalframework.org">GF</a>
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
|
||||
|
||||
211
examples/phrasebook/www/phrasebook.js
Normal file
211
examples/phrasebook/www/phrasebook.js
Normal file
@@ -0,0 +1,211 @@
|
||||
|
||||
//var server="http://www.grammaticalframework.org:41296"
|
||||
//var server="http://tournesol.cs.chalmers.se:41296";
|
||||
var server="http://localhost:41296";
|
||||
var grammars_url=server+"/grammars/";
|
||||
var current_grammar_url=grammars_url+"Foods.pgf";
|
||||
|
||||
var tree_icon=server+"/translate/se.chalmers.cs.gf.gwt.TranslateApp/tree-btn.png";
|
||||
|
||||
function start_minibar() {
|
||||
var minibar=element("minibar");
|
||||
minibar.appendChild(div_id("menubar"));
|
||||
minibar.appendChild(div_id("surface"));
|
||||
minibar.appendChild(div_id("words"));
|
||||
minibar.appendChild(div_id("translations"));
|
||||
/// jsonp(grammars_url+"grammars.cgi",""); // calls show_grammarlist
|
||||
show_grammarlist(["Phrasebook.pgf","Foods.pgf", "ResourceDemo.pgf"]) ;
|
||||
}
|
||||
|
||||
function show_grammarlist(grammars) {
|
||||
var menu=empty("select");
|
||||
for(var i=0;i<grammars.length;i++) {
|
||||
var opt=empty("option");
|
||||
opt.setAttribute("value",grammars[i]);
|
||||
opt.innerHTML=grammars[i];
|
||||
menu.appendChild(opt);
|
||||
}
|
||||
menu.setAttribute("onchange","new_grammar(this)");
|
||||
var menubar=element("menubar");
|
||||
// menubar.innerHTML="Grammar: ";
|
||||
// menubar.appendChild(menu);
|
||||
menubar.appendChild(text("From: "));
|
||||
menubar.appendChild(empty_id("select","language_menu"));
|
||||
menubar.appendChild(button("Del","delete_last()"));
|
||||
menubar.appendChild(button("Clear","clear_all()"));
|
||||
menubar.appendChild(button("Random","generate_random()"));
|
||||
select_grammar(grammars[0]);
|
||||
}
|
||||
|
||||
function new_grammar(menu) {
|
||||
select_grammar(menu.options[menu.selectedIndex].value);
|
||||
}
|
||||
|
||||
function select_grammar(grammar_name) {
|
||||
current_grammar_url=grammars_url+grammar_name;
|
||||
jsonp(current_grammar_url,"show_languages");
|
||||
}
|
||||
|
||||
function show_languages(grammar) {
|
||||
var r="";
|
||||
var lang=grammar.languages;
|
||||
var menu=element("language_menu");
|
||||
menu.setAttribute("onchange","new_language(this)");
|
||||
menu.grammar=grammar;
|
||||
menu.innerHTML="";
|
||||
for(var i=0; i<lang.length; i++) {
|
||||
if(lang[i].canParse) {
|
||||
var opt=empty("option");
|
||||
opt.setAttribute("value",""+i);
|
||||
opt.innerHTML=short_name(lang[i].name);
|
||||
menu.appendChild(opt);
|
||||
}
|
||||
}
|
||||
new_language(menu);
|
||||
}
|
||||
|
||||
function short_name(cnc) {
|
||||
var s = "" ;
|
||||
for (var c = cnc.length - 3 ; c < cnc.length ; c++)
|
||||
s = s + cnc[c] ;
|
||||
return s ;
|
||||
}
|
||||
|
||||
function new_language(menu) {
|
||||
var ix=menu.options[menu.selectedIndex].value;
|
||||
var langname=menu.grammar.languages[ix].name;
|
||||
menu.current={from: langname, input: ""};
|
||||
clear_all();
|
||||
}
|
||||
|
||||
function clear_all1() {
|
||||
var menu=element("language_menu");
|
||||
menu.current.input="";
|
||||
menu.previous=null;
|
||||
element("surface").innerHTML="";
|
||||
element("translations").innerHTML="";
|
||||
return menu;
|
||||
}
|
||||
|
||||
function clear_all() {
|
||||
get_completions(clear_all1());
|
||||
}
|
||||
|
||||
function delete_last() {
|
||||
var menu=element("language_menu");
|
||||
if(menu.previous) {
|
||||
menu.current.input=menu.previous.input;
|
||||
menu.previous=menu.previous.previous;
|
||||
var s=element("surface");
|
||||
s.removeChild(s.lastChild);
|
||||
element("translations").innerHTML="";
|
||||
get_completions(menu);
|
||||
}
|
||||
}
|
||||
|
||||
function generate_random() {
|
||||
jsonp(current_grammar_url+"?command=random&random="+Math.random(),"lin_random");
|
||||
|
||||
}
|
||||
|
||||
function lin_random(abs) {
|
||||
var menu=element("language_menu");
|
||||
var lang=menu.current.from;
|
||||
jsonp(current_grammar_url+"?command=linearize&tree="+encodeURIComponent(abs[0].tree)
|
||||
+"&to="+lang,
|
||||
"show_random")
|
||||
|
||||
}
|
||||
|
||||
function show_random(random) {
|
||||
var menu=clear_all1();
|
||||
var words=random[0].text.split(" ");
|
||||
for(var i=0;i<words.length;i++)
|
||||
add_word1(menu,words[i]+" ");
|
||||
element("words").innerHTML="...";
|
||||
get_completions(menu);
|
||||
}
|
||||
|
||||
function get_completions(menu) {
|
||||
var c=menu.current;
|
||||
jsonp(current_grammar_url
|
||||
+"?command=complete"
|
||||
+"&from="+encodeURIComponent(c.from)
|
||||
+"&input="+encodeURIComponent(c.input),
|
||||
"show_completions");
|
||||
}
|
||||
|
||||
function word(s) {
|
||||
var w=div_class("word",text(s));
|
||||
w.setAttribute("onclick",'add_word("'+s+'")');
|
||||
return w;
|
||||
}
|
||||
|
||||
function add_word1(menu,s) {
|
||||
menu.previous={ input: menu.current.input, previous: menu.previous };
|
||||
menu.current.input+=s;
|
||||
element("surface").appendChild(span_class("word",text(s)));
|
||||
}
|
||||
|
||||
function add_word(s) {
|
||||
var menu=element("language_menu");
|
||||
add_word1(menu,s);
|
||||
element("words").innerHTML="...";
|
||||
get_completions(menu);
|
||||
}
|
||||
|
||||
function show_completions(completions) {
|
||||
var box=element("words");
|
||||
var menu=element("language_menu");
|
||||
var prefixlen=menu.current.input.length;
|
||||
var emptycnt=0;
|
||||
box.innerHTML="";
|
||||
for(var i=0;i<completions.length;i++) {
|
||||
var s=completions[i].text.substring(prefixlen);
|
||||
if(s.length>0) box.appendChild(word(s));
|
||||
else emptycnt++;
|
||||
}
|
||||
if(emptycnt>0)
|
||||
//setTimeout(function(){get_translations(menu);},200);
|
||||
get_translations(menu);
|
||||
}
|
||||
|
||||
function get_translations(menu) {
|
||||
jsonp(current_grammar_url
|
||||
+"?command=translategroup"
|
||||
// +"?command=translate"
|
||||
+"&from="+encodeURIComponent(menu.current.from)
|
||||
+"&input="+encodeURIComponent(menu.current.input),
|
||||
"show_translations")
|
||||
}
|
||||
|
||||
function show_translations(translations) {
|
||||
var trans=element("translations");
|
||||
var cnt=translations.length;
|
||||
trans.innerHTML="";
|
||||
for(p=0;p<cnt;p++) {
|
||||
var t=translations[p];
|
||||
var lin=t.linearizations;
|
||||
var tbody=empty("tbody");
|
||||
tbody.appendChild(tr([th(text(t.to+":"))]));
|
||||
for(var i=0;i<lin.length;i++)
|
||||
tbody.appendChild(tr([(text(lin[i].text))]));
|
||||
trans.appendChild(wrap("table",tbody));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function toggle_img(i) {
|
||||
var tmp=i.src;
|
||||
i.src=i.other;
|
||||
i.other=tmp;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
se.chalmers.cs.gf.gwt.TranslateApp/align-btn.png
|
||||
|
||||
GET /grammars/Foods.pgf?&command=abstrtree&tree=Pred+(This+Fish)+(Very+Fresh)
|
||||
GET /grammars/Foods.pgf?&command=parsetree&tree=Pred+(This+Fish)+Expensive&from=FoodsAfr
|
||||
GET /grammars/Foods.pgf?&command=alignment&tree=Pred+(This+Fish)+Expensive
|
||||
*/
|
||||
157
examples/phrasebook/www/support.js
Normal file
157
examples/phrasebook/www/support.js
Normal file
@@ -0,0 +1,157 @@
|
||||
function element(id) {
|
||||
return document.getElementById(id);
|
||||
}
|
||||
|
||||
// Inspired by the function jsonp from
|
||||
// http://www.west-wind.com/Weblog/posts/107136.aspx
|
||||
// See also http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/
|
||||
// http://en.wikipedia.org/wiki/JSON#JSONP
|
||||
function jsonp(url,callback)
|
||||
{
|
||||
if (url.indexOf("?") > -1)
|
||||
url += "&jsonp="
|
||||
else
|
||||
url += "?jsonp="
|
||||
url += callback + "&";
|
||||
//url += new Date().getTime().toString(); // prevent caching
|
||||
|
||||
var script = empty("script");
|
||||
script.setAttribute("src",url);
|
||||
script.setAttribute("type","text/javascript");
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
|
||||
/* --- HTML construction ---------------------------------------------------- */
|
||||
function text(s) { return document.createTextNode(s); }
|
||||
|
||||
function empty(tag,name,value) {
|
||||
var el=document.createElement(tag);
|
||||
if(name && value) el.setAttribute(name,value);
|
||||
return el;
|
||||
}
|
||||
|
||||
function empty_id(tag,id) { return empty(tag,"id",id); }
|
||||
function empty_class(tag,cls) { return empty(tag,"class",cls); }
|
||||
|
||||
function div_id(id) { return empty_id("div",id); }
|
||||
|
||||
function wrap(tag,contents) {
|
||||
var el=empty(tag);
|
||||
el.appendChild(contents);
|
||||
return el;
|
||||
}
|
||||
|
||||
function wrap_class(tag,cls,contents) {
|
||||
var el=empty_class(tag,cls);
|
||||
if(contents) el.appendChild(contents);
|
||||
return el;
|
||||
}
|
||||
|
||||
function span_class(cls,contents) { return wrap_class("span",cls,contents); }
|
||||
function div_class(cls,contents) { return wrap_class("div",cls,contents); }
|
||||
|
||||
function p(contents) { return wrap("p",contents); }
|
||||
function dt(contents) { return wrap("dt",contents); }
|
||||
|
||||
function th(contents) { return wrap("th",contents); }
|
||||
function td(contents) { return wrap("td",contents); }
|
||||
|
||||
function tr(cells) {
|
||||
var tr=empty("tr");
|
||||
for(var i=0;i<cells.length;i++)
|
||||
tr.appendChild(cells[i]);
|
||||
return tr;
|
||||
}
|
||||
|
||||
function button(label,action) {
|
||||
var el=empty("input");
|
||||
el.setAttribute("type","button");
|
||||
el.setAttribute("value",label);
|
||||
el.setAttribute("onclick",action);
|
||||
return el;
|
||||
}
|
||||
|
||||
function appendChildren(el,cs) {
|
||||
for(var i=0;i<cs.length;i++)
|
||||
el.appendChild(cs[i]);
|
||||
return el;
|
||||
}
|
||||
|
||||
function tda(cs) { return appendChildren(empty("td"),cs); }
|
||||
|
||||
function img(src) { return empty("img","src",src); }
|
||||
|
||||
/* --- Debug ---------------------------------------------------------------- */
|
||||
|
||||
function show_props(obj, objName) {
|
||||
var result = "";
|
||||
for (var i in obj) {
|
||||
result += objName + "." + i + " = " + obj[i] + "<br>";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function field_names(obj) {
|
||||
var result = "";
|
||||
for (var i in obj) {
|
||||
result += " " + i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --- Data manipulation ---------------------------------------------------- */
|
||||
function swap(a,i,j) { // Note: this doesn't work on strings.
|
||||
var tmp=a[i];
|
||||
a[i]=a[j];
|
||||
a[j]=tmp;
|
||||
return a;
|
||||
}
|
||||
|
||||
function sort(a) { // Note: this doesn't work on strings.
|
||||
for(var i=0;i<a.length-1;i++) {
|
||||
var min=i;
|
||||
for(var j=i+1;j<a.length;j++)
|
||||
if(a[j]<a[min]) min=j;
|
||||
if(min!=i) swap(a,i,min);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
function filter(p,xs) {
|
||||
var ys=[];
|
||||
for(var i=0;i<xs.length;i++)
|
||||
if(p(xs[i])) ys[ys.length]=xs[i];
|
||||
return ys;
|
||||
}
|
||||
|
||||
function implode(cs) {
|
||||
/*
|
||||
var s="";
|
||||
for(var i=0;i<cs.length;i++)
|
||||
s+=cs[i];
|
||||
return s;
|
||||
*/
|
||||
return cs.join("");
|
||||
}
|
||||
/*
|
||||
function all(p,xs) {
|
||||
for(var i=0;i<xs.length;i++)
|
||||
if(!p(xs[i])) return false;
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
function map(f,xs) {
|
||||
var ys=[];
|
||||
for(var i=0;i<xs.length;i++) ys[i]=f(xs[i]);
|
||||
return ys;
|
||||
}
|
||||
|
||||
function mapc_from(f,xs,i,ys,cont) {
|
||||
if(i<xs.length)
|
||||
f(xs[i],function(y){ys[i]=y;mapc_from(f,xs,i+1,ys,cont)});
|
||||
else
|
||||
cont(ys);
|
||||
}
|
||||
|
||||
function mapc(f,xs,cont) { mapc_from(f,xs,0,[],cont); }
|
||||
Reference in New Issue
Block a user