Skip to content

Commit

Permalink
test(*): add more model format checks & corresponding tests
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 18, 2021
1 parent bfa6e7c commit a9bf001
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/bm25-vectorizer-specs.js
Expand Up @@ -297,9 +297,14 @@ describe( 'bm25-vectorizer', function () {
} );

it( 'should throw error if invalid model is used', function () {
// Incorrect JSON.
expect( () => v3.loadModel('[]') ).to.throw( 'wink-nlp: invalid model format/version' );
// No fields.
expect( () => v3.loadModel('{}') ).to.throw( 'wink-nlp: invalid model format/version' );
// Incorrect UID.
expect( () => v3.loadModel( JSON.stringify( { uid: 'junk' } ) ) ).to.throw( 'wink-nlp: invalid model format/version' );
// Missing required fields.
expect( () => v3.loadModel( JSON.stringify( { uid: 'WinkNLP-BM25Vectorizer-Model/1.0.0', 0: 0, 1: 1, 2: 2, 3: 3, 4: 4 } ) ) ).to.throw( 'wink-nlp: invalid model format/version' );
} );
} );
} );
11 changes: 11 additions & 0 deletions utilities/bm25-vectorizer.js
Expand Up @@ -316,6 +316,9 @@ var bm25Vectorizer = function ( config ) {
* @return {void} Nothing!
*/
methods.loadModel = function ( json ) {
// Used to check presence of required fields; `uid` is checked separately.
const modelFields = [ 'docId', 'tf', 'idf', 'terms', 'sumOfAllDLs' ];

let model;

if ( docId > 0 ) throw Error( 'wink-nlp: can not load model after learning.' );
Expand All @@ -327,11 +330,19 @@ var bm25Vectorizer = function ( config ) {
}

if ( helper.isObject( model ) && ( Object.keys( model ).length === 6 ) && ( model.uid === 'WinkNLP-BM25Vectorizer-Model/1.0.0' ) ) {
// Check presence of all required fields.
modelFields.forEach( ( f ) => {
if ( model[ f ] === undefined ) throw Error( 'wink-nlp: invalid model format/version' );
} );

// All good, set fields.
docId = model.docId;
tf = model.tf;
idf = model.idf;
terms = model.terms;
sumOfAllDLs = model.sumOfAllDLs;

// To prevent further learning.
weightsComputed = true;
} else {
throw Error( 'wink-nlp: invalid model format/version' );
Expand Down

0 comments on commit a9bf001

Please sign in to comment.