Package opennlp.tools.util.normalizer
Class SocialMediaCharSequenceNormalizer
java.lang.Object
opennlp.tools.util.normalizer.SocialMediaCharSequenceNormalizer
- All Implemented Interfaces:
Serializable,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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final SocialMediaCharSequenceNormalizerprivate static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the shared, stateless instance.private booleanisAsciiWord(int codePoint) Returns whethercodePointis an ASCII letter, digit, or underscore.private booleanisEmoticonEyes(char c) Returns whethercis an emoticon's eyes::,;, orx.private booleanisEmoticonMouth(char c) Returns whethercis an emoticon's mouth: one of( ) d o p, either case.private booleanisLaughConsonant(char c) Returns whethercis a laughter consonant:horj, either case.private booleanisLaughVowel(char c) Returns whethercis a laughter vowel:a e i o u, either case.private booleanisRetweetUnit(CharSequence text, int at) Returns whether the three chars starting atatform one retweet unit:rorR,torT, then a space or colon.normalize(CharSequence text) Normalizes a sequence of characters.private CharSequenceremoveEmoticons(CharSequence text) Replaces each emoticon with one space, including inside words: eyes, then an optional-nose (tried first when present), then a mouth.private CharSequenceReplaces each sequence of one or more retweet units with one space when the sequence starts on a word boundary.private CharSequenceReplaces 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@.private CharSequenceshrinkLaughter(CharSequence text) Shrinks laughter: a run ofh/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.private booleanwordBeforeBlocksBoundary(CharSequence text, int index) Returns whether the word boundary beforeindexis blocked.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
INSTANCE
-
-
Constructor Details
-
SocialMediaCharSequenceNormalizer
public SocialMediaCharSequenceNormalizer()
-
-
Method Details
-
getInstance
Returns the shared, stateless instance.- Returns:
- the shared, stateless instance
-
normalize
Normalizes a sequence of characters.- Specified by:
normalizein interfaceCharSequenceNormalizer- Parameters:
text- TheCharSequenceto normalize. Must not benull.- Returns:
- The normalized
CharSequence.
-
removeTagsAndHandles
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
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
Returns whether the three chars starting atatform one retweet unit:rorR,torT, 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
atform one retweet unit:rorR,torT, then a space or colon
-
wordBeforeBlocksBoundary
Returns whether the word boundary beforeindexis 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
indexis blocked
-
isAsciiWord
private boolean isAsciiWord(int codePoint) Returns whethercodePointis an ASCII letter, digit, or underscore.- Returns:
- whether
codePointis an ASCII letter, digit, or underscore
-
removeEmoticons
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 whethercis an emoticon's eyes::,;, orx.- Returns:
- whether
cis an emoticon's eyes::,;, orx
-
isEmoticonMouth
private boolean isEmoticonMouth(char c) Returns whethercis an emoticon's mouth: one of( ) d o p, either case.- Returns:
- whether
cis an emoticon's mouth: one of( ) d o p, either case
-
shrinkLaughter
Shrinks laughter: a run ofh/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 whethercis a laughter consonant:horj, either case.- Returns:
- whether
cis a laughter consonant:horj, either case
-
isLaughVowel
private boolean isLaughVowel(char c) Returns whethercis a laughter vowel:a e i o u, either case.- Returns:
- whether
cis a laughter vowel:a e i o u, either case
-