Skip to content

Commit 3f76c8f

Browse files
feat(*): add error handling for missing/unloaded word vectors
1 parent 5578a8d commit 3f76c8f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/as.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,14 @@ as.markedUpText = function ( twps, rdd, start, end ) {
186186
}; // markedUpText()
187187

188188
as.vector = function ( tokens, rdd ) {
189+
if ( !rdd.wordVectors )
190+
throw Error( 'wink-nlp: word vectors are not loaded, use const nlp = winkNLP( model, pipe, wordVectors ) to load.' );
191+
189192
// Get size of a vector from word vectors
190193
const size = rdd.wordVectors.dimensions;
191194
const precision = rdd.wordVectors.precision;
192195
const vectors = rdd.wordVectors.vectors;
196+
193197
// Set up a new initialized vector of `size`
194198
const v = new Array( size );
195199
v.fill( 0 );

src/wink-nlp.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
457457
methods.as = asHelpers;
458458
// Vector of a token method.
459459
methods.vectorOf = function ( word, safe = true ) {
460+
if ( !wordVectorsJSON )
461+
throw Error( 'wink-nlp: word vectors are not loaded, use const nlp = winkNLP( model, pipe, wordVectors ) to load.' );
462+
460463
const vectors = wordVectorsJSON.vectors;
461464
const unkVector = wordVectorsJSON.unkVector;
462465
const sliceUpTo = wordVectorsJSON.l2NormIndex + 1;

test/wink-nlp-specs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,3 +661,15 @@ describe( 'Incorrect word vector loading', function () {
661661
expect( winkNLP.bind( null, model, undefined, { hello: 'world' } ) ).to.throw( /^wink-nlp: invalid word vectors format/ );
662662
} );
663663
} );
664+
665+
describe( 'word vector methods should throw error if the vectors are not loaded', function () {
666+
const myNLP = winkNLP( model );
667+
const doc = myNLP.readDoc( 'this' );
668+
it( 'should throw error when vectorOf() is called', function () {
669+
expect( myNLP.vectorOf.bind( null, 'the' ) ).to.throw( /^wink-nlp: word vectors are not loaded/ );
670+
} );
671+
672+
it( 'should throw error when as.vector is used', function () {
673+
expect( doc.tokens().out.bind( null, its.value, as.vector ) ).to.throw( /^wink-nlp: word vectors are not loaded/ );
674+
} );
675+
} );

0 commit comments

Comments
 (0)