summaryrefslogtreecommitdiff
path: root/src/client/textanalysis.js
blob: 80d50613736bf623fef2b52702a47f7f3baf685f (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
"use strict";

define(['rz_core', 'model/core', 'model/util', 'model/diff', 'consts', 'util'],
function(rz_core,   model_core,   model_util,   model_diff,   consts,   util) {

var typeindex = 0,
    nodetypes = consts.nodetypes,
    node_name_to_type = {};

var _get_lastnode,
    get_lastnode = function (editgraph, cursor) { return _get_lastnode(editgraph, cursor); };

var sugg_name = {},
    id_to_name_map = {},
    suggestions_bus = new Bacon.Bus(),
    suggestions_options = suggestions_bus.toProperty();

suggestions_bus.push([]);

var ANALYSIS_NODE_START = 'ANALYSIS_NODE_START';
var ANALYSIS_NODE = 'ANALYSIS_NODE'
var ANALYSIS_LINK = 'ANALYSIS_LINK';

var NEW_NODE_NAME = consts.NEW_NODE_NAME;

function selectedType()
{
    return nodetypes[typeindex];
}

/**
 * Tokenizer for input.
 *
 * node_token is it's own token, represented by itself.
 *
 * accepts a quotation char which allows whitespace in between.
 *
 * treats '\\' as a quote for the next char.
 *
 * TODO:  should only apply if cursor is actually on token,
 *
 * so need tokenise to be fixed to split according to actual tokens,
 * i.e.:
 * #"one two" four five #six
 * is exactly 3 tokens:
 * #"one two"
 * four five
 * #six
 * or 5 if you assign a token for the '#' char:
 * #
 * "one two"
 * four five
 * #
 * six
 */
function tokenize(text, node_token, quote)
{
    var c,
        i,
        tokens = [],
        token = [],
        inquote = false,
        prev = null,
        prev_whitespace = true,
        start = 0,
        next = function() {
            if (token.length > 0) {
                tokens.push({start: start, end: i, token: token.join('')});
                token = [];
                start = i;
            }
        };
    for (i = 0 ; i < text.length; ++i) {
        c = text[i];
        if (prev == '\\') {
            token.push(c);
            prev = null;
            continue;
        }
        switch (c) {
        case ' ':
        case '\t':
            if (inquote) {
                token.push(c);
            } else {
                next();
            }
            break;
        case quote:
            inquote = !inquote;
            break;
        default:
            if (c == node_token && prev_whitespace) {
                tokens.push({start: i, end: i + 1, token: node_token});
                start = i + 1;
            } else {
                token.push(c);
            }
        }
        prev = c;
        prev_whitespace = prev === null || prev === ' ' || prev === '\t';
    }
    next();
    return tokens;
}


/*
 * id - undefined | node id 
 *
 */
function auto_suggest__update_name(name, id)
{
    if (id !== undefined && id_to_name_map[id] !== undefined) {
        delete sugg_name[id_to_name_map[id]];
        id_to_name_map[id] = name;
    }
    /* note that name can contain spaces - this is ok. We might want to limit this though? */
    sugg_name[name] = 1;
    suggestions_bus.push(sugg_name);
}

function auto_suggest_remove_name(name, id)
{
    if (name !== undefined) {
        delete sugg_name[name];
    }
    if (id !== undefined) {
        delete sugg_name[id_to_name_map[id]];
        delete id_to_name_map[id];
    }
    suggestions_bus.push(sugg_name);
}

function auto_suggest__update_from_graph()
{
    sugg_name = {};
    id_to_name_map = {};
    rz_core.main_graph.nodes().forEach(function (node) {
        auto_suggest__update_name(node.name.toLowerCase(), node.id);
    });
    suggestions_bus.push(sugg_name);
}

/*
 * 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 than two nodes (two '#'
 *  marks).
 *
 */
var textAnalyser = function (spec) {
    var newtext = spec.sentence,
        finalize = spec.finalize,

        tokens,
        sentence,
        token_set_new_node_names = [], // token set representing new node names
        token_set_new_link_names = [], // token set representing new link names
        linkindex = 0,
        nodeindex = 0,
        orderStack = [],
        and_count = 0,
        prefix = "",
        m,
        word,
        completeSentence,
        completeSentenceParts,
        starGraph,
        n,
        link_hash = {},
        yell_bug = false, // TODO: fix both issues
        NODE = "NODE",
        LINK = "LINK",
        START = "START",
        node_by_name = {},
        nodes = [],
        links = [],
        ret = model_diff.new_topo_diff();

    util.assert(spec.sentence !== undefined &&
                spec.finalize !== undefined &&
                "bad input");

    function __addNode(name) {
        var type;
        if (name == NEW_NODE_NAME) {
            type = selectedType()
        } else {
            type = node_name_to_type[name] || nodetypes[0];
        }
        var node = {
                    'name': name,
                    'type': type,
                   };
        node_name_to_type[name] = type;
        nodes.push(node);
    }

    function __addLink(src_name, dst_name, name) {
        if (!src_name || !dst_name) {
            if (yell_bug) {
                console.log('bug - adding link (' + src_name + ', ' + dst_name + ')');
            }
            return;
        }
        name = name || 'is';
        if (link_hash[src_name] && link_hash[src_name][dst_name]) {
            if (yell_bug) {
                console.log('bug - adding link twice (' + src_name + ', ' + dst_name + ')');
            }
            return;
        }
        if (!link_hash[src_name]) {
            link_hash[src_name] = {};
        }
        link_hash[src_name][dst_name] = 1;

        var link = {
            src_name: src_name,
            dst_name: dst_name,
            name: name,
        };
        links.push(link);
    }

    //Sentence Sequencing
    //Build the words and cuts the main elements
    tokens = tokenize(newtext, '#', '"');
    sentence = tokens.map(function (d) { return d.token; });

    // build new node,link arrays in order of appearance
    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);
                token_set_new_node_names.push(sentence[m]);
                linkindex++;
            } else if (orderStack[orderStack.length - 1] === NODE) {
                orderStack.push(LINK);
                if (!token_set_new_link_names[linkindex]) {
                    token_set_new_link_names[linkindex] = sentence[m];
                } else {
                    token_set_new_link_names[linkindex] += " " + sentence[m];
                }
            } else {
                if (!token_set_new_link_names[linkindex]) {
                    token_set_new_link_names[linkindex] = sentence[m];
                } else {
                    token_set_new_link_names[linkindex] += " " + sentence[m];
                }
            }
            if (token_set_new_node_names.length === 0) {
                prefix += (prefix.length > 0 ? ' ' : '') + sentence[m];
            }
            break;
        }
    }

    starGraph = (token_set_new_link_names.length - and_count) >= 3  ||
        ((token_set_new_link_names.length - and_count >= 1) &&
         token_set_new_link_names.length > 2 &&
         orderStack.length > 1 &&
         orderStack[orderStack.length - 1] != NODE);

    //PREFIX not null case - put complete sentence in first link.
    if (prefix && !starGraph) {
        token_set_new_link_names[1] = prefix + " " + token_set_new_node_names[0] +
        (token_set_new_link_names[1] !== undefined || token_set_new_node_names[1] !== undefined ?
        " " : "")
        + (token_set_new_link_names[1] !== undefined ? token_set_new_link_names[1] : "")
        + (token_set_new_node_names[1] !== undefined ? token_set_new_node_names[1] : "");
    }

    //WRITE COMPLETE SENTENCE
    linkindex = 0;
    nodeindex = 0;
    word = "";
    completeSentenceParts = prefix.length > 0 ? [String(prefix)] : [];
    for (m = 0; m < orderStack.length; m++) {
        if (orderStack[m] === NODE) {
            word += " (" + token_set_new_node_names[nodeindex] + ") ";
            completeSentenceParts.push(token_set_new_node_names[nodeindex]);
            nodeindex++;
        } else if (orderStack[m] === LINK) {
            word += " -->" + token_set_new_link_names[nodeindex] + " --> ";
            completeSentenceParts.push(token_set_new_link_names[nodeindex]);
        }
    }
    completeSentence = completeSentenceParts.join(" ").trim();

    //REBUILD GRAPH
    linkindex = 0;
    nodeindex = 0;

    //0-N ORDER STACK
    for (m = 0; m < orderStack.length - 1; m++) {
        switch (orderStack[m]) {
            case START:
                break;
            case NODE:
                __addNode(token_set_new_node_names[nodeindex]);
                if (!starGraph && nodeindex > 0 && token_set_new_link_names[linkindex] !== undefined) {
                    __addLink(token_set_new_node_names[nodeindex - 1],
                              token_set_new_node_names[nodeindex],
                              token_set_new_link_names[linkindex]);
                }
                nodeindex++;
                break;
            case LINK:
                linkindex++;
                break;
        }
    }

    //FINAL N ORDER
    switch (orderStack[orderStack.length - 1]) {
        case START:
            __addNode(NEW_NODE_NAME);
            if (!starGraph && nodeindex > 0) {
                __addLink(token_set_new_node_names[nodeindex - 1], NEW_NODE_NAME,
                          token_set_new_link_names[linkindex], "temp");
                and_connect(NEW_NODE_NAME);
            }
            ret.state = ANALYSIS_NODE_START;
            break;
        case NODE:
            __addNode(token_set_new_node_names[nodeindex]);
            if (!starGraph && nodeindex > 0 && token_set_new_link_names[linkindex] !== undefined) {
                __addLink(token_set_new_node_names[nodeindex - 1],
                          token_set_new_node_names[nodeindex],
                          token_set_new_link_names[linkindex]);
                and_connect(token_set_new_node_names[nodeindex]);
            }
            ret.state = ANALYSIS_NODE_START;
            break;
        case LINK:
            linkindex++;
            __addNode(NEW_NODE_NAME, selectedType(), "temp");
            if (!starGraph) {
                __addLink(token_set_new_node_names[nodeindex - 1], NEW_NODE_NAME, token_set_new_link_names[linkindex], "temp");
                and_connect(NEW_NODE_NAME);
            }
            ret.state = ANALYSIS_LINK;
            break;
    }

    //EXTERNAL AND CONNECTION CHECKING
    function and_connect(node) {
        var verb;
        for(var x=0;x<token_set_new_link_names.length;x++){
            if(token_set_new_link_names[x])if(token_set_new_link_names[x].replace(/ /g,"")!=="and"){
                verb = token_set_new_link_names[x];
                for(var y=0; y<x ;y++){
                    __addLink(token_set_new_node_names[y], node, verb);
                    for(var z=x; z<token_set_new_node_names.length ;z++){
                        __addLink(token_set_new_node_names[y],
                                  token_set_new_node_names[z], verb);
                    }
                }
            }
        }
    }

    //STAR CASE
    if (starGraph) {
        __addNode(completeSentence, "chainlink");
        for (n = 0; n < token_set_new_node_names.length; n++) {
            __addLink(token_set_new_node_names[n], completeSentence, "chained");
        }
    }

    ret.drop_conjugator_links = true; // leaving since we might change behavior again

    ret.applyToGraph = function(spec) {
        var edit_graph = spec.edit_graph,
            backend_commit = spec.backend_commit,
            main_graph = edit_graph.base,
            existing_nodes = [];

        util.assert(edit_graph !== undefined &&
                    main_graph !== undefined &&
                    backend_commit !== undefined, "missing inputs");
        window.ret = ret;

        ret.node_set_add = nodes
            .filter(function (node_spec) {
                var main_node = main_graph.find_node__by_name(node_spec.name);
                if (main_node) {
                    existing_nodes.push(main_node);
                }
                return main_node === null;
            })
            .map(function (node_spec) {
                return model_core.create_node__set_random_id(node_spec);
            });
        // fill in hash to be used for link creation, new and existing nodes
        ret.node_set_add.forEach(function (node) {
            node_by_name[node.name] = node;
        });
        existing_nodes.forEach(function (node) {
            node_by_name[node.name] = node;
        });

        ret.link_set_add = links
            .filter(function (link) {
                return !finalize ||
                       !ret.drop_conjugator_links ||
                       (link.name.replace(/ /g,"") !== "and");
                })
            .map(function (link_spec) {
                var src = node_by_name[link_spec.src_name],
                    dst = node_by_name[link_spec.dst_name],
                    link = model_core.create_link__set_random_id(src, dst, {
                        name: link_spec.name,
                        state: 'perm', // FIXME: this is meaningless now with graph separation
                    });
                link.__src_id = src.id;
                link.__dst_id = dst.id;
                return link;
            });

        // REINITIALISE GRAPH (DUMB BUT IT WORKS)
        /* don't push diff, avoid bubble on main_graph going to zero */
        edit_graph.clear(finalize);

        if (!finalize) {
            main_graph.markRelated(token_set_new_node_names);
        } else {
            main_graph.removeRelated();
        }

        if (finalize && backend_commit) {
            main_graph.commit_and_tx_diff__topo(ret);
        } else {
            edit_graph.commit_diff__topo(ret);
        }
    };

    function lookup_node_in_bounds(edit_graph, cursor) {
        var i, d, d_next, j, name, node;

        if (nodes.length <= 0) {
            return null;
        }
        if (nodes.length == 1) {
            return edit_graph.find_node__by_name(nodes[0].name);
        }
        // go forward to find cursor location in tokens
        for (i = 0 ; i < tokens.length; ++i) {
            d = tokens[i];
            d_next = tokens[i + 1];
            if (cursor >= d.start && (d_next === undefined || cursor < d_next.end)) {
                break;
            }
        }
        i = Math.min(tokens.length - 1, i);
        // go forward if on a token
        for (; tokens[i] !== undefined && tokens[i].token === '#'; ++i) {}
        // go back to find token
        for (j = i; j >= 0 && tokens[j] === undefined || tokens[j].token !== '#'; --j) {}
        name = tokens[j + 1] ? tokens[j + 1].token : NEW_NODE_NAME;
        node = edit_graph.find_node__by_name(name);
        util.assert(node !== undefined, "can't find node");
        return node;
    }

    _get_lastnode = finalize || tokens.length == 0 ? function () { return null }
                                                  : lookup_node_in_bounds;

    return ret;
};

function init(main_graph)
{
    main_graph.diffBus
        .onValue(auto_suggest__update_from_graph);
}

return {
    init:init,
    textAnalyser:textAnalyser,
    suggestions_options: suggestions_options,
    ANALYSIS_NODE_START:ANALYSIS_NODE_START,
    ANALYSIS_NODE: ANALYSIS_NODE,
    ANALYSIS_LINK:ANALYSIS_LINK,

    //for the external arrow-type changer
    lastnode: get_lastnode,
    set_type: function(name, nodetype) {
        node_name_to_type[name] = nodetype;
    },

    selected_type_next: function() {
        typeindex = (typeindex + 1) % 5;
        return selectedType();
    },
    selected_type_prev: function() {
        typeindex = (typeindex + 4) % 5;
        return selectedType();
    }
};
});