diff --git a/src/doc-v2.js b/src/doc-v2.js index 5e3d2be..bc5bf70 100644 --- a/src/doc-v2.js +++ b/src/doc-v2.js @@ -459,10 +459,14 @@ var doc = function ( docData, addons ) { // eslint-disable-next-line complexity contextualVectors = function ( { lemma = true, specificWordVectors = [], similarWordVectors = false, wordVectorsLimit = 0 } = {} ) { // Error handling! + if ( docData.wordVectors === null ) + throw Error( 'wink-nlp: word vectors are not loaded: load them winkNLP\'s instantiation time.' ); if ( !Array.isArray( specificWordVectors ) ) throw Error( `wink-nlp: expecting a valid Javascript array for similarWordVectos, instead found "${typeof specificWordVectors}".`); if ( !Number.isInteger( wordVectorsLimit ) || wordVectorsLimit >= docData.wordVectors.size ) throw Error( 'wink-nlp: invalid value or type encountered for wordVectorsLimit.' ); + if ( lemma && !docData.currPipe.pos ) + throw Error( 'wink-nlp: Can\'t create lemma vectors without pos: add a "pos" to NLP pipe.' ); // Initialize contextual vectors. const cv = Object.create( null ); // Following properties are constants, therefore can be directly copied. diff --git a/test/contextual-vectors-specs.js b/test/contextual-vectors-specs.js index 1581ba3..d5d1b9b 100644 --- a/test/contextual-vectors-specs.js +++ b/test/contextual-vectors-specs.js @@ -161,4 +161,18 @@ describe( 'contextual vectors', function () { expect( doc1.contextualVectors.bind( null, { specificWordVectors: {} } ) ).to.throw( /^wink-nlp: expecting a valid Javascript/ ); expect( doc1.contextualVectors.bind( null, { specificWordVectors: 'a' } ) ).to.throw( /^wink-nlp: expecting a valid Javascript/ ); } ); + + it( 'missing word vectors should throw error', function () { + const nlpWithNoVW = winkNLP( model ); + const textNoVW = 'It is'; + const docNoVW = nlpWithNoVW.readDoc( textNoVW ); + expect( () => docNoVW.contextualVectors() ).to.throw( /^wink-nlp: word vectors are not loaded/ ); + } ); + + it( 'lemma: true and missing pos in pipe should throw error', function () { + const nlpMissingPOS = winkNLP( model, [ 'sbd' ], vectors ); + const textMissingPOS = 'It is'; + const docMissingPOS = nlpMissingPOS.readDoc( textMissingPOS ); + expect( () => docMissingPOS.contextualVectors() ).to.throw( /^wink-nlp: Can\'t create lemma vectors/ ); + } ); } ); diff --git a/types/index.d.ts b/types/index.d.ts index 31fa2cf..62ec5e9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -271,6 +271,13 @@ declare module 'wink-nlp' { out(itsf: ItsFunction, asf: AsFunction): U | T[] | string[]; } + export interface Include { + lemma?: boolean; + specificWordVectors?: string[]; + similarWordVectors?: boolean; + wordVectorsLimit?: number; + } + export interface Document { entities(): Entities; customEntities(): CustomEntities; @@ -282,7 +289,7 @@ declare module 'wink-nlp' { tokens(): Tokens; printTokens(): void; pipeConfig(): string[]; - contextualVectors(lemma: boolean, specifcWordVectors: string[], similarWordVectors: boolean, wordVectorsLimit: number): string; + contextualVectors(include: Include): string; } export interface CerExample { @@ -317,6 +324,7 @@ declare module 'wink-nlp' { readDoc(text: string): Document; // returns number of learned entities learnCustomEntities(examples: CustomEntityExample[], config?: CerConfig): number; + vectorOf(word: string): number[]; its: ItsHelpers; as: AsHelpers; }