Skip to content

Commit 5578a8d

Browse files
feat(*): enable validation of loaded word vectors
1 parent c33d304 commit 5578a8d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/wink-nlp.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,29 @@ var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
413413
validAnnotations.ner = typeof theModel.ner === 'function';
414414
validAnnotations.cer = typeof theModel.metaCER === 'function';
415415

416+
if ( wordVectorsJSON !== null ) {
417+
if ( !helper.isObject( wordVectorsJSON ) )
418+
throw Error( `wink-nlp: invalid word vectors, it must be an object instead found a "${typeof wordVectorsJSON}".` );
419+
420+
let numOfKeys = 0;
421+
const wordVectorKeys = Object.create( null );
422+
wordVectorKeys.precision = true;
423+
wordVectorKeys.l2NormIndex = true;
424+
wordVectorKeys.wordIndex = true;
425+
wordVectorKeys.dimensions = true;
426+
wordVectorKeys.unkVector = true;
427+
wordVectorKeys.size = true;
428+
wordVectorKeys.words = true;
429+
wordVectorKeys.vectors = true;
430+
for ( const key in wordVectorsJSON ) { // eslint-disable-line guard-for-in
431+
numOfKeys += 1;
432+
if ( !wordVectorKeys[ key ] )
433+
throw Error( 'wink-nlp: invalid word vectors format.' );
434+
}
435+
436+
if ( numOfKeys === 0 ) throw Error( 'wink-nlp: empty word vectors found.' );
437+
}
438+
416439
const tempPipe = ( pipe === undefined ) ? Object.keys( validAnnotations ) : pipe;
417440
if ( helper.isArray( tempPipe ) ) {
418441
tempPipe.forEach( ( at ) => {

test/wink-nlp-specs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,17 @@ describe( 'vectorOf method', function () {
647647
expect( myNLP.vectorOf( 'the', false ).length ).to.deep.equal( 102 );
648648
} );
649649
} );
650+
651+
describe( 'Incorrect word vector loading', function () {
652+
it( 'wrong data type should throw error', function () {
653+
expect( winkNLP.bind( null, model, undefined, 3 ) ).to.throw( /^wink-nlp: invalid word vectors, it must/ );
654+
} );
655+
656+
it( 'empty model should throw error', function () {
657+
expect( winkNLP.bind( null, model, undefined, {} ) ).to.throw( /^wink-nlp: empty word vectors found/ );
658+
} );
659+
660+
it( 'incorrect model format should throw error', function () {
661+
expect( winkNLP.bind( null, model, undefined, { hello: 'world' } ) ).to.throw( /^wink-nlp: invalid word vectors format/ );
662+
} );
663+
} );

0 commit comments

Comments
 (0)