Skip to content

Commit e26083f

Browse files
refactor(wink-nlp): improve naming and set defaults
Co-authored-by: Rachna <[email protected]>
1 parent 7ffb5b6 commit e26083f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/wink-nlp.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ var tkSize = constants.tkSize;
6161
*
6262
* @param {object} theModel language model.
6363
* @param {string[]} pipe of nlp annotations.
64-
* @param {string} wordVectorsJSON JSON read using node require.
64+
* @param {object} wordEmbeddings object read using node require.
6565
* @returns {object} conatining set of API methods for natural language processing.
6666
* @example
6767
* const nlp = require( 'wink-nlp' );
6868
* var myNLP = nlp();
6969
*/
70-
var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
70+
var nlp = function ( theModel, pipe = null, wordEmbeddings = null ) {
7171

7272
var methods = Object.create( null );
7373
// Token Regex; compiled from `model`
@@ -213,7 +213,7 @@ var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
213213
// The `cache` is also part of document data structure.
214214
rdd.cache = cache;
215215
// Each document gets a pointer to the word vectors.
216-
rdd.wordVectors = wordVectorsJSON;
216+
rdd.wordVectors = wordEmbeddings;
217217
// Document's tokens; each token is represented as an array of numbers:
218218
// ```
219219
// [
@@ -413,9 +413,9 @@ 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}".` );
416+
if ( wordEmbeddings !== null ) {
417+
if ( !helper.isObject( wordEmbeddings ) )
418+
throw Error( `wink-nlp: invalid word vectors, it must be an object instead found a "${typeof wordEmbeddings}".` );
419419

420420
let numOfKeys = 0;
421421
const wordVectorKeys = Object.create( null );
@@ -427,7 +427,7 @@ var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
427427
wordVectorKeys.size = true;
428428
wordVectorKeys.words = true;
429429
wordVectorKeys.vectors = true;
430-
for ( const key in wordVectorsJSON ) { // eslint-disable-line guard-for-in
430+
for ( const key in wordEmbeddings ) { // eslint-disable-line guard-for-in
431431
numOfKeys += 1;
432432
if ( !wordVectorKeys[ key ] )
433433
throw Error( 'wink-nlp: invalid word vectors format.' );
@@ -436,7 +436,7 @@ var nlp = function ( theModel, pipe, wordVectorsJSON = null ) {
436436
if ( numOfKeys === 0 ) throw Error( 'wink-nlp: empty word vectors found.' );
437437
}
438438

439-
const tempPipe = ( pipe === undefined ) ? Object.keys( validAnnotations ) : pipe;
439+
const tempPipe = ( pipe === null || pipe === undefined ) ? Object.keys( validAnnotations ) : pipe;
440440
if ( helper.isArray( tempPipe ) ) {
441441
tempPipe.forEach( ( at ) => {
442442
if ( !validAnnotations[ at ] ) throw Error( `wink-nlp: invalid pipe annotation "${at}" found.` );
@@ -457,12 +457,12 @@ 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 )
460+
if ( !wordEmbeddings )
461461
throw Error( 'wink-nlp: word vectors are not loaded, use const nlp = winkNLP( model, pipe, wordVectors ) to load.' );
462462

463-
const vectors = wordVectorsJSON.vectors;
464-
const unkVector = wordVectorsJSON.unkVector;
465-
const sliceUpTo = wordVectorsJSON.l2NormIndex + 1;
463+
const vectors = wordEmbeddings.vectors;
464+
const unkVector = wordEmbeddings.unkVector;
465+
const sliceUpTo = wordEmbeddings.l2NormIndex + 1;
466466

467467
if ( typeof word !== 'string' ) {
468468
throw Error( 'winkNLP: input word must be of type string.' );

0 commit comments

Comments
 (0)