diff --git a/lib/MyStem.js b/lib/MyStem.js index a7e3a64..e44c045 100644 --- a/lib/MyStem.js +++ b/lib/MyStem.js @@ -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 = []; } @@ -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)); @@ -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) { @@ -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; } diff --git a/tests/stem.js b/tests/stem.js index 3c968e8..0ae6827 100644 --- a/tests/stem.js +++ b/tests/stem.js @@ -76,4 +76,28 @@ test('Extract all grammemes unknown word', function(done) { myStem.stop(); done(); }); -}); \ No newline at end of file +}); + +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(); + }); +})