Skip to content

Instantly share code, notes, and snippets.

@ikr7
Created July 31, 2015 11:53
Show Gist options
  • Save ikr7/4c53b16963c81702b339 to your computer and use it in GitHub Desktop.
Save ikr7/4c53b16963c81702b339 to your computer and use it in GitHub Desktop.
使い方は察して
'use strict';
var spawn = require('child_process').spawn;
var MeCab = module.exports = function (mecabPath) {
this.mecabPath = mecabPath || '/usr/local/bin/mecab';
this.process = spawn(this.mecabPath);
};
MeCab.prototype.end = function () {
this.process.kill('SIGINT');
};
MeCab.prototype.parse = function (text, callback) {
this.process.stdout.on('data', function (data) {
callback(
data.toString('utf8').trim().split('\n').map(function (seg) {
seg = seg.split('\t');
var word = seg[0];
if (word == 'EOS') {
return [word];
}
var info = seg[1] || '';
return [word, info.split(',')];
})
);
});
this.process.stdin.write(text + '\n');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment