diff options
| author | Alon Levy <alon@pobox.com> | 2014-09-21 16:04:20 +0300 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-09-21 16:04:20 +0300 |
| commit | af27835be74279fe894318fe65d097ddff7384b0 (patch) | |
| tree | 723d07d023d3f92366ea0d434a5183916cc482b1 /scripts/textanalysis.js | |
| parent | 9aa40fd4881538878fa68b035f79a72cfaf13b29 (diff) | |
textanalysis: multiple changes to autocomplete
1. move the sugg variable to textanalysis.js instead of the ui part
2. use a callback for the autocomplete widget (it supports three
options: array, string as url for a json returning service, or callback)
3. use a dictionary instead of an array to keep the list of suggestions.
We iterate on the dictionary instead of using a Trie or some other
datastructure in the callback. But the dictionary avoids iteration when
adding a word. Overall a loss in performance, but not noticeable.
Googling / memory suggests a Trie is what we want here, but I'm
postponing that for later. See:
http://ejohn.org/blog/javascript-trie-performance-analysis
Diffstat (limited to 'scripts/textanalysis.js')
| -rw-r--r-- | scripts/textanalysis.js | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/scripts/textanalysis.js b/scripts/textanalysis.js index 3fe553da..715ccaa3 100644 --- a/scripts/textanalysis.js +++ b/scripts/textanalysis.js @@ -10,9 +10,36 @@ var ExecutionStack = []; var lastnode; +var sugg = {}; // suggestions for autocompletion of node names + var ANALYSIS_NODE_START = 'ANALYSIS_NODE_START'; var ANALYSIS_LINK = 'ANALYSIS_LINK'; +function autoSuggestAddName(name) +{ + console.log('adding suggestion ' + name); + /* note that name can contain spaces - this is ok. We might want to limit this though? */ + if(name.split(" ").length > 1) { + sugg['"'+newnodes[n]+'"'] = 1; + } else { + sugg[name] = 1; + } +} + +function autocompleteCallback(request, response_callback) +{ + console.log('autocompleteCallback: ' + request.term); + var ret = []; + if (request.term === "" || request.term) { + for (var name in sugg) { + if (name.indexOf(request.term) == 0) { + ret.push(name); + } + } + } + response_callback(ret); +} + function TextAnalyser2(newtext, finalize) { var segment = [], subsegment = [], @@ -130,19 +157,9 @@ function TextAnalyser2(newtext, finalize) { var typesetter = ""; if (finalize === true) { typesetter = "perm"; - var sugg = [] for (var n = 0; n < newnodes.length; n++) { - if (Unique(newnodes[n])) { - if(newnodes[n].split(" ").length>1){ - sugg.push('"'+newnodes[n]+'"'); - }else{ - sugg.push(newnodes[n]); - } - - } + autoSuggestAddName(newnodes[n]); } - sugg.push(text); - ret.sugg = sugg; } else { typesetter = "temp"; } @@ -246,15 +263,3 @@ function TextAnalyser2(newtext, finalize) { return ret; } - - - -function Unique(newnode) { - var truth = true; - for (var p = 0; p < sugg.length; p++) { - if (sugg[p] === newnode) { - truth = false; - } - } - return truth; -} |
