Skip to content

Commit 54f2df1

Browse files
fix(sentence-wise-importance): never return an empty array
This was result of refactoring code from observable where return was used inside function which got propogated to a for loop here!
1 parent 0934dd6 commit 54f2df1

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/sentence-wise-importance.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const sentenceWiseImportance = function ( rdd ) {
7979
}
8080

8181
// Ignore sentences where we cannot build NGram i.e. sentences shorter than NGram.
82-
if ( pos.length < 4 ) return [];
82+
if ( pos.length < 4 ) continue; // eslint-disable-line no-continue
8383
// Determine NGrams;
8484
for ( let k = 0; k + NGram - 1 < pos.length; k += 1 ) {
8585
const pos4Gram = pos.slice( k, k + NGram );
@@ -114,7 +114,9 @@ const sentenceWiseImportance = function ( rdd ) {
114114
} );
115115
});
116116
// Normalize weights by dividing them by the max.
117-
const max = Math.max( ...sentenceWiseWeights );
117+
let max = Math.max( ...sentenceWiseWeights );
118+
// Avoid divide by zero situation
119+
if ( max === 0 ) max = 1;
118120

119121
return sentenceWiseWeights.map( ( e, i ) => ( { index: i, importance: +( e / max ).toFixed( 4 ) } ) );
120122
};

test/its-specs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ describe( 'its functions for .out()', function () {
221221
];
222222

223223
expect( nlp.readDoc( text ).out( its.sentenceWiseImportance ) ).to.deep.equal( rank );
224-
expect( nlp.readDoc( 'text' ).out( its.sentenceWiseImportance ) ).to.deep.equal( [] );
224+
expect( nlp.readDoc( 'text' ).out( its.sentenceWiseImportance ) ).to.deep.equal( [ { index: 0, importance: 0 } ] );
225+
expect( nlp.readDoc( '' ).out( its.sentenceWiseImportance ) ).to.deep.equal( [ { index: 0, importance: 0 } ] );
225226
} );
226227

227228
it( 'selected entity with its.detail, its.span as.?', function () {

0 commit comments

Comments
 (0)