mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-23 18:02:54 -06:00
minibar: word-for-word replacement (experimental)
The new functionality also needs some new PGF service functionality, which will be provided in a separate patch.
This commit is contained in:
@@ -21,7 +21,7 @@ div#words {
|
|||||||
border: 3px solid #e0e0e0;
|
border: 3px solid #e0e0e0;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.word, span.word, div#words div, div#words input[type=button] {
|
div.word, span.word, div#words div, div#words input[type=button], div#surface input[type=button] {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
font-family: sans-serif;
|
font-family: sans-serif;
|
||||||
font-size: 100%;
|
font-size: 100%;
|
||||||
@@ -31,6 +31,12 @@ div.word, span.word, div#words div, div#words input[type=button] {
|
|||||||
margin: 3px;
|
margin: 3px;
|
||||||
box-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
box-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
||||||
}
|
}
|
||||||
|
div#surface span.editable, div#surface input[type=button] {
|
||||||
|
color: blue;
|
||||||
|
border-color: blue;
|
||||||
|
box-shadow: 2px 2px 4px rgba(0,0,255,0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
.invalid { color: red; }
|
.invalid { color: red; }
|
||||||
|
|
||||||
@@ -53,3 +59,18 @@ span.field { background-color: #eee; }
|
|||||||
pre.feedbacklist { background: white }
|
pre.feedbacklist { background: white }
|
||||||
|
|
||||||
img.button { padding: 1px; }
|
img.button { padding: 1px; }
|
||||||
|
|
||||||
|
div.brackets {
|
||||||
|
margin: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
span.brackets {
|
||||||
|
background: #ddd;
|
||||||
|
display: inline-block;
|
||||||
|
border: 1px solid black;
|
||||||
|
padding: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.brackets .token {
|
||||||
|
padding: 0.5ex
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ function Input(server,translations,opts) { // Input object constructor
|
|||||||
default_source_language: null,
|
default_source_language: null,
|
||||||
startcat_menu: true,
|
startcat_menu: true,
|
||||||
random_button: true,
|
random_button: true,
|
||||||
|
word_replacements: false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply supplied options
|
// Apply supplied options
|
||||||
@@ -46,6 +47,8 @@ function Input(server,translations,opts) { // Input object constructor
|
|||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.change_grammar=function (grammar) {
|
Input.prototype.change_grammar=function (grammar) {
|
||||||
|
grammar.browse={} // for caching output from the browse command
|
||||||
|
this.grammar=grammar;
|
||||||
update_language_menu(this.from_menu,grammar);
|
update_language_menu(this.from_menu,grammar);
|
||||||
set_initial_language(this.options,this.from_menu,grammar);
|
set_initial_language(this.options,this.from_menu,grammar);
|
||||||
this.update_startcat_menu(grammar)
|
this.update_startcat_menu(grammar)
|
||||||
@@ -91,9 +94,9 @@ Input.prototype.get_completions=function() {
|
|||||||
with(this) {
|
with(this) {
|
||||||
//debug("get_completions ");
|
//debug("get_completions ");
|
||||||
words.innerHTML="...";
|
words.innerHTML="...";
|
||||||
server.complete({from:current.from,input:current.input,
|
var args={from:current.from,input:current.input,cat:startcat_menu.value}
|
||||||
cat:startcat_menu.value},
|
server.complete(args,bind(show_completions,this));
|
||||||
bind(show_completions,this));
|
if(options.word_replacements) server.parse(args,bind(get_tree1,this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,6 +285,106 @@ Input.prototype.delete_last=function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* --- Structural editing --------------------------------------------------- */
|
||||||
|
|
||||||
|
Input.prototype.get_tree1=function(parse_output) {
|
||||||
|
with(this) {
|
||||||
|
if(parse_output.length==1 && parse_output[0].from==current.from
|
||||||
|
&& parse_output[0].trees
|
||||||
|
&& parse_output[0].trees.length==1)
|
||||||
|
server.linearize({to:current.from,tree:parse_output[0].trees[0]},
|
||||||
|
bind(get_tree2,this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Input.prototype.get_tree2=function(lin) {
|
||||||
|
with(this) {
|
||||||
|
if(lin.length==1 && lin[0].to==current.from
|
||||||
|
&& lin[0].text+" "==current.input
|
||||||
|
&& lin[0].brackets)
|
||||||
|
enable_structural_editing(lin[0].brackets)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Input.prototype.enable_structural_editing=function(brackets) {
|
||||||
|
var t=this;
|
||||||
|
with(t) {
|
||||||
|
function add_bs(b,parent) {
|
||||||
|
if(b.token) {
|
||||||
|
var fun=parent.fun,cat=parent.cat;
|
||||||
|
function showrepl() { t.show_replacements(brackets,parent) }
|
||||||
|
var w= span_class("word editable",text(b.token));
|
||||||
|
if(fun && cat) w.onclick=showrepl
|
||||||
|
w.title=(fun||"_")+":"+(cat||"_")
|
||||||
|
surface.appendChild(w);
|
||||||
|
}
|
||||||
|
else b.children.map(function(c){add_bs(c,b)})
|
||||||
|
}
|
||||||
|
var typed=surface.typed;
|
||||||
|
surface.innerHTML="";
|
||||||
|
add_bs(brackets);
|
||||||
|
if(typed) surface.appendChild(typed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Input.prototype.show_replacements=function(brackets,parent) {
|
||||||
|
var fun=parent.fun,cat=parent.cat;
|
||||||
|
var t=this;
|
||||||
|
with(t) {
|
||||||
|
function browse1(fun_info) {
|
||||||
|
var fun_type = fun_info.def.split(":")[1];
|
||||||
|
function browse2(cat_info) {
|
||||||
|
function examine_replacement(rfun) {
|
||||||
|
function browse3(rfun_info) {
|
||||||
|
var rfun_type=rfun_info.def.split(":")[1];
|
||||||
|
function replace() {
|
||||||
|
t.replace_word(brackets,parent,rfun);
|
||||||
|
}
|
||||||
|
if(rfun_type==fun_type)
|
||||||
|
t.words.appendChild(button(rfun,replace))
|
||||||
|
}
|
||||||
|
t.browse(rfun,browse3)
|
||||||
|
}
|
||||||
|
var ps=cat_info.producers;
|
||||||
|
t.words.innerHTML="";
|
||||||
|
if(ps)
|
||||||
|
for(var pi in ps)
|
||||||
|
if(ps[pi]!=fun) examine_replacement(ps[pi])
|
||||||
|
}
|
||||||
|
t.browse(cat,browse2)
|
||||||
|
}
|
||||||
|
t.browse(fun,browse1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Input.prototype.replace_word=function(brackets,parent,fun) {
|
||||||
|
var t=this;
|
||||||
|
with(t) {
|
||||||
|
parent.fun=fun;
|
||||||
|
var tree=show_abstract_tree(brackets);
|
||||||
|
function replace(lin_output) {
|
||||||
|
if(lin_output.length==1 && lin_output[0].to==t.current.from) {
|
||||||
|
t.clear_all1();
|
||||||
|
t.add_words(lin_output[0].text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
server.linearize({to:current.from,tree:tree},replace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Input.prototype.browse=function(id,cont) {
|
||||||
|
var t=this;
|
||||||
|
if(t.grammar.browse[id]) cont(t.grammar.browse[id])
|
||||||
|
else {
|
||||||
|
function browsed(info) {
|
||||||
|
t.grammar.browse[id]=info;
|
||||||
|
cont(info);
|
||||||
|
}
|
||||||
|
server.pgf_call("browse",{id:id,format:"json"},browsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* --- Auxiliary functions -------------------------------------------------- */
|
/* --- Auxiliary functions -------------------------------------------------- */
|
||||||
|
|
||||||
function set_initial_language(options,menu,grammar) {
|
function set_initial_language(options,menu,grammar) {
|
||||||
@@ -294,3 +397,25 @@ function set_initial_language(options,menu,grammar) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function show_abstract_tree(b) { return show_tree(abstract_tree(b)) }
|
||||||
|
|
||||||
|
function abstract_tree(b) {
|
||||||
|
return { fun:b.fun,args:abstract_trees(b.children) }
|
||||||
|
}
|
||||||
|
|
||||||
|
function abstract_trees(bs) {
|
||||||
|
var as=[];
|
||||||
|
for(var i in bs)
|
||||||
|
if(bs[i].fun) as.push(abstract_tree(bs[i]))
|
||||||
|
return as
|
||||||
|
}
|
||||||
|
|
||||||
|
function show_tree(t) {
|
||||||
|
return t.fun+" "+t.args.map(show_tree_atomic).join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
function show_tree_atomic(t) {
|
||||||
|
var s=show_tree(t);
|
||||||
|
return t.args.length>0 ? "("+s+")" : s
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,13 +8,15 @@ var online_options={
|
|||||||
}
|
}
|
||||||
|
|
||||||
var minibar_options= {
|
var minibar_options= {
|
||||||
show_abstract: true,
|
show_abstract: true,
|
||||||
show_trees: true,
|
show_trees: true,
|
||||||
tree_img_format: "png", // or "svg"
|
tree_img_format: "png", // or "svg"
|
||||||
show_grouped_translations: false,
|
show_grouped_translations: false,
|
||||||
default_source_language: "Eng",
|
show_brackets: true,
|
||||||
//feedback_url: "feedback.html",
|
word_replacements: true,
|
||||||
try_google: true
|
default_source_language: "Eng",
|
||||||
|
//feedback_url: "feedback.html",
|
||||||
|
try_google: true
|
||||||
}
|
}
|
||||||
|
|
||||||
if(/^\?\/tmp\//.test(location.search)) {
|
if(/^\?\/tmp\//.test(location.search)) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ function Translations(server,opts) {
|
|||||||
tree_img_format: "png", // format for trees & alignment images,
|
tree_img_format: "png", // format for trees & alignment images,
|
||||||
// can be "gif", "png" or "svg"
|
// can be "gif", "png" or "svg"
|
||||||
show_grouped_translations: true,
|
show_grouped_translations: true,
|
||||||
|
show_brackets: false // show bracketed string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply supplied options
|
// Apply supplied options
|
||||||
@@ -74,7 +75,7 @@ Translations.prototype.show_translations=function(translationResults) {
|
|||||||
var trans=main;
|
var trans=main;
|
||||||
//var to=target_lang(); // wrong
|
//var to=target_lang(); // wrong
|
||||||
var to=to_menu.value;
|
var to=to_menu.value;
|
||||||
var cnt=translationResults.length;
|
var cnt=translationResults.length; // cnt==1 usually
|
||||||
//trans.translations=translations;
|
//trans.translations=translations;
|
||||||
trans.single_translation=[];
|
trans.single_translation=[];
|
||||||
trans.innerHTML="";
|
trans.innerHTML="";
|
||||||
@@ -85,6 +86,7 @@ Translations.prototype.show_translations=function(translationResults) {
|
|||||||
*/
|
*/
|
||||||
for(var p=0;p<cnt;p++) {
|
for(var p=0;p<cnt;p++) {
|
||||||
var tra=translationResults[p];
|
var tra=translationResults[p];
|
||||||
|
var bra=tra.brackets;
|
||||||
if (tra.translations != null) {
|
if (tra.translations != null) {
|
||||||
for (q = 0; q < tra.translations.length; q++) {
|
for (q = 0; q < tra.translations.length; q++) {
|
||||||
var t = tra.translations[q];
|
var t = tra.translations[q];
|
||||||
@@ -99,6 +101,8 @@ Translations.prototype.show_translations=function(translationResults) {
|
|||||||
for(var i=0;i<lin.length;i++) {
|
for(var i=0;i<lin.length;i++) {
|
||||||
if(lin[i].to==to)
|
if(lin[i].to==to)
|
||||||
trans.single_translation.push(lin[i].text);
|
trans.single_translation.push(lin[i].text);
|
||||||
|
if(lin[i].to==current.from && lin[i].brackets)
|
||||||
|
bra=lin[i].brackets;
|
||||||
if(to=="All" || lin[i].to==to)
|
if(to=="All" || lin[i].to==to)
|
||||||
tbody.appendChild(tr([th(text(langpart(lin[i].to,grammar.name)+": ")),
|
tbody.appendChild(tr([th(text(langpart(lin[i].to,grammar.name)+": ")),
|
||||||
tdt(parsetree_button(t.tree,lin[i].to),
|
tdt(parsetree_button(t.tree,lin[i].to),
|
||||||
@@ -108,10 +112,13 @@ Translations.prototype.show_translations=function(translationResults) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(tra.typeErrors) {
|
else if(tra.typeErrors) {
|
||||||
var errs=tra.typeErrors;
|
var errs=tra.typeErrors;
|
||||||
for(var i=0;i<errs.length;i++)
|
for(var i=0;i<errs.length;i++)
|
||||||
trans.appendChild(wrap("pre",text(errs[i].msg)))
|
trans.appendChild(wrap("pre",text(errs[i].msg)))
|
||||||
}
|
}
|
||||||
|
if(options.show_brackets)
|
||||||
|
trans.appendChild(div_class("brackets",draw_brackets(bra)));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,3 +174,10 @@ Translations.prototype.parsetree_button=function(abs,lang) {
|
|||||||
+"?command=parsetree&format="+f+"&from="+lang+"&tree="+encodeURIComponent(abs);
|
+"?command=parsetree&format="+f+"&from="+lang+"&tree="+encodeURIComponent(abs);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function draw_brackets(b) {
|
||||||
|
return b.token
|
||||||
|
? span_class("token",text(b.token))
|
||||||
|
: node("span",{"class":"brackets",title:(b.fun||"_")+":"+b.cat},
|
||||||
|
b.children.map(draw_brackets))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user