Skip to content

Commit

Permalink
feat(*): improve error handling in contextual vectors
Browse files Browse the repository at this point in the history
and update typescript defn

Co-authored-by: Rachna <[email protected]>
  • Loading branch information
sanjayaksaxena and rachnachakraborty committed May 8, 2024
1 parent cb602a5 commit 64d1f40
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/doc-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions test/contextual-vectors-specs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ declare module 'wink-nlp' {
out<T, U>(itsf: ItsFunction<T>, asf: AsFunction<T, U>): U | T[] | string[];
}

export interface Include {
lemma?: boolean;
specificWordVectors?: string[];
similarWordVectors?: boolean;
wordVectorsLimit?: number;
}

export interface Document {
entities(): Entities;
customEntities(): CustomEntities;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 64d1f40

Please sign in to comment.