function growSeq()

in src/models/SentenTreeModel.js [8:83]


function growSeq(seq, terms, minSupport, maxSupport, itemset) {
  /* find the next frequent sequence by inserting a new word to current sequence */
  let pos = -1;
  let word = null;
  let count = 0;
  const len = seq.words.length;
  for (let s = 0; s <= len; s++) {
    const fdist = {};
    seq.DBs.forEach(t => {
      const l = s === 0 ? 0 : t.seqIndices[s - 1] + 1;
      const r = s === len ? t.tokens.length : t.seqIndices[s];
      const duplicate = {};
      for (let i = l; i < r; i++) {
        const w = t.tokens[i];

        if (duplicate[w]) continue;
        duplicate[w] = true;

        if (w in fdist) {
          fdist[w] += t.count;
        } else {
          fdist[w] = t.count;
        }
      }
    });

    let maxw = null;
    let maxc = 0;

    const isNotRoot = len > 0;
    const words = isNotRoot
      ? Object.keys(fdist)
      : Object.keys(fdist).filter(w => !itemset[w].startsWith('#'));

    words.forEach(w => {
      const value = fdist[w];
      if (value < maxSupport && value > maxc) {
        maxw = +w;
        maxc = value;
      }
    });

    if (maxc > count) {
      pos = s;
      word = maxw;
      count = maxc;
    }
  }

  let s0 = null;
  let s1 = null;

  /* split the current group in two */
  if (count >= minSupport) {
    s0 = { size: 0, DBs: [] };
    s1 = { size: 0, DBs: [] };
    const words = seq.words;
    for (let ti = 0; ti < seq.DBs.length; ti ++) {
      const t = seq.DBs[ti];
      const l = pos === 0 ? 0 : t.seqIndices[pos - 1] + 1;
      const r = pos === words.length ? t.tokens.length : t.seqIndices[pos];
      let i = t.tokens.slice(l, r).indexOf(word);
      if (i < 0) {
        s0.DBs.push(t);
        s0.size += t.count;
      } else {
        i += l;
        t.seqIndices.splice(pos, 0, i);
        s1.DBs.push(t);
        s1.size += t.count;
      }
    }
  }

  return { word, pos, count, s0, s1 };
}