summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Levy <alon@pobox.com>2014-08-20 08:34:30 +0300
committerAlon Levy <alon@pobox.com>2014-08-20 08:34:30 +0300
commitc346cb74078ea9ef8280764e35a6d8c1bfa3ceca (patch)
treeb7e6423dab8e313507a5fac1fdb96f70bdfdc188
parentb7723546a8b9b07c724e4be062c0c0c58501478f (diff)
textanalysis & textanalysis-ui split complete
-rw-r--r--scripts/textanalysis-ui.js25
-rw-r--r--scripts/textanalysis.js58
2 files changed, 50 insertions, 33 deletions
diff --git a/scripts/textanalysis-ui.js b/scripts/textanalysis-ui.js
index f5d2d5fe..92407d69 100644
--- a/scripts/textanalysis-ui.js
+++ b/scripts/textanalysis-ui.js
@@ -10,11 +10,11 @@ $('#textanalyser').autocompleteTrigger({
// TODO - hide this in a scope
function analyzeSentence(sentence, finalize)
{
- var d = TextAnalyser2(graph, sentence, finalize);
- for (var k in d.sugg) {
+ var ret = TextAnalyser2(sentence, finalize);
+ for (var k in ret.sugg) {
sugg.push(k);
}
- switch (d.state) {
+ switch (ret.state) {
case ANALYSIS_NODE_START:
$('.typeselection').css({top:window.innerHeight/2-115,left:window.innerWidth/2-325});
$('.typeselection').html('<table><tr><td style="height:28px"></td></tr><tr><td>Use [TAB] key to pick a type</td></tr></table>');
@@ -24,6 +24,25 @@ function analyzeSentence(sentence, finalize)
$('.typeselection').css('left', 0);
break;
}
+ //REINITIALISE GRAPH (DUMB BUT IT WORKS)
+ graph.removeNodes("temp");
+ graph.removeLinks("temp");
+ for (var k in ret.nodes) {
+ var n = ret.nodes[k];
+ graph.addNode(n.id, n.type, n.state);
+ }
+ for (var k in ret.links) {
+ var l = ret.links[k];
+ graph.addLink(l.sourceId, l.targetId, l.name, l.state);
+ }
+
+ //UPDATE GRAPH ONCE
+ graph.update();
+
+ if (finalize) {
+ $('.typeselection').css('top', -300);
+ $('.typeselection').css('left', 0);
+ }
}
diff --git a/scripts/textanalysis.js b/scripts/textanalysis.js
index 5c69edb9..99c15861 100644
--- a/scripts/textanalysis.js
+++ b/scripts/textanalysis.js
@@ -13,7 +13,7 @@ var lastnode;
var ANALYSIS_NODE_START = 'ANALYSIS_NODE_START';
var ANALYSIS_LINK = 'ANALYSIS_LINK';
-function TextAnalyser2(graph, newtext, finalize) {
+function TextAnalyser2(newtext, finalize) {
var segment = [],
subsegment = [],
sentence = [];
@@ -26,7 +26,14 @@ function TextAnalyser2(graph, newtext, finalize) {
var ANDcase = false;
var ANDcount = 0;
var prefix = "";
- var ret = {};
+ var ret = {'nodes': [], 'links': []};
+
+ function addNode(id, type, state) {
+ ret.nodes.push({'id':id, 'type':type, 'state':state});
+ }
+ function addLink(src, dst, name, state) {
+ ret.links.push({'sourceId':src, 'targetId':dst, 'name':name, 'state':state});
+ }
//Sentence Sequencing
//Build the words and cuts the main elements
@@ -140,13 +147,9 @@ function TextAnalyser2(graph, newtext, finalize) {
typesetter = "temp";
}
- //REINITIALISE GRAPH (DUMB BUT IT WORKS)
- graph.removeNodes("temp");
- graph.removeLinks("temp");
-
//ADD SURROUNDING BUBBLE
if (orderStack.length > 0) {
- graph.addNode("", "bubble", "temp");
+ addNode("", "bubble","temp");
}
var abnormalGraph = (newlinks.length - ANDcount) >= 3;
@@ -160,8 +163,10 @@ function TextAnalyser2(graph, newtext, finalize) {
}
break;
case "NODE":
- graph.addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter);
- if (!abnormalGraph) graph.addLink(newnodes[nodeindex - 1], newnodes[nodeindex], newlinks[linkindex], typesetter);
+ addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter);
+ if (!abnormalGraph) {
+ addLink(newnodes[nodeindex - 1], newnodes[nodeindex], newlinks[linkindex], typesetter);
+ }
nodeindex++;
break;
case "LINK":
@@ -174,25 +179,28 @@ function TextAnalyser2(graph, newtext, finalize) {
switch (orderStack[orderStack.length - 1]) {
case "START":
typeStack[nodeindex]=nodetypes[typeindex];
- graph.addNode("new node", typeStack[nodeindex], "temp");
- if (!abnormalGraph){graph.addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp");
+ addNode("new node", typeStack[nodeindex], "temp");
+ if (!abnormalGraph) {
+ addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp");
ANDconnect("new node");}
ret.state = ANALYSIS_NODE_START;
break;
case "NODE":
typeStack[nodeindex]=nodetypes[typeindex];
- graph.addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter);
+ addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter);
if (!abnormalGraph) {
- graph.addLink(newnodes[nodeindex - 1], newnodes[nodeindex], newlinks[linkindex], typesetter);
+ addLink(newnodes[nodeindex - 1], newnodes[nodeindex], newlinks[linkindex], typesetter);
ANDconnect(newnodes[nodeindex]);
}
break;
case "LINK":
linkindex++;
- graph.addNode("new node", "empty", "temp");
- if (!abnormalGraph){ graph.addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp");
- ANDconnect("new node");}
+ addNode("new node", "empty", "temp");
+ if (!abnormalGraph) {
+ addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp");
+ ANDconnect("new node");
+ }
ret.state = ANALYSIS_LINK;
break;
}
@@ -204,16 +212,14 @@ function TextAnalyser2(graph, newtext, finalize) {
if(newlinks[x])if(newlinks[x].replace(/ /g,"")!=="and"){
verb=newlinks[x];
for(var y=0; y<x ;y++){
- graph.addLink(newnodes[y],node,verb,typesetter);
+ addLink(newnodes[y], node, verb, typesetter);
for(var z=x; z<newnodes.length ;z++){
- graph.addLink(newnodes[y],newnodes[z],verb,typesetter);
+ addLink(newnodes[y], newnodes[z], verb, typesetter);
}
}
}
}
-
-
}
/*console.log(sentence);
@@ -229,23 +235,15 @@ function TextAnalyser2(graph, newtext, finalize) {
verb = newlinks[l];
}
}
- graph.addNode(completeSentence, "chainlink", typesetter);
+ addNode(completeSentence, "chainlink", typesetter);
for (var n = 0; n < newnodes.length; n++) {
- graph.addLink(newnodes[n], completeSentence, "", typesetter);
+ addLink(newnodes[n], completeSentence, "", typesetter);
}
}
//FOR THE EXTERNAL ARROW-TYPE CHANGER
lastnode = newnodes[nodeindex];
- if(finalize){
- $('.typeselection').css('top', -300);
- $('.typeselection').css('left', 0);
- }
-
- //UPDATE GRAPH ONCE
- graph.update();
-
return ret;
}