UNPKG

kusamoji

Version:

Japanese morphological analyzer for Node.js — Viterbi tokenizer with mmap dict loading and pluggable POS-source strategy

44 lines (36 loc) 1.03 kB
"use strict"; let ViterbiNode = require("./ViterbiNode"); /** * ViterbiLattice is a lattice in Viterbi algorithm * @constructor */ function ViterbiLattice() { this.nodes_end_at = []; this.nodes_end_at[0] = [ new ViterbiNode(-1, 0, 0, 0, "BOS", 0, 0, "") ]; this.eos_pos = 1; } /** * Append node to ViterbiLattice * @param {ViterbiNode} node */ ViterbiLattice.prototype.append = function (node) { let last_pos = node.start_pos + node.length - 1; if (this.eos_pos < last_pos) { this.eos_pos = last_pos; } let prev_nodes = this.nodes_end_at[last_pos]; if (prev_nodes == null) { prev_nodes = []; } prev_nodes.push(node); this.nodes_end_at[last_pos] = prev_nodes; }; /** * Set ends with EOS (End of Statement) */ ViterbiLattice.prototype.appendEos = function () { let last_index = this.nodes_end_at.length; this.eos_pos++; this.nodes_end_at[last_index] = [ new ViterbiNode(-1, 0, this.eos_pos, 0, "EOS", 0, 0, "") ]; }; module.exports = ViterbiLattice;