From 76d1de512a58f023ae348ae8500100490fba9915 Mon Sep 17 00:00:00 2001 From: LV-426 Date: Thu, 13 Nov 2014 17:41:57 +0200 Subject: rename scripts/ -> src/ --- src/textanalysis.js | 459 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 459 insertions(+) create mode 100644 src/textanalysis.js (limited to 'src/textanalysis.js') diff --git a/src/textanalysis.js b/src/textanalysis.js new file mode 100644 index 00000000..4cff49bc --- /dev/null +++ b/src/textanalysis.js @@ -0,0 +1,459 @@ +"use strict"; + +define(['rz_core', 'model/util', 'model/diff'], +function(rz_core, model_util, model_diff) { + +var typeindex = 0; +var nodetypes = ["person", "project", "skill", "deliverable", "objective"]; +var typeStack = []; + +var lastnode; + +var sugg = {}; // suggestions for autocompletion of node names + +var ANALYSIS_NODE_START = 'ANALYSIS_NODE_START'; +var ANALYSIS_LINK = 'ANALYSIS_LINK'; + +function selectedType() +{ + return nodetypes[typeindex]; +} + +function autoSuggestAddName(name) +{ + /* note that name can contain spaces - this is ok. We might want to limit this though? */ + if(name.split(" ").length > 1) { + sugg['"' + name + '"'] = 1; + } else { + sugg[name] = 1; + } +} + +function autocompleteCallback(request, response_callback) +{ + var ret = []; + if (request.term === "" || request.term) { + for (var name in sugg) { + if (name.toLowerCase().indexOf(request.term.toLowerCase()) === 0) { + ret.push(name); + } + } + } + response_callback(ret); +} + +/* up_to_two_renames: + * + * allow one letter or 'new node' to anything changes */ +function up_to_two_renames(graph, old_name, new_name) +{ + var not_one_letter = false; + var k; + /* Allowed renames: + * no change + * s1 is substring of s2 + * older (s1) node being 'new node' + */ + function allowed_rename(s1, s2) + { + return (s1 == s2 || + s1 == 'new node' || + s1.substr(0, s2.length) == s2 || + s2.substr(0, s1.length) == s1); + } + + if (old_name.length != new_name.length) { + console.log('bug: up_to_two_renames: not equal inputs'); + return; + } + if (old_name.length > 2) { + console.log('bug: up_to_two_renames: input length 2 < ' + old_name.length); + return; + } + if (old_name.length == 2) { + if (allowed_rename(old_name[0], new_name[1]) && + allowed_rename(old_name[1], new_name[0])) { + old_name = [old_name[1], old_name[0]]; + } else { + if (!allowed_rename(old_name[0], new_name[0]) || + !allowed_rename(old_name[1], new_name[1])) { + not_one_letter = true; + } + } + } + if (not_one_letter) { + console.log('bug: up_to_two_renames: not one letter changes'); + console.log(old_name); + console.log(new_name); + return; + } + for (k = 0 ; k < old_name.length ; ++k) { + graph.editNameByName(old_name[k], new_name[k]); + } +} + +// TODO: add escape char, i.e. r"bla\"bla" -> ['bla"bla'] +function tokenize(text, node_token, quote) +{ + var segment = [], + subsegment = [], + sentence = [], + quoteword; + var j; + + segment = text.split(node_token); + for (j = 0; j < segment.length; j++) { + if (j !== 0) sentence.push(node_token); + subsegment = segment[j].split(" "); + if (subsegment.length === 0) { + sentence.push(" "); + } + for (var k = 0; k < subsegment.length; k++) { + if (subsegment[k] !== " " && subsegment[k] !== "") { + if (subsegment[k].charAt(0) === quote) { + quoteword = ""; + do { + quoteword += subsegment[k] + ' '; + if(subsegment[k].charAt(subsegment[k].length-1) !== quote)k++; + } while (k < subsegment.length && subsegment[k].charAt(subsegment[k].length - 1) !== quote); + if (subsegment[k] && subsegment[k]!==quoteword.replace(/ /g, "")) { + quoteword += subsegment[k]; + } + sentence.push(quoteword.replace(new RegExp(quote, 'g'), "")); + } else { + sentence.push(subsegment[k]); + } + } + } + } + return sentence; +} + +/* + * textAnalyser + * + * Input: + * @newtext - new sentence + * @finalize - is this an intermediate editing state or are we editing the graph + * + * Output: + * none + * + * Side effect: + * updating graph (global) + * + * Implementation notes: + * There is no well defined grammer. The translation goes from obvious to not + * so much for more complex sentences involving more then two nodes (two '#' + * marks). + * + */ +var textAnalyser = function (newtext, finalize) { + var sentence, + newlinks = [], + newnodes = [], + linkindex = 0, + nodeindex = 0, + orderStack = [], + and_count = 0, + prefix = "", + ret = {'nodes': [], 'links': []}, + m, + word, + completeSentence, + typesetter, starGraph, + n, + link_hash = {}, + yell_bug = false, // TODO: fix both issues + NODE = "NODE", + LINK = "LINK", + START = "START"; + + function addNode(name, type, state) { + if (type === undefined) { + console.log('bug: textanalyser.addNode of type undefined'); + } + ret.nodes.push({'name':name, 'type':type, 'state':state}); + } + function addLink(src, dst, name, state) { + if (!src || !dst) { + if (yell_bug) { + console.log('bug - adding link (' + src + ', ' + dst + ')'); + } + return; + } + if (link_hash[src] && link_hash[src][dst]) { + if (yell_bug) { + console.log('bug - adding link twice (' + src + ', ' + dst + ')'); + } + return; + } + if (!link_hash[src]) { + link_hash[src] = {}; + } + link_hash[src][dst] = 1; + ret.links.push({'sourceName':src, 'targetName':dst, 'name':name ? name.trim() : "", 'state':state}); + } + + //Sentence Sequencing + //Build the words and cuts the main elements + sentence = tokenize(newtext, '#', '"'); + + //BUILD NEW NODE AND LINK ARRAYS WITH ORDER OF APPEARENCE + for (m = 0; m < sentence.length; m++) { + switch (sentence[m]) { + case "#": + orderStack.push(START); + break; + case "and": + case "+": + case ",": + case "&": + sentence[m] = "and"; + and_count++; + //orderStack.push("AND"); + default: + if (orderStack[orderStack.length - 1] === START) { + orderStack.push(NODE); + newnodes.push(sentence[m]); + linkindex++; + } else if (orderStack[orderStack.length - 1] === NODE) { + orderStack.push(LINK); + if (!newlinks[linkindex]) { + newlinks[linkindex] = sentence[m] + " "; + } else { + newlinks[linkindex] += sentence[m] + " "; + } + } else { + if (!newlinks[linkindex]) { + newlinks[linkindex] = sentence[m] + " "; + } else { + newlinks[linkindex] += sentence[m] + " "; + } + } + if (newnodes.length === 0) { + prefix += (prefix.length > 0 ? ' ' : '') + sentence[m]; + } + break; + } + } + + starGraph = (newlinks.length - and_count) >= 3 || + ((newlinks.length - and_count >= 1) && + newlinks.length > 2 && + orderStack.length > 1 && + orderStack[orderStack.length - 1] != NODE); + + //PREFIX not null case - put complete sentence in first link. + if (prefix && !starGraph) { + newlinks[1] = prefix + " " + newnodes[0] + + (newlinks[1] !== undefined || newnodes[1] !== undefined ? + " " : "") + + (newlinks[1] !== undefined ? newlinks[1] : "") + + (newnodes[1] !== undefined ? newnodes[1] : ""); + } + + //WRITE COMPLETE SENTENCE + linkindex = 0; + nodeindex = 0; + word = ""; + completeSentence = prefix.length > 0 ? String(prefix) + " " : ""; + for (m = 0; m < orderStack.length; m++) { + if (orderStack[m] === NODE) { + word += " (" + newnodes[nodeindex] + ") "; + completeSentence += newnodes[nodeindex] + " "; + nodeindex++; + } else if (orderStack[m] === LINK) { + word += " -->" + newlinks[nodeindex] + " --> "; + completeSentence += newlinks[nodeindex]; + } + } + completeSentence = completeSentence.trim(); + + //REBUILD GRAPH + linkindex = 0; + nodeindex = 0; + + //CHANGE TO PERMANENT STATE AND UPDATE SUGGESTIONLIST + typesetter = ""; + if (finalize === true) { + typesetter = "perm"; + for (n = 0; n < newnodes.length; n++) { + autoSuggestAddName(newnodes[n]); + } + } else { + typesetter = "temp"; + } + + //ADD SURROUNDING BUBBLE + if (orderStack.length > 0) { + addNode("", "bubble","temp"); + } + + //0-N ORDER STACK + for (m = 0; m < orderStack.length - 1; m++) { + switch (orderStack[m]) { + case START: + if (!typeStack[nodeindex]) { + typeStack[nodeindex] = selectedType(); + } + break; + case NODE: + addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter); + if (!starGraph) { + addLink(newnodes[nodeindex - 1], newnodes[nodeindex], + newlinks[linkindex], typesetter); + } + nodeindex++; + break; + case LINK: + linkindex++; + break; + } + } + + //FINAL N ORDER + switch (orderStack[orderStack.length - 1]) { + case START: + typeStack[nodeindex] = selectedType(); + addNode("new node", typeStack[nodeindex], "temp"); + if (!starGraph) { + addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp"); + and_connect("new node"); + } + ret.state = ANALYSIS_NODE_START; + break; + case NODE: + typeStack[nodeindex] = selectedType(); + addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter); + if (!starGraph) { + addLink(newnodes[nodeindex - 1], newnodes[nodeindex], newlinks[linkindex], typesetter); + and_connect(newnodes[nodeindex]); + } + break; + case LINK: + linkindex++; + addNode("new node", selectedType(), "temp"); + if (!starGraph) { + addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp"); + and_connect("new node"); + } + ret.state = ANALYSIS_LINK; + break; + } + + //EXTERNAL AND CONNECTION CHECKING + function and_connect(node) { + var verb; + for(var x=0;x