From c84800f32a902b054295cbc12f906634b3d3d6e6 Mon Sep 17 00:00:00 2001 From: Alexander Bespoyasov Date: Wed, 11 Nov 2020 12:33:36 +0300 Subject: [PATCH 1/5] Extract grammemes searcher options in an object --- lib/MyStem.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/MyStem.js b/lib/MyStem.js index a7e3a64..17e18df 100644 --- a/lib/MyStem.js +++ b/lib/MyStem.js @@ -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,10 @@ MyStem.prototype = { lemmatize: function(word) { var onlyLemma = true; - return this.callMyStem(word, onlyLemma); + return this.callMyStem(word, {onlyLemma}); }, - callMyStem : function (word, onlyLemma) { + 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 +82,16 @@ MyStem.prototype = { resolve: resolve, reject: reject, word: word, - onlyLemma : onlyLemma + onlyLemma: options.onlyLemma, }); }.bind(this)); }, - _getGrammemes: function(data, onlyLemma) { + _getGrammemes: function(data, options = {}) { if (!data[0]) return; if (data[0].analysis.length) { - if ( onlyLemma ) { + if ( options.onlyLemma ) { return data[0].analysis[0].lex; } From af07e4fe88f763f9192e71763709b95cafb78d23 Mon Sep 17 00:00:00 2001 From: Alexander Bespoyasov Date: Wed, 11 Nov 2020 12:34:29 +0300 Subject: [PATCH 2/5] Add a method for accessing the full grammeme analysis --- lib/MyStem.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/MyStem.js b/lib/MyStem.js index 17e18df..5f7dcde 100644 --- a/lib/MyStem.js +++ b/lib/MyStem.js @@ -69,6 +69,11 @@ MyStem.prototype = { return this.callMyStem(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) { @@ -83,6 +88,7 @@ MyStem.prototype = { reject: reject, word: word, onlyLemma: options.onlyLemma, + fullAnalysis: options.fullAnalysis, }); }.bind(this)); }, @@ -91,6 +97,10 @@ MyStem.prototype = { if (!data[0]) return; if (data[0].analysis.length) { + if (options.fullAnalysis) { + return data[0].analysis; + } + if ( options.onlyLemma ) { return data[0].analysis[0].lex; } From e12e35bf37d9950d57467c4aee4299ce30dc664c Mon Sep 17 00:00:00 2001 From: Alexander Bespoyasov Date: Wed, 11 Nov 2020 12:40:09 +0300 Subject: [PATCH 3/5] Add tests for an `analyze` method --- tests/stem.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/stem.js b/tests/stem.js index 3c968e8..a24a25e 100644 --- a/tests/stem.js +++ b/tests/stem.js @@ -76,4 +76,29 @@ 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, [{ 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) { + console.log(analysis) + assert.deepEqual( analysis, 'шоп78шол'); + }).then(function() { + myStem.stop(); + done(); + }); +}) From 9fb5138092e255d3236ecab22383fecb1a98ef06 Mon Sep 17 00:00:00 2001 From: Alexander Bespoyasov Date: Wed, 11 Nov 2020 12:41:42 +0300 Subject: [PATCH 4/5] Remove logs --- tests/stem.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/stem.js b/tests/stem.js index a24a25e..159a84d 100644 --- a/tests/stem.js +++ b/tests/stem.js @@ -95,7 +95,6 @@ test('Extract the full analysis for a non-word', function(done) { myStem.start(); myStem.analyze("шоп78шол").then(function(analysis) { - console.log(analysis) assert.deepEqual( analysis, 'шоп78шол'); }).then(function() { myStem.stop(); From 11fb8f8e7cab96eae1a56633136cacc1fb280981 Mon Sep 17 00:00:00 2001 From: Alexander Bespoyasov Date: Thu, 12 Nov 2020 10:49:38 +0300 Subject: [PATCH 5/5] Return the full grammeme instead of only analysis part from `_getGrammemes` method when `fullAnalysis` option is on --- lib/MyStem.js | 6 +++--- tests/stem.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/MyStem.js b/lib/MyStem.js index 5f7dcde..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 = []; } @@ -98,7 +98,7 @@ MyStem.prototype = { if (data[0].analysis.length) { if (options.fullAnalysis) { - return data[0].analysis; + return data[0]; } if ( options.onlyLemma ) { diff --git a/tests/stem.js b/tests/stem.js index 159a84d..0ae6827 100644 --- a/tests/stem.js +++ b/tests/stem.js @@ -83,7 +83,7 @@ test('Extract the full analysis for a known word', function(done) { myStem.start(); myStem.analyze("немцы").then(function(analysis) { - assert.deepEqual( analysis, [{ lex: 'немец', gr: 'S,m,anim=nom,pl' }]); + assert.deepEqual( analysis, {text: "немцы", analysis: [{ lex: 'немец', gr: 'S,m,anim=nom,pl' }]}); }).then(function() { myStem.stop(); done();