kuroshiro-analyzer-mecab
Version:
mecab morphological analyzer for kuroshiro
141 lines (121 loc) • 5.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // Check where we are
var _mecabAsync = require("mecab-async");
var _mecabAsync2 = _interopRequireDefault(_mecabAsync);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Mecab Analyzer
*/
var Analyzer = function () {
/**
* Constructor
* @param {string} [command] mecab command. If set, the param `dictPath` is ignored.
* @param {string} [dictPath] Path of the dictionary mecab used.
* @param {Object} [execOptions] The exec options to run mecab command.
* @param {Number} [execOptions.maxBuffer] Largest amount of data in bytes allowed on stdout or stderr. see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback.
* @param {Number} [execOptions.timeout] Timeout. see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback.
*/
function Analyzer() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
command = _ref.command,
dictPath = _ref.dictPath,
execOptions = _ref.execOptions;
_classCallCheck(this, Analyzer);
this._analyzer = null;
this._execOptions = execOptions || {};
if (command) {
this._command = command;
} else if (dictPath) {
this._command = "mecab -d " + dictPath;
} else {
this._command = "mecab";
}
}
/**
* Initialize the analyzer
* @returns {Promise} Promise object represents the result of initialization.
*/
_createClass(Analyzer, [{
key: "init",
value: function init() {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this._analyzer == null) {
_this._analyzer = new _mecabAsync2.default();
_this._analyzer.command = _this._command;
_this._analyzer.options = _this._execOptions;
_this._analyzer.parser = function (data) {
return {
surface_form: data[0],
pos: data[1],
pos_detail_1: data[2],
pos_detail_2: data[3],
pos_detail_3: data[4],
conjugated_type: data[5],
conjugated_form: data[6],
basic_form: data[7],
reading: data[8],
pronunciation: data[9]
};
};
resolve();
} else {
reject(new Error("This analyzer has already been initialized."));
}
});
}
/**
* Parse the given string
* @param {*} str input string.
* @returns {Promise} Promise object represents the result of parsing.
*/
}, {
key: "parse",
value: function parse() {
var _this2 = this;
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var parseToken = function parseToken(token) {
if (token === "") return Promise.resolve([]);
return new Promise(function (resolve, reject) {
_this2._analyzer.parseFormat(token, function (err, result) {
if (err) return reject(err);
resolve(result);
});
});
};
return new Promise(function (resolve, reject) {
var tokens = str.split(" ");
Promise.all(tokens.map(parseToken)).then(function (results) {
var result = [];
for (var i = 0; i < results.length; i++) {
if (i === results.length - 1) {
result = result.concat(results[i]);
} else {
result = result.concat(results[i]);
result.push({
surface_form: " ",
pos: "記号",
pos_detail_1: "空白",
pos_detail_2: "*",
pos_detail_3: "*",
conjugated_type: "*",
conjugated_form: "*",
basic_form: "*"
});
}
}
resolve(result);
}).catch(function (err) {
return reject(err);
});
});
}
}]);
return Analyzer;
}();
exports.default = Analyzer;
module.exports = exports["default"];