kusamoji
Version:
Japanese morphological analyzer for Node.js — Viterbi tokenizer with mmap dict loading and pluggable POS-source strategy
54 lines (41 loc) • 1.46 kB
JavaScript
;
let ConnectionCosts = require("../ConnectionCosts");
/**
* Builder class for constructing ConnectionCosts object
* @constructor
*/
function ConnectionCostsBuilder() {
this.lines = 0;
this.connection_cost = null;
}
ConnectionCostsBuilder.prototype.putLine = function (line) {
if (this.lines === 0) {
let dimensions = line.split(" ");
let forward_dimension = dimensions[0];
let backward_dimension = dimensions[1];
if (forward_dimension < 0 || backward_dimension < 0) {
throw "Parse error of matrix.def";
}
this.connection_cost = new ConnectionCosts(forward_dimension, backward_dimension);
this.lines++;
return this;
}
let costs = line.split(" ");
if (costs.length !== 3) {
return this;
}
let forward_id = parseInt(costs[0], 10);
let backward_id = parseInt(costs[1], 10);
let cost = parseInt(costs[2], 10);
if (forward_id < 0 || backward_id < 0 || !isFinite(forward_id) || !isFinite(backward_id) ||
this.connection_cost.forward_dimension <= forward_id || this.connection_cost.backward_dimension <= backward_id) {
throw "Parse error of matrix.def";
}
this.connection_cost.put(forward_id, backward_id, cost);
this.lines++;
return this;
};
ConnectionCostsBuilder.prototype.build = function () {
return this.connection_cost;
};
module.exports = ConnectionCostsBuilder;