diff options
| author | Alon Levy <alon@pobox.com> | 2014-10-08 13:37:16 +0300 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-10-08 13:37:18 +0300 |
| commit | 4aa9df9352080ec5f45730ccf5594395fb0a86bf (patch) | |
| tree | 9428e1b94fb93c01c66609154b72a0941d628712 | |
| parent | eda05d4d8df67a26598f21883f1cf920d2e311fa (diff) | |
Fixes #85
This is more of #49 basically, just for a more complex sentence.
I've kept the flow that we have, but augmented the graph change check
(myGraph.compareSubset) and introduced some helper functions for sets on
the way.
| -rw-r--r-- | scripts/rhizicore.js | 80 | ||||
| -rw-r--r-- | scripts/textanalysis.js | 43 |
2 files changed, 98 insertions, 25 deletions
diff --git a/scripts/rhizicore.js b/scripts/rhizicore.js index 637a321b..235969a6 100644 --- a/scripts/rhizicore.js +++ b/scripts/rhizicore.js @@ -22,6 +22,36 @@ var scrollValue = 0, var force; +function set_from_array(a) { + var ret = {}; + for (var k = 0 ; k < a.length ; ++k) { + ret[a[k]] = 1; + } + return ret; +} + +function set_diff(sa, sb) { + var ret = {a_b:[], b_a:[]}; + var i; + for (i in sa) { + if (!(i in sb)) { + ret.a_b.push(i); + } + } + for (i in sb) { + if (!(i in sa)) { + ret.b_a.push(i); + } + } + return ret; +} + +function array_diff(aa, ab) { + var sa = set_from_array(aa); + var sb = set_from_array(ab); + return set_diff(sa, sb); +} + function myGraph(el) { this.update = function(no_relayout) { @@ -170,50 +200,52 @@ function myGraph(el) { this.compareSubset = function(state, new_nodes, new_links) { // Note: the nodes include a state=='temp', type=='bubble' node // but it's ok since it exists both in new_nodes and in state_nodes - var state_nodes = findNodes(null, state).sort(); + var state_nodes = findNodes(null, state); var state_links = findLinks(state).map(function(link) { return [link.source.id, link.target.id]; }).sort(); var k; var changed_old_id = undefined, changed_new_id = undefined; var state_source, state_target, new_source, new_target; + var changed_nodes; + var verbose = false; // XXX should be global. should have only one global. sigh. + var set_old_id, set_new_id; new_nodes.sort(); new_links.sort(); if (new_nodes.length != state_nodes.length || new_links.length != state_links.length) { return {graph_same: false}; } - for (k in state_nodes) { - if (new_nodes[k] != state_nodes[k].id) { - if (changed_old_id === undefined) { - // found the changed node - changed_old_id = state_nodes[k].id; - changed_new_id = new_nodes[k]; - } else { - return {graph_same: false}; - } - } + changed_nodes = set_diff(set_from_array(state_nodes.map(function(d) { return d.id; })), + set_from_array(new_nodes)); + // we allow any number of changed nodes as long as we it is 1 or 2 :) + if (changed_nodes.a_b.length <= 2) { + set_old_id = set_from_array(changed_nodes.a_b); + set_new_id = set_from_array(changed_nodes.b_a); + } else { + return {graph_same: false}; } - for (k in state_links) { + for (k = 0 ; k < state_links.length ; ++k) { state_source = state_links[k][0]; state_target = state_links[k][1]; new_source = new_links[k][0]; new_target = new_links[k][1]; - if (state_source != new_source || - state_target != new_target) { - if ((state_source == changed_old_id && - new_source == changed_new_id && - state_target == new_target) || - (state_target == changed_old_id && - new_target == changed_new_id && - state_source == new_source)) { - // this one is ok - } else { - return {graph_same: false}; + if ((state_source != new_source && + !(state_source in set_old_id && new_source in set_new_id)) + || + (state_target != new_target && + !(state_target in set_old_id && new_target in set_new_id))) { + if (verbose) { + console.log('not same link: ' + + state_source + '->' + state_target + ' != ' + + new_source + '->' + new_target); + console.log(set_old_id); + console.log(set_new_id); } + return {graph_same: false}; } } - return {graph_same: true, old_id: changed_old_id, new_id: changed_new_id}; + return {graph_same: true, old_id: changed_nodes.a_b, new_id: changed_nodes.b_a}; } this.addLink = function(sourceId, targetId, name, state, drop_conjugator_links) { diff --git a/scripts/textanalysis.js b/scripts/textanalysis.js index 6bf6070b..8e2cfb96 100644 --- a/scripts/textanalysis.js +++ b/scripts/textanalysis.js @@ -38,6 +38,47 @@ function autocompleteCallback(request, response_callback) response_callback(ret); } +function same_up_to_one_letter(s1, s2) +{ + return ((s1.length == s2.length + 1 && s1.substr(0, s2.length) == s2) || + (s2.length == s1.length + 1 && s2.substr(0, s1.length) == s1)); +} + +function up_to_two_renames(graph, old_id, new_id) +{ + var not_one_letter = false; + var k; + + if (old_id.length != new_id.length) { + console.log('bug: up_to_two_renames: not equal inputs'); + return; + } + if (old_id.length > 2) { + console.log('bug: up_to_two_renames: input length 2 < ' + old_id.length); + return; + } + if (old_id.length == 2) { + if (same_up_to_one_letter(old_id[0], new_id[1]) && + same_up_to_one_letter(old_id[1], new_id[0])) { + old_id = [old_id[1], old_id[0]]; + } else { + if (!same_up_to_one_letter(old_id[0], new_id[0]) || + !same_up_to_one_letter(old_id[1], new_id[1])) { + not_one_letter = true; + } + } + } + if (not_one_letter) { + console.log('bug: up_to_two_renames: not one letter changes'); + console.log(old_id); + console.log(new_id); + return; + } + for (k = 0 ; k < old_id.length ; ++k) { + graph.editName(old_id[k], null, new_id[k]); + } +} + /* * textAnalyser2 * @@ -329,7 +370,7 @@ var textAnalyser2 = function (newtext, finalize) { var k, n, l; if (comp.graph_same && !finalize) { if (comp.old_id && comp.new_id) { - graph.editName(comp.old_id, null, comp.new_id); + up_to_two_renames(graph, comp.old_id, comp.new_id); } for (k in ret.links) { l = ret.links[k]; |
