Skip to content

Commit

Permalink
feat(*): add error handling for missing/unloaded word vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayaksaxena committed Mar 21, 2024
1 parent 5578a8d commit 3f76c8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/as.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ as.markedUpText = function ( twps, rdd, start, end ) {
}; // markedUpText()

as.vector = function ( tokens, rdd ) {
if ( !rdd.wordVectors )
throw Error( 'wink-nlp: word vectors are not loaded, use const nlp = winkNLP( model, pipe, wordVectors ) to load.' );

// Get size of a vector from word vectors
const size = rdd.wordVectors.dimensions;
const precision = rdd.wordVectors.precision;
const vectors = rdd.wordVectors.vectors;

// Set up a new initialized vector of `size`
const v = new Array( size );
v.fill( 0 );
Expand Down
3 changes: 3 additions & 0 deletions src/wink-nlp.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
methods.as = asHelpers;
// Vector of a token method.
methods.vectorOf = function ( word, safe = true ) {
if ( !wordVectorsJSON )
throw Error( 'wink-nlp: word vectors are not loaded, use const nlp = winkNLP( model, pipe, wordVectors ) to load.' );

const vectors = wordVectorsJSON.vectors;
const unkVector = wordVectorsJSON.unkVector;
const sliceUpTo = wordVectorsJSON.l2NormIndex + 1;
Expand Down
12 changes: 12 additions & 0 deletions test/wink-nlp-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,15 @@ describe( 'Incorrect word vector loading', function () {
expect( winkNLP.bind( null, model, undefined, { hello: 'world' } ) ).to.throw( /^wink-nlp: invalid word vectors format/ );
} );
} );

describe( 'word vector methods should throw error if the vectors are not loaded', function () {
const myNLP = winkNLP( model );
const doc = myNLP.readDoc( 'this' );
it( 'should throw error when vectorOf() is called', function () {
expect( myNLP.vectorOf.bind( null, 'the' ) ).to.throw( /^wink-nlp: word vectors are not loaded/ );
} );

it( 'should throw error when as.vector is used', function () {
expect( doc.tokens().out.bind( null, its.value, as.vector ) ).to.throw( /^wink-nlp: word vectors are not loaded/ );
} );
} );

0 comments on commit 3f76c8f

Please sign in to comment.