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-web-model:

npm install wink-nlp --save
npm install wink-eng-lite-web-model --save
If you’re using winkNLP in the browser use the wink-eng-lite-web-model. Learn about its installation and usage in our guide to using winkNLP in the browser. Explore winkNLP recipes on Observable for live browser based examples.

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.
const winkNLP = require( 'wink-nlp' );
// Load english language model — light version.
const model = require( 'wink-eng-lite-web-model' );
// Instantiate winkNLP.
const nlp = winkNLP( model );
// Obtain "its" helper to extract item properties.
const its = nlp.its;
// Obtain "as" reducer helper to reduce a collection.
const as = nlp.as;
The "its" and "as" helpers may be directly obtained via require() from wink-nlp/src/ directory using its.js and as.js files.

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