1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
"use strict"
var typeindex = 0;
var nodetypes = ["person", "project", "skill", "deliverable", "objective"];
var suggestionChange=false;
var sentenceStack = [];
var typeStack = [];
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)
{
/* 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.indexOf(request.term) == 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_id, new_id)
{
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)
{
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
return (s1 == s2 ||
s1 == 'new node' ||
s1.substr(0, s2.length) == s2 ||
s2.substr(0, s1.length) == s1);
}
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 (allowed_rename(old_id[0], new_id[1]) &&
allowed_rename(old_id[1], new_id[0])) {
old_id = [old_id[1], old_id[0]];
} else {
if (!allowed_rename(old_id[0], new_id[0]) ||
!allowed_rename(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
*
* 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 textAnalyser2 = function (newtext, finalize) {
var segment = [],
subsegment = [],
sentence = [];
var newlinks = [];
var newnodes = [];
var linkindex = 0;
var nodeindex = 0;
var orderStack = [];
var quoteword = "";
var ANDcase = false;
var ANDcount = 0;
var prefix = "";
var ret = {'nodes': [], 'links': []};
var m;
var word;
var completeSentence;
var typesetter, abnormalGraph;
var verb;
var l, n, j;
var link_hash = {};
var yell_bug = false; // TODO: fix both issues
function addNode(id, type, state) {
ret.nodes.push({'id':id, '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({'sourceId':src, 'targetId':dst, 'name':name ? name.trim() : "", 'state':state});
}
//Sentence Sequencing
//Build the words and cuts the main elements
segment = newtext.split("#");
for (j = 0; j < segment.length; j++) {
if (j !== 0) sentence.push("#");
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) === '"') {
quoteword = "";
do {
quoteword += subsegment[k] + " ";
if(subsegment[k].charAt(subsegment[k].length-1) !== '"')k++;
} while (k < subsegment.length && subsegment[k].charAt(subsegment[k].length - 1) !== '"');
if (subsegment[k] && subsegment[k]!==quoteword.replace(/ /g, "")) {
quoteword += subsegment[k];
}
sentence.push(quoteword.replace(/"/g, ""));
} else {
sentence.push(subsegment[k]);
}
}
}
}
//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 ";
ANDcount++;
//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;
}
}
abnormalGraph = (newlinks.length - ANDcount) >= 3;
//PREFIX not null case - put complete sentence in first link.
if (prefix && !abnormalGraph) {
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] + ") ";
if(newnodes[nodeindex].split(" ").length>1){
completeSentence += '"'+newnodes[nodeindex]+'"' + " ";
}else{
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 (var 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] = nodetypes[typeindex];
}
break;
case "NODE":
addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter);
if (!abnormalGraph) {
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]=nodetypes[typeindex];
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];
addNode(newnodes[nodeindex], typeStack[nodeindex], typesetter);
if (!abnormalGraph) {
addLink(newnodes[nodeindex - 1], newnodes[nodeindex], newlinks[linkindex], typesetter);
ANDconnect(newnodes[nodeindex]);
}
break;
case "LINK":
linkindex++;
addNode("new node", "empty", "temp");
if (!abnormalGraph) {
addLink(newnodes[nodeindex - 1], "new node", newlinks[linkindex], "temp");
ANDconnect("new node");
}
ret.state = ANALYSIS_LINK;
break;
}
//EXTERNAL AND CONNECTION CHECKING
verb = "";
function ANDconnect(node) {
for(var x=0;x<newlinks.length;x++){
if(newlinks[x])if(newlinks[x].replace(/ /g,"")!=="and"){
verb=newlinks[x];
for(var y=0; y<x ;y++){
addLink(newnodes[y], node, verb, typesetter);
for(var z=x; z<newnodes.length ;z++){
addLink(newnodes[y], newnodes[z], verb, typesetter);
}
}
}
}
}
/*console.log(sentence);
console.log(completeSentence);
console.log(orderStack);*/
//STAR CASE
verb = "";
if (abnormalGraph) {
for (l = 1; l < newlinks.length; l++) {
if (newlinks[l])
if (newlinks[l].replace(/ /g, "") !== "and") {
verb = newlinks[l];
}
}
addNode(completeSentence, "chainlink", typesetter);
for (n = 0; n < newnodes.length; n++) {
addLink(newnodes[n], completeSentence, "", typesetter);
}
}
ret.drop_conjugator_links = ANDcount < linkindex;
//FOR THE EXTERNAL ARROW-TYPE CHANGER
lastnode = newnodes[nodeindex];
ret.applyToGraph = function(graph) {
window.ret = ret;
var comp = graph.compareSubset('temp',
ret.nodes.filter(
function(node) {
return !graph.hasNode(node.id, "perm");
}).map(function (node) {
return {id: node.id, name: node.name};
}),
ret.links.map(
function (link) {
return [link.sourceId.toLowerCase(), link.targetId.toLowerCase()];
}));
var k, n, l;
if (comp.graph_same && !finalize) {
if (comp.old_id && comp.new_id) {
up_to_two_renames(graph, comp.old_id, comp.new_name);
}
for (k in ret.links) {
l = ret.links[k];
graph.addLink(l.sourceId, l.targetId, l.name, l.state, ret.drop_conjugator_links);
}
} else {
//REINITIALISE GRAPH (DUMB BUT IT WORKS)
graph.removeNodes("temp");
graph.removeLinks("temp");
for (k in ret.nodes) {
n = ret.nodes[k];
graph.addNode(n.id, n.type, n.state);
}
for (k in ret.links) {
l = ret.links[k];
graph.addLink(l.sourceId, l.targetId, l.name, l.state, ret.drop_conjugator_links);
}
}
//UPDATE GRAPH ONCE
graph.update(!finalize && comp.graph_same);
}
return ret;
}
|