out()

The out() method produces output in JavaScript built-in datatypes intelligently depending on the usage. It is available universally at all levels — document, collection, and item.

No other winkNLP APIs can be chained after the out() method. This method returns the output in JavaScript data types.

item.out() and doc.out()

out( its.propertyNameoptional ) → { stringdefault }

The behaviour of doc.out() and item.out() is similar. The out() method accepts its.propertyName as one of its parameter. The default parameter is its.value, which returns the winkNLP doc as a string. Some properties like shape, prefix, suffix, PoS tag, etc. can be computed only for token items.

You can find a complete list of properties in the reference section.

Examples:

Let’s explore the out() method for the winkNLP document and items with the following example text:

const text = '#Celebration Nov. 20, 2018 marks the @Space_Station\'s 20th anniversary🌌! Our orbiting laboratory makes technology and research breakthroughs beyond    Earth🌎. Unpack its architecture here: https://go.nasa.gov/2FH23qG #SpaceStation20th🚀';
const patterns = [
  { name: 'wordEmoji', patterns: [ '[NOUN|PROPN] [EMOJI]' ] },
  { name: 'hashtagEmoji', patterns: [ '[HASHTAG] [EMOJI]' ] }
];
nlp.learnCustomEntities( patterns );
const doc = nlp.readDoc( text );

doc.out()

doc.out( its.propertyNameoptional ) → { stringdefault }

console.log( doc.out() );
// -> #Celebration Nov. 20, 2018 marks the @Space_Station's 20th
//    anniversary🌌! Our orbiting laboratory makes technology and
//    research breakthroughs beyond    Earth🌎. Unpack its architecture
//    here: https://go.nasa.gov/2FH23qG #SpaceStation20th🚀
winkNLP has a lossless tokenizer — notice how it has preserved and reproduced all the spaces between "beyond" and "Earth" in the following examples.

sentence.out()

doc.sentences().itemAt( 0 ).out( its.propertyName) → { stringdefault }

const sentence = doc.sentences().itemAt( 1 )
console.log( `${ sentence.index() }:`, sentence.out() );
// -> 1: Our orbiting laboratory makes technology and research breakthroughs
//       beyond    Earth🌎.

entity.out()

doc.entities().itemAt( 0 ).out(its.propertyName) → {stringdefault}

const entity = doc.entities().itemAt( 3 );
console.log( entity.out( its.value ) ); // Returns: string
// -> 20th

console.log( entity.out( its.detail ) ); // Returns: object
// -> { value: '20th', type: 'ORDINAL' }

console.log( entity.out( its.type ) ); // Returns: string
// -> ORDINAL

customEntity.out()

doc.customEntities().itemAt( 0 ).out( its.propertyName ) -> { stringdefault}

// Note that the custom entities were learnt before running readDoc above
customEntities = doc.customEntities().out( its.detail );
console.log( customEntities );
// -> [ { value: 'anniversary🌌', type: 'wordEmoji' },
//      { value: 'Earth🌎', type: 'wordEmoji' },
//      { value: '#SpaceStation20th🚀', type: 'hashtagEmoji' } ]

// Custom entity item
const customEntity = doc.customEntities().itemAt( 1 );
console.log( customEntity.out( its.value ) ); // Returns: string
// -> Earth🌎

console.log( customEntity.out( its.detail ) ); // Returns: object
// -> { value: 'Earth🌎', type: 'wordEmoji' }

console.log( customEntity.out( its.type ) ); // Returns: string
// -> wordEmoji

token.out()

doc.tokens().itemAt( 0 ).out( its.propertyName) → {stringdefault}

const token = doc.tokens().itemAt( 31 );
console.log( `Token: ${ token.out() }, Type: "${ token.out( its.type ) }"` );
// -> Token: #SpaceStation20th, Type: "hashtag"

collection.out()

out( its.propertyName, as.reducedValue ) → {string[ ]default}

Any winkNLP collection followed by the out() method returns an array of strings by default.

The out() API for collection accepts two parameters: its.propertyName and as.reducedValue.

The first parameter, its.propertyName behaves similar to the out() method for document and item.

The second parameter is a predefined reducer function which is executed on each item of the collection to compute bag of words, frequency table, bigrams, etc. It defaults to as.array.

You can find a complete list of reducers in the reference section.

Examples: Let’s explore the out() method for collections with the following example text:

const text = 'The Godfather premiered on March 15, 1972 and was released on March 24, 1972. It is the first installment in The Godfather trilogy. The story of the movie spans from 1945 to 1955.';
const patterns = [
  { name: 'event', patterns: [ '[VERB] [|ADP] [DATE|DURATION]' ] }
];
nlp.learnCustomEntities( patterns );
const doc = nlp.readDoc( text );

sentences().out()

doc.sentences().out(its.propertyName, as.reducedValue) → {string[ ]default}

console.log( doc.sentences().out() );
// -> [ 'The Godfather premiered on March 15, 1972 and was released on
//       March 24, 1972.',
//      'It is the first installment in The Godfather trilogy.',
//      'The story of the movie spans from 1945 to 1955.' ]

entities.out()

doc.entities().out( its.propertyName, as.reducedValue ) → {string[ ]default}

The default, string array returned by its.value contains only the text of the entity. However, the its.detail property returns an object array with the entity text and type together. For example:

console.log( doc.entities().out( its.value, as.array ) );
// -> [ 'March 15, 1972',
//      'March 24, 1972',
//      'first',
//      'from 1945 to 1955' ]

console.log( doc.entities().out( its.detail, as.array ) );
// -> [ { value: 'March 15, 1972', type: 'DATE' },
//      { value: 'March 24, 1972', type: 'DATE' },
//      { value: 'first', type: 'ORDINAL' },
//      { value: 'from 1945 to 1955', type: 'DURATION' } ]

console.log( doc.entities().out( its.type, as.array ) );
// -> [ 'DATE', 'DATE', 'ORDINAL', 'DURATION' ]

customEntities().out()

doc.customEntities().out( its.propertyName, as.reducedValue ) -> { string[]default}

console.log( doc.customEntities().out( its.value, as.array ) );
// -> [ 'premiered on March 15, 1972',
//      'released on March 24, 1972',
//      'spans from 1945 to 1955' ]

console.log( doc.customEntities().out( its.detail, as.array ) );
// -> [ { value: 'premiered on March 15, 1972', type: 'event' },
//      { value: 'released on March 24, 1972', type: 'event' },
//      { value: 'spans from 1945 to 1955', type: 'event' } ]

console.log( doc.customEntities().out( its.type, as.array ) );
// -> [ 'event', 'event', 'event' ]

tokens().out()

doc.tokens().out( its.propertyName, as.reducedValue ) → {string[ ]default}

console.log( doc.tokens().out() ); // default
// -> [ 'The', 'Godfather', 'premiered',...'1945', 'to', '1955', '.' ]

console.log( doc.tokens().out( its.type ) ); // get the token types
// -> [ 'word', 'word', 'word',...'number', 'word', 'number', 'punctuation' ]

Leave feedback