Class SocialMediaCharSequenceNormalizer

java.lang.Object
opennlp.tools.util.normalizer.SocialMediaCharSequenceNormalizer
All Implemented Interfaces:
Serializable, CharSequenceNormalizer

public class SocialMediaCharSequenceNormalizer extends Object implements CharSequenceNormalizer
A CharSequenceNormalizer implementation that normalizes social media text: hashtags, user handles, retweet markers, emoticons, and repeated laughter. Every encounter will be replaced by a whitespace.

Normalization runs in four passes: a # or @ together with the following non-whitespace run becomes one space; each sequence of case-insensitive rt units, each followed by a space or colon, becomes one space when it starts on a word boundary; each emoticon (eyes :, ; or x, an optional - nose, and a mouth out of ( ) d o p, case-insensitive) becomes one space, including inside words. Finally, repeated laughter shrinks to two of its repeating units ("hahaha" to "haha"), comparing ASCII case-insensitively.

See Also:
  • Field Details

  • Constructor Details

    • SocialMediaCharSequenceNormalizer

      public SocialMediaCharSequenceNormalizer()
  • Method Details

    • getInstance

      public static SocialMediaCharSequenceNormalizer getInstance()
      Returns the shared, stateless instance.
      Returns:
      the shared, stateless instance
    • normalize

      public CharSequence normalize(CharSequence text)
      Normalizes a sequence of characters.
      Specified by:
      normalize in interface CharSequenceNormalizer
      Parameters:
      text - The CharSequence to normalize. Must not be null.
      Returns:
      The normalized CharSequence.
    • removeTagsAndHandles

      private CharSequence removeTagsAndHandles(CharSequence text)
      Replaces each hashtag or handle with one space: a # or @ matches only if a non-whitespace char follows, and the match then swallows the whole non-whitespace run, including further # and @.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when nothing matched, otherwise the normalized copy.
    • removeRetweetMarkers

      private CharSequence removeRetweetMarkers(CharSequence text)
      Replaces each sequence of one or more retweet units with one space when the sequence starts on a word boundary.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when nothing matched, otherwise the normalized copy.
    • isRetweetUnit

      private boolean isRetweetUnit(CharSequence text, int at)
      Returns whether the three chars starting at at form one retweet unit: r or R, t or T, then a space or colon.
      Parameters:
      text - The text to look into; never null.
      at - The index of the unit's first char; at least three chars must remain.
      Returns:
      whether the three chars starting at at form one retweet unit: r or R, t or T, then a space or colon
    • wordBeforeBlocksBoundary

      private boolean wordBeforeBlocksBoundary(CharSequence text, int index)
      Returns whether the word boundary before index is blocked. The boundary is blocked if the preceding code point is an ASCII word character, or is a non-spacing mark whose backwards base-character scan reaches a letter or digit. The scan reads one char at a time and stops on the low surrogate of a supplementary-plane mark; that stop is part of the boundary behavior this normalizer preserves.
      Parameters:
      text - The text to look into; never null.
      index - The index the boundary is checked before.
      Returns:
      whether the word boundary before index is blocked
    • isAsciiWord

      private boolean isAsciiWord(int codePoint)
      Returns whether codePoint is an ASCII letter, digit, or underscore.
      Returns:
      whether codePoint is an ASCII letter, digit, or underscore
    • removeEmoticons

      private CharSequence removeEmoticons(CharSequence text)
      Replaces each emoticon with one space, including inside words: eyes, then an optional - nose (tried first when present), then a mouth. A bare - is never a mouth.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when nothing matched, otherwise the normalized copy.
    • isEmoticonEyes

      private boolean isEmoticonEyes(char c)
      Returns whether c is an emoticon's eyes: :, ;, or x.
      Returns:
      whether c is an emoticon's eyes: :, ;, or x
    • isEmoticonMouth

      private boolean isEmoticonMouth(char c)
      Returns whether c is an emoticon's mouth: one of ( ) d o p, either case.
      Returns:
      whether c is an emoticon's mouth: one of ( ) d o p, either case
    • shrinkLaughter

      private CharSequence shrinkLaughter(CharSequence text)
      Shrinks laughter: a run of h/j, a run of vowels, and at least one complete repetition of runs of the two last characters of those runs, shrink to those two characters twice. The character sets involved are pairwise disjoint, so the greedy runs never overlap ambiguously; a failed attempt resumes at the next char.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when nothing matched, otherwise the normalized copy.
    • isLaughConsonant

      private boolean isLaughConsonant(char c)
      Returns whether c is a laughter consonant: h or j, either case.
      Returns:
      whether c is a laughter consonant: h or j, either case
    • isLaughVowel

      private boolean isLaughVowel(char c)
      Returns whether c is a laughter vowel: a e i o u, either case.
      Returns:
      whether c is a laughter vowel: a e i o u, either case