filter()

filter( callback ) → { collection }

This method accepts a callback function as its parameter, which tests each item of the collection on which the method is called. Items which pass the test are filtered out. A new winkNLP collection of the same type is returned. For example, when a collection of entities is filtered, it returns a new collection of entities!

Note: This API works only with a collection of entities and tokens.

The filtered out, new collection can be further chained with other methods like itemAt(), length(), each(), etc. To get the collection in a JavaScript data type, you need to use the out() method.

Examples:

const text = '#Breaking: Can’t get over this #Oscars selfie from @TheEllenShow🤩! Go check it out:)https://pic.twitter.com/C9U5NOtGap #Share your best selfie@r2d2@gmail.com💯';
const patterns = [
  { name: 'wordEmoji', patterns: [ '[|NOUN|PROPN|ADP] [EMOJI|EMOTICON]' ] },
  { name: 'emailEmoji', patterns: [ '[EMAIL] [EMOJI|EMOTICON]' ] },
  { name: 'mentionEmoji', patterns: [ '[MENTION] [EMOJI|EMOTICON]' ] }
];
nlp.learnCustomEntities( patterns );
const doc = nlp.readDoc( text );

entities.filter()

doc.entities().filter( callback ) → { collection }

const hashtags = doc.entities()
  .filter( ( e ) => ( e.out( its.type ) === 'HASHTAG') )
  .out();
console.log( hashtags );
// -> [ '#Breaking', '#Oscars', '#Share' ]

customEntities().filter()

doc.customEntities().filter( callback ) -> { collection }

const mentionEmoji = doc.customEntities()
  .filter( ( c ) => ( c.out( its.type ) === 'mentionEmoji' ) )
  .out();
console.log( mentionEmoji );
// -> [ '@TheEllenShow🤩' ]

tokens().filter()

doc.tokens().filter( callback ) → { collection }

const punctuations = doc.tokens()
  .filter( ( t ) => ( t.out( its.type ) === 'punctuation' ) )
  .out();
console.log( punctuations );
// -> [ ':', '!' ]

Leave feedback