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
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 );
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 ] ]
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', '?' ]
Let's explore the wink-nlp document in the next section.