Skip to content

Commit 11ae2b3

Browse files
feat(doc-v2): add error handling
1 parent fb2580d commit 11ae2b3

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/doc-v2.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ var doc = function ( docData, addons ) {
458458
*/
459459
// eslint-disable-next-line complexity
460460
contextualVectors = function ( { lemma = true, specificWordVectors = [], similarWordVectors = false, wordVectorsLimit = 0 } = {} ) {
461+
// Error handling!
462+
if ( !Array.isArray( specificWordVectors ) )
463+
throw Error( `wink-nlp: expecting a valid Javascript array for similarWordVectos, instead found "${typeof specificWordVectors}".`);
464+
if ( !Number.isInteger( wordVectorsLimit ) || wordVectorsLimit >= docData.wordVectors.size )
465+
throw Error( 'wink-nlp: invalid value or type encountered for wordVectorsLimit.' );
461466
// Initialize contextual vectors.
462467
const cv = Object.create( null );
463468
// Following properties are constants, therefore can be directly copied.
@@ -478,13 +483,17 @@ var doc = function ( docData, addons ) {
478483
.out()
479484
.map( ( t ) => t.toLowerCase() );
480485
let docTokensLemma = [];
481-
if ( lemma === true ) docTokensLemma = colTokens( 0, docData.numOfTokens - 1 )()
486+
if ( lemma ) docTokensLemma = colTokens( 0, docData.numOfTokens - 1 )()
482487
.out( its.lemma )
483488
.map( ( t ) => t.toLowerCase() );
484489

485490
for ( let i = 0; i < docTokens.length; i += 1 ) cv.vectors[ docTokens[ i ] ] = awvs[ docTokens[ i ] ] || cv.unkVector;
486491
for ( let i = 0; i < docTokensLemma.length; i += 1 ) cv.vectors[ docTokensLemma[ i ] ] = awvs[ docTokensLemma[ i ] ] || cv.unkVector;
487-
for ( let i = 0; i < specificWordVectors.length; i += 1 ) cv.vectors[ specificWordVectors[ i ] ] = awvs[ specificWordVectors[ i ] ] || cv.unkVector;
492+
for ( let i = 0; i < specificWordVectors.length; i += 1 ) {
493+
const spWord = specificWordVectors[ i ].toString().trim();
494+
if ( spWord )
495+
cv.vectors[ specificWordVectors[ i ] ] = awvs[ specificWordVectors[ i ] ] || cv.unkVector;
496+
}
488497

489498
if ( similarWordVectors ) {
490499
// Extract similar words on the basis of shortest Manhattan distance.

0 commit comments

Comments
 (0)