wink-nlp icon indicating copy to clipboard operation
wink-nlp copied to clipboard

Typescript Types are not accurate

Open ali-habibzadeh opened this issue 1 year ago • 3 comments

it seems docs says we should use the embeddings like this:

import wink from "wink-nlp";
import model from "wink-eng-lite-web-model";
import vectors from "wink-embeddings-sg-100d";

const nlp = wink(model, [], vectors);

But in the types wink doesn't take a 3rd argument:

wink(theModel: Model, pipe?: string[] | undefined): WinkMethods

but I can see its correct in code, but due to the clash we can't use this in Typescript

var nlp = function ( theModel, pipe = null, wordEmbeddings = null ) {

Additionally, most JS examples online do not match the types provided.

ali-habibzadeh avatar May 02 '24 21:05 ali-habibzadeh

Hi @ali-habibzadeh,

Thank you so much for highlighting the issue along with details. We've confirmed the TypeScript type inconsistency — an accidental miss. A fix is underway, and we'll update this issue with the progress. We're also improving our processes to prevent similar issues in the future.

Thank you once again for your contribution.

Best, Rachna

rachnachakraborty avatar May 03 '24 11:05 rachnachakraborty

Hi @ali-habibzadeh

Is it possible to also share the JS examples online do not match the types provided?

Best, Sanjaya

sanjayaksaxena avatar May 03 '24 13:05 sanjayaksaxena

Have released following packages to resolve the issue: 1. wink-embeddings-sg-100d: 1.1.0 2. wink-eng-lite-web-model: 1.7.1 3. wink-nlp: 2.2.1


Now the following typescript code works properly:

import WinkNLP, { AsHelpers, ItemSentence, Document, ItsHelpers, WinkMethods, ItemToken } from 'wink-nlp';
import model from 'wink-eng-lite-web-model';
import vectors from 'wink-embeddings-sg-100d';
import similarity from 'wink-nlp/utilities/similarity';

// Use only tokenization and sentence boundary detection pipe.
const nlp:WinkMethods = WinkNLP( model, [ 'sbd' ], vectors );
// Obtain "its" helper to extract item properties.
const its:ItsHelpers = nlp.its;
// Obtain "as" reducer helper to reduce a collection.
const as:AsHelpers = nlp.as;
// The following text contains 4-sentences, where the first
// two and the last two have high similarity.
const text:string = `The cat rested on the carpet. The kitten slept on the rug.
The table was in the drawing room. The desk was in the study room.`;
// This will hold the array of vectors for each sentence.
const v:any[][] = [];
// Run the nlp pipe.
const doc:Document = nlp.readDoc( text ); 
// Compute each sentence's embedding and fill in "v[i]".
// Only use words and ignore stop words.
doc
    .sentences()
    .each( ( s:ItemSentence, k:number ) => {
      v[ k ] = s
        .tokens()
        .filter( (t: ItemToken) => (t.out(its.type) === 'word' && !t.out(its.stopWordFlag)))
        .out(its.value, as.vector);
    })
// Compute & print similarity for each unique pair.
for ( let i:number = 0; i < v.length; i += 1 ) {
    for ( let j:number = i; j < v.length; j += 1 ) {
        if ( i !== j )
          console.log(
            doc.sentences().itemAt( i ).out(), ' & ',
            doc.sentences().itemAt( j ).out(),
            +similarity.vector.cosine( v[ i ], v[ j ] ).toFixed( 2 )
          );
    }
}

npx tsc index.ts --esModuleInterop --lib 'es6, dom' command was used to transpile the typescript.

sanjayaksaxena avatar May 06 '24 15:05 sanjayaksaxena