Skip to content

Commit

Permalink
feat(*): throw error in computeWeights if there is no learning
Browse files Browse the repository at this point in the history
references #63

Co-authored-by: Rachna <rachna@graype.in>
  • Loading branch information
sanjayaksaxena and rachnachakraborty committed Nov 14, 2021
1 parent ad561e4 commit 38d54e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
51 changes: 11 additions & 40 deletions test/bm25-vectorizer-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,59 +67,24 @@ describe( 'bm25-vectorizer', function () {
} );
} );

describe( 'learn from 0-documents', function () {
describe( 'out() should throw error without any learnings, whereas length() should work', function () {
const v = bm25();
it( '.out() should return []', function () {
expect( v.out() ).to.deep.equal( [] );
it( 'should throw error without any learnning on out()', function () {
expect( v.out.bind() ).to.throw( 'wink-nlp: this operation doesn\'t make sense without any learning; use learn() API first.' );
} );

it( '.out( its.docTermMatrix ) should return []', function () {
expect( v.out( its.docTermMatrix ) ).to.deep.equal( [] );
} );

it( '.out( its.docBOWArray ) should return []', function () {
expect( v.out( its.docBOWArray ) ).to.deep.equal( [] );
} );

it( '.out( its.terms ) should return []', function () {
expect( v.out( its.terms ) ).to.deep.equal( [] );
} );

it( '.out( its.idf ) should return []', function () {
expect( v.out( its.idf ) ).to.deep.equal( [] );
} );

it( '.out( its.modelJSON ) should return []', function () {
expect( v.out( its.modelJSON ) ).to.deep.equal( '{"uid":"WinkNLP-BM25Vectorizer-Model/1.0.0","tf":[],"idf":{},"terms":[],"docId":0,"sumOfAllDLs":0}' );
it( 'doc.out() should return undefined', function () {
expect( v.doc( 0 ).out.bind() ).to.throw( 'wink-nlp: this operation doesn\'t make sense without any learning; use learn() API first.' );
} );

it( '.length() should return []', function () {
expect( v.length() ).to.equal( 0 );
} );

it( 'doc.out() should return undefined', function () {
expect( v.doc( 0 ).out() ).to.deep.equal( undefined );
} );

it( 'doc.out( its.tf ) should return []', function () {
expect( v.doc( 0 ).out( its.tf ) ).to.deep.equal( [] );
} );

it( 'doc.out( its.vector ) should return []', function () {
expect( v.doc( 0 ).out( its.vector ) ).to.deep.equal( [] );
} );

it( 'doc.out( its.bow ) should return []', function () {
expect( v.doc( 0 ).out( its.bow ) ).to.deep.equal( undefined );
} );

it( 'doc.length() should return []', function () {
expect( v.doc( 0 ).length() ).to.equal( 0 );
} );

// it( 'should throw error if readDoc is given non-text', function () {
// expect( nlp.readDoc.bind( 1 ) ).to.throw( /^wink-nlp: expecting a valid Javascript string/ );
// } );
} );

describe( 'learn from 1-document', function () {
Expand Down Expand Up @@ -168,6 +133,12 @@ describe( 'bm25-vectorizer', function () {
expect( v.doc( 0 ).out( its.tf ) ).to.deep.equal( [ [ 'rain', 0.395562849 ], [ 'away', 0.287682072 ], [ 'go', 0.287682072 ] ] );
} );

it( 'doc.out() should return freq table of terms', function () {
// To test its.bow — default fall back.
expect( v.doc( 0 ).out() ).to.deep.equal( { rain: 0.395562849, away: 0.287682072, go: 0.287682072 } );
} );


it( 'doc.out( its.vector ) should return its vector', function () {
expect( v.doc( 0 ).out( its.vector ) ).to.deep.equal( [ 0.287682072, 0.287682072, 0.395562849 ] );
} );
Expand Down
1 change: 1 addition & 0 deletions utilities/bm25-vectorizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var bm25Vectorizer = function ( config ) {
var computeWeights = function () {
// If weights have been computed, then re-computation is not allowed.
if ( weightsComputed ) return;
if ( docId === 0 ) throw Error( 'wink-nlp: this operation doesn\'t make sense without any learning; use learn() API first.' );
// Set the average document length used for normalization.
const avgDL = sumOfAllDLs / docId;
// Compute IDF.
Expand Down

0 comments on commit 38d54e9

Please sign in to comment.