each()

each( callback ) → { void }

This method iterates on a collection and executes the provided function once for each item in the collection. The callback function executes once for each item in the collection, starting from first to the last item. The callback function accepts the single item from the collection and its index as parameters.

It works like the JavaScript forEach() method.

This method returns nothing. However, you can use the out() method in the callback function to get an output in a JavaScript data type. See the examples below:

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: [ '[|ADP] [EMOJI|EMOTICON]' ] },
  { name: 'emailEmoji', patterns: [ '[EMAIL] [EMOJI|EMOTICON]' ] },
  { name: 'mentionEmoji', patterns: [ '[MENTION] [EMOJI|EMOTICON]' ] }
];
nlp.learnCustomEntities( patterns );
const doc = nlp.readDoc( text );

sentences().each()

doc.sentences().each( callback ) → { void }

doc.sentences().each( ( sentence, index ) => {
  console.log( `${index}:`, sentence.out() );
} );
// -> 0: #Breaking: Can’t get over this #Oscars selfie from
//       @TheEllenShow🤩!
//    1: Go check it out:)https://pic.twitter.com/C9U5NOtGap #Share
//       your best selfie@r2d2@gmail.com💯

entities().each()

doc.entities().each( callback ) → { void }

doc.entities().each( ( entity, index ) => {
  console.log( `${index}:`, entity.out() );
} );
// -> 0: #Breaking
//    1: #Oscars
//    2: @TheEllenShow
//    3: 🤩
//    4: :)
//    5: https://pic.twitter.com/C9U5NOtGap
//    6: #Share
//    7: r2d2@gmail.com
//    8: 💯

customEntities().each()

doc.customEntities().each( callback ) -> { void }

doc.customEntities().each( ( customEntity, index ) => {
  console.log( `${ index }:`, customEntity.out() );
} );
// -> 0: @TheEllenShow🤩
//    1: out:)
//    2: r2d2@gmail.com💯

tokens().each()

doc.tokens().each( callback ) → { void }

doc.tokens().each( ( token, index ) => {
  console.log( `${index}:`, token.out() );
} );
// -> 0: #Breaking
//    1: :
//    2: Ca
//    3: n’t
//    .
//    .
//    .
//    23: @
//    24: r2d2@gmail.com
//    25: 💯

Leave feedback