Created
July 31, 2015 11:53
-
-
Save ikr7/4c53b16963c81702b339 to your computer and use it in GitHub Desktop.
使い方は察して
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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