Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions lib/MyStem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function MyStem(args) {


this.path = args.path || path.join(__dirname, '..', 'vendor', process.platform, 'mystem');

if ( process.platform === 'win32' ) {
this.path += '.exe'
}

this.handlers = [];
}

Expand All @@ -30,7 +30,12 @@ MyStem.prototype = {

if (handler) {
var data = JSON.parse(line);
handler.resolve( this._getGrammemes(data, handler.onlyLemma) || handler.word );
var options = {
onlyLemma: handler.onlyLemma,
fullAnalysis: handler.fullAnalysis,
};

handler.resolve( this._getGrammemes(data, options) || handler.word );
}
}.bind(this));

Expand Down Expand Up @@ -61,10 +66,15 @@ MyStem.prototype = {

lemmatize: function(word) {
var onlyLemma = true;
return this.callMyStem(word, onlyLemma);
return this.callMyStem(word, {onlyLemma});
},

callMyStem : function (word, onlyLemma) {
analyze: function(word) {
var fullAnalysis = true;
return this.callMyStem(word, {fullAnalysis});
},

callMyStem : function (word, options = {}) {
word = word.replace(/(\S+)\s+.*/, '$1'); // take only first word. TODO
return new Promise(function(resolve, reject) {
if (!this.mystemProcess) {
Expand All @@ -77,16 +87,21 @@ MyStem.prototype = {
resolve: resolve,
reject: reject,
word: word,
onlyLemma : onlyLemma
onlyLemma: options.onlyLemma,
fullAnalysis: options.fullAnalysis,
});
}.bind(this));
},

_getGrammemes: function(data, onlyLemma) {
_getGrammemes: function(data, options = {}) {
if (!data[0]) return;

if (data[0].analysis.length) {
if ( onlyLemma ) {
if (options.fullAnalysis) {
return data[0];
}

if ( options.onlyLemma ) {
return data[0].analysis[0].lex;
}

Expand Down
26 changes: 25 additions & 1 deletion tests/stem.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,28 @@ test('Extract all grammemes unknown word', function(done) {
myStem.stop();
done();
});
});
});

test('Extract the full analysis for a known word', function(done) {
var myStem = new MyStem();
myStem.start();

myStem.analyze("немцы").then(function(analysis) {
assert.deepEqual( analysis, {text: "немцы", analysis: [{ lex: 'немец', gr: 'S,m,anim=nom,pl' }]});
}).then(function() {
myStem.stop();
done();
});
});

test('Extract the full analysis for a non-word', function(done) {
var myStem = new MyStem();
myStem.start();

myStem.analyze("шоп78шол").then(function(analysis) {
assert.deepEqual( analysis, 'шоп78шол');
}).then(function() {
myStem.stop();
done();
});
})