mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-24 10:22:50 -06:00
minibar: better implementation of word replacement
Instead of trying to reconstruct the abstract tree from the bracketed string, use the node numbers (the field called "fid") to identify which node in the abstact syntax tree a word in the bracketed string corresponds to.
This commit is contained in:
@@ -297,26 +297,25 @@ Input.prototype.delete_last=function() {
|
|||||||
|
|
||||||
/* --- Structural editing --------------------------------------------------- */
|
/* --- Structural editing --------------------------------------------------- */
|
||||||
|
|
||||||
Input.prototype.get_tree1=function(parse_output) {
|
Input.prototype.get_tree1=function(parse) {
|
||||||
with(this) {
|
var t=this;
|
||||||
if(parse_output.length==1 && parse_output[0].from==current.from
|
function proceed(lin) { t.get_tree2(lin,parse[0].trees[0]) }
|
||||||
&& parse_output[0].trees
|
|
||||||
&& parse_output[0].trees.length==1)
|
if(parse.length==1 && parse[0].from==t.current.from
|
||||||
server.linearize({to:current.from,tree:parse_output[0].trees[0]},
|
&& parse[0].trees && parse[0].trees.length==1)
|
||||||
bind(get_tree2,this));
|
t.server.linearize({to:t.current.from,tree:parse[0].trees[0]},proceed);
|
||||||
else end_structural_editing();
|
else t.end_structural_editing();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.get_tree2=function(lin) {
|
Input.prototype.get_tree2=function(lin,tree) {
|
||||||
var t=this;
|
var t=this;
|
||||||
with(t) {
|
with(t) {
|
||||||
if(lin.length==1 && lin[0].to==current.from
|
if(lin.length==1 && lin[0].to==current.from
|
||||||
&& lin[0].text+" "==current.input
|
&& lin[0].text+" "==current.input
|
||||||
&& (lin[0].brackets)) {
|
&& (lin[0].brackets)) {
|
||||||
var bs=lin[0].brackets;
|
var bs=lin[0].brackets;
|
||||||
var tree=show_abstract_tree(bs);
|
//var tree=show_abstract_tree(bs);
|
||||||
function proceed() { t.enable_structural_editing(bs) }
|
function proceed() { t.enable_structural_editing(bs,tree) }
|
||||||
server.linearize({to:current.from,tree:tree},
|
server.linearize({to:current.from,tree:tree},
|
||||||
proceed,bind(end_structural_editing,t))
|
proceed,bind(end_structural_editing,t))
|
||||||
}
|
}
|
||||||
@@ -334,16 +333,16 @@ Input.prototype.end_structural_editing=function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.enable_structural_editing=function(brackets) {
|
Input.prototype.enable_structural_editing=function(brackets,tree) {
|
||||||
var t=this;
|
var t=this;
|
||||||
with(t) {
|
with(t) {
|
||||||
function add_bs(b,parent) {
|
function add_bs(b,parent) {
|
||||||
if(b.token) {
|
if(b.token) {
|
||||||
var fun=parent.fun,cat=parent.cat;
|
var fun=parent.fun,cat=parent.cat;
|
||||||
function showrepl() { t.show_replacements(brackets,parent) }
|
function showrepl() { t.show_replacements(brackets,parent,tree)}
|
||||||
var w= span_class("word editable",text(b.token));
|
var w= span_class("word editable",text(b.token));
|
||||||
if(fun && cat) w.onclick=showrepl
|
if(fun && cat) w.onclick=showrepl
|
||||||
w.title=(fun||"_")+":"+(cat||"_")
|
w.title=(fun||"_")+":"+(cat||"_")+" "+parent.fid+":"+parent.index
|
||||||
surface.appendChild(w);
|
surface.appendChild(w);
|
||||||
}
|
}
|
||||||
else b.children.map(function(c){add_bs(c,b)})
|
else b.children.map(function(c){add_bs(c,b)})
|
||||||
@@ -356,7 +355,7 @@ Input.prototype.enable_structural_editing=function(brackets) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.show_replacements=function(brackets,parent) {
|
Input.prototype.show_replacements=function(brackets,parent,tree) {
|
||||||
var fun=parent.fun,cat=parent.cat;
|
var fun=parent.fun,cat=parent.cat;
|
||||||
var t=this;
|
var t=this;
|
||||||
with(t) {
|
with(t) {
|
||||||
@@ -368,7 +367,7 @@ Input.prototype.show_replacements=function(brackets,parent) {
|
|||||||
function browse3(rfun_info) {
|
function browse3(rfun_info) {
|
||||||
var rfun_type=rfun_info.def.split(":")[1];
|
var rfun_type=rfun_info.def.split(":")[1];
|
||||||
function replace() {
|
function replace() {
|
||||||
t.replace_word(brackets,parent,rfun);
|
t.replace_word(brackets,parent,rfun,tree);
|
||||||
}
|
}
|
||||||
if(rfun_type==fun_type)
|
if(rfun_type==fun_type)
|
||||||
t.words.insertBefore(button(rfun,replace),extb);
|
t.words.insertBefore(button(rfun,replace),extb);
|
||||||
@@ -392,11 +391,13 @@ Input.prototype.show_replacements=function(brackets,parent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.replace_word=function(brackets,parent,fun) {
|
Input.prototype.replace_word=function(brackets,parent,fun,tree) {
|
||||||
var t=this;
|
var t=this;
|
||||||
with(t) {
|
function proceed(tree) {
|
||||||
parent.fun=fun;
|
//parent.fun=fun;
|
||||||
var tree=show_abstract_tree(brackets);
|
//var tree=show_abstract_tree(brackets);
|
||||||
|
tree=modify_tree(tree,parent.fid,fun)
|
||||||
|
tree=show_tree(tree) // Convert JSON repr of tree back to string
|
||||||
function replace(lin_output) {
|
function replace(lin_output) {
|
||||||
if(lin_output.length==1 && lin_output[0].to==t.current.from) {
|
if(lin_output.length==1 && lin_output[0].to==t.current.from) {
|
||||||
t.clear_all1();
|
t.clear_all1();
|
||||||
@@ -409,8 +410,10 @@ Input.prototype.replace_word=function(brackets,parent,fun) {
|
|||||||
? text
|
? text
|
||||||
: "Word replacement failed"
|
: "Word replacement failed"
|
||||||
}
|
}
|
||||||
server.linearize({to:current.from,tree:tree},replace,err)
|
t.server.linearize({to:t.current.from,tree:tree},replace,err)
|
||||||
}
|
}
|
||||||
|
// Convert the string representaiton of the abstract syntax tree to JSON:
|
||||||
|
t.server.pgf_call("abstrjson",{tree:tree},proceed)
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.browse=function(id,cont) {
|
Input.prototype.browse=function(id,cont) {
|
||||||
@@ -439,10 +442,11 @@ function set_initial_language(options,menu,grammar) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
function show_abstract_tree(b) { return show_tree(abstract_tree(b)) }
|
function show_abstract_tree(b) { return show_tree(abstract_tree(b)) }
|
||||||
|
|
||||||
function abstract_tree(b) {
|
function abstract_tree(b) {
|
||||||
return { fun:b.fun,args:abstract_trees(b.children) }
|
return { fun:b.fun,children:abstract_trees(b.children) }
|
||||||
}
|
}
|
||||||
|
|
||||||
function abstract_trees(bs) {
|
function abstract_trees(bs) {
|
||||||
@@ -451,12 +455,25 @@ function abstract_trees(bs) {
|
|||||||
if(bs[i].fun) as.push(abstract_tree(bs[i]))
|
if(bs[i].fun) as.push(abstract_tree(bs[i]))
|
||||||
return as
|
return as
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
function show_tree(t) {
|
function show_tree(t) {
|
||||||
return t.fun+" "+t.args.map(show_tree_atomic).join(" ");
|
return t.children
|
||||||
|
? t.fun+" "+t.children.map(show_tree_atomic).join(" ")
|
||||||
|
: t.fun;
|
||||||
}
|
}
|
||||||
|
|
||||||
function show_tree_atomic(t) {
|
function show_tree_atomic(t) {
|
||||||
var s=show_tree(t);
|
var s=show_tree(t);
|
||||||
return t.args.length>0 ? "("+s+")" : s
|
return t.children && t.children.length>0 ? "("+s+")" : s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the node labelled fid in tree and replace the function there with fun
|
||||||
|
function modify_tree(tree,fid,fun) {
|
||||||
|
if(tree.fun) {
|
||||||
|
if(tree.fid==fid) tree.fun=fun
|
||||||
|
else if(tree.children)
|
||||||
|
tree.children.map(function(t) { modify_tree(t,fid,fun) })
|
||||||
|
}
|
||||||
|
return tree;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,6 +178,7 @@ Translations.prototype.parsetree_button=function(abs,lang) {
|
|||||||
function draw_brackets(b) {
|
function draw_brackets(b) {
|
||||||
return b.token
|
return b.token
|
||||||
? span_class("token",text(b.token))
|
? span_class("token",text(b.token))
|
||||||
: node("span",{"class":"brackets",title:(b.fun||"_")+":"+b.cat},
|
: node("span",{"class":"brackets",
|
||||||
|
title:(b.fun||"_")+":"+b.cat+" "+b.fid+":"+b.index},
|
||||||
b.children.map(draw_brackets))
|
b.children.map(draw_brackets))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user