Getting started

Let's start with "Hello, World!" in winkNLP. First we'll install the wink-nlp package. Next, we'll use that package to install the wink-eng-lite-model:

npm install wink-nlp --save
node -e "require( 'wink-nlp/models/install' )" wink-eng-lite-model
If you’re using winkNLP in the browser use the wink-eng-lite-web-model instead. Learn about its installation and usage in our guide to using winkNLP in the browser.

We start our code by loading the wink-nlp package, some helpers, and the language model. Then we instantiate winkNLP using the language model:

// Load wink-nlp package  & helpers.
const winkNLP = require( 'wink-nlp' );
// Load "its" helper to extract item properties.
const its = require( 'wink-nlp/src/its.js' );
// Load "as" reducer helper to reduce a collection.
const as = require( 'wink-nlp/src/as.js' );
// Load english language model — light version.
const model = require( 'wink-eng-lite-model' );
// Instantiate winkNLP.
const nlp = winkNLP( model );
It is also possible to acquire "its" and "as" helpers directly from `nlp` from version 1.5.0 onwards. These are acquired using `const its = nlp.its` and `const as =`.

Next, we use the readDoc() method of winkNLP. It transforms the input text string into a winkNLP document. It is the centerpiece of your code. The document provides a rich set of chainable API methods for easy navigation through it's sentences, entities and/or tokens. This simplifies information processing and extraction from a document.

const text = 'Hello   World🌎! How are you?';
const doc = nlp.readDoc( text );

console.log( doc.out() );
// -> Hello   World🌎! How are you?

console.log( doc.tokens().out( its.type, as.freqTable ) );
// -> [ [ 'word', 5 ], [ 'punctuation', 2 ], [ 'emoji', 1 ] ]
winkNLP has a lossless tokenizer — notice how it has preserved and reproduced all the spaces between "Hello" and "World" in the first example.

The out() method always returns JavaScript’s built-in datatypes — most often arrays of Strings or objects. doc.out() is context dependent, for example, it produces a string when used on doc, but when applied on sentences, entities, or tokens, it outputs an array of strings:

console.log( doc.sentences().out() );
// -> [ 'Hello   World🌎!', 'How are you?' ]

console.log( doc.entities().out( its.detail ) );
// -> [ { value: '🌎', type: 'EMOJI' } ]

console.log( doc.tokens().out() );
// -> [ 'Hello', 'World', '🌎', '!', 'How', 'are', 'you', '?' ]

Before we jump to the wink-nlp document, let's explore the processing pipeline in the next section.

Leave feedback