Package opennlp.tools.util.normalizer
Class EmojiEmoticons
java.lang.Object
opennlp.tools.util.normalizer.EmojiEmoticons
The bundled emoji/emoticon fold tables (
emoji-emoticons.txt) and the sequence
substitution shared by EmojiToEmoticonCharSequenceNormalizer and
EmoticonToEmojiCharSequenceNormalizer. Both directions substitute code point
sequences, longest match first at each position. In the emoticon direction a source
matches only when delimited by the text boundary or whitespace on both sides; in the emoji
direction a mapped pictograph inside a ZWJ sequence or before U+FE0E is left untouched, and a
trailing U+FE0F is absorbed into the fold.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final recordOne direction table, keyed by the first code point of the source sequence, candidates ordered longest source first so a scan is longest-match.(package private) static final recordOne fold row: a source code point sequence and its replacement.(package private) static final recordThe two direction tables produced byparse(InputStream). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringStarts a comment line inemoji-emoticons.txt.private final EmojiEmoticons.Directionprivate final EmojiEmoticons.Directionprivate static final charField separator inemoji-emoticons.txt(source ; target ; fold_type ; standard ; unicode_version ; notes).private static final BundledUnicodeData.Lazy<EmojiEmoticons> private static final charSeparates hex code points inside a source or target field.private static final Stringprivate static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleanboundaryAfter(CharSequence text, int end) Returns whether positionendis the text boundary or followed by whitespace.private static booleanboundaryBefore(CharSequence text, int i) Returns whether positioniis preceded by the text boundary or whitespace.private static StringDecodes a space-separated hexadecimal code point sequence.private static EmojiEmoticons.Directiondirection(Map<Integer, List<EmojiEmoticons.Mapping>> table) Builds aEmojiEmoticons.Directionfrom a parsed table: sorts each candidate list longest source first, so the scan inmatchAt(java.lang.CharSequence, int, java.util.List<opennlp.tools.util.normalizer.EmojiEmoticons.Mapping>, boolean)is longest-match by construction, and computes the first-code-point bounds that feed the no-match short circuit.(package private) StringemojiToEmoticon(CharSequence text) Folds mapped pictographs intextto their ASCII emoticons.(package private) AlignedTextFolds mapped pictographs intextto their ASCII emoticons, producing theAlignmentback to the original text.(package private) StringemoticonToEmoji(CharSequence text) Folds whitespace-delimited ASCII emoticons intextto their pictographs.(package private) AlignedTextFolds whitespace-delimited ASCII emoticons intextto their pictographs, producing theAlignmentback to the original text.(package private) static EmojiEmoticonsReturns the shared instance over the bundledemoji-emoticons.txtdata.private static EmojiEmoticons.TablesReturns the tables parsed from the bundledemoji-emoticons.txtresource.private static longmatchAt(CharSequence text, int i, List<EmojiEmoticons.Mapping> candidates, boolean delimited) Finds the winning candidate at positioni.(package private) static EmojiEmoticons.Tablesparse(InputStream in) Parses rows ofsource ; target ; fold_type ; standard ; unicode_version ; noteswith space-separated hexadecimal code points;'#'starts a comment line.private static Stringsubstitute(CharSequence text, EmojiEmoticons.Direction direction, boolean delimited) Applies the direction table in a single longest-match-first cursor pass.private static AlignedTextsubstituteAligned(CharSequence text, EmojiEmoticons.Direction direction, boolean delimited) Likesubstitute(CharSequence, Direction, boolean)but also produces theAlignmentback to the original text.
-
Field Details
-
RESOURCE
- See Also:
-
COMMENT_PREFIX
Starts a comment line inemoji-emoticons.txt.- See Also:
-
FIELD_SEPARATOR
private static final char FIELD_SEPARATORField separator inemoji-emoticons.txt(source ; target ; fold_type ; standard ; unicode_version ; notes).- See Also:
-
MAPPING_CODE_POINT_SEPARATOR
private static final char MAPPING_CODE_POINT_SEPARATORSeparates hex code points inside a source or target field. The bundled table format uses ASCII space (U+0020), not a general whitespace class.- See Also:
-
ZERO_WIDTH_JOINER
private static final int ZERO_WIDTH_JOINER- See Also:
-
INSTANCE
-
emojiToEmoticon
-
emoticonToEmoji
-
-
Constructor Details
-
EmojiEmoticons
-
-
Method Details
-
getInstance
Returns the shared instance over the bundledemoji-emoticons.txtdata. The tables are loaded on the first call; a failed load is not cached, so the next call loads again.- Returns:
- the shared instance over the bundled
emoji-emoticons.txtdata - Throws:
IllegalArgumentException- if the bundled data is malformed.IllegalStateException- if the resource is missing.UncheckedIOException- if the resource cannot be read.
-
emojiToEmoticon
Folds mapped pictographs intextto their ASCII emoticons.- Parameters:
text- the text to fold. Must not benull.- Returns:
- the folded text.
-
emojiToEmoticonAligned
Folds mapped pictographs intextto their ASCII emoticons, producing theAlignmentback to the original text. Each replaced source sequence, including an absorbed trailing U+FE0F, maps to its replacement as one block.- Parameters:
text- the text to fold. Must not benull.- Returns:
- the folded text with its alignment.
-
emoticonToEmoji
Folds whitespace-delimited ASCII emoticons intextto their pictographs.- Parameters:
text- the text to fold. Must not benull.- Returns:
- the folded text.
-
emoticonToEmojiAligned
Folds whitespace-delimited ASCII emoticons intextto their pictographs, producing theAlignmentback to the original text.- Parameters:
text- the text to fold. Must not benull.- Returns:
- the folded text with its alignment.
-
substitute
private static String substitute(CharSequence text, EmojiEmoticons.Direction direction, boolean delimited) Applies the direction table in a single longest-match-first cursor pass.- Parameters:
text- the text to fold.direction- the direction table to apply.delimited- iftrue, a source matches only when delimited by the text boundary or UnicodeWhite_Spaceon both sides (the emoticon direction). Iffalse, the pictographic sequence rules apply instead: no fold inside a ZWJ sequence or before U+FE0E, and a trailing U+FE0F is absorbed.- Returns:
- the folded text.
-
substituteAligned
private static AlignedText substituteAligned(CharSequence text, EmojiEmoticons.Direction direction, boolean delimited) Likesubstitute(CharSequence, Direction, boolean)but also produces theAlignmentback to the original text. Each replaced source sequence, including an absorbed trailing U+FE0F, maps to its replacement as one block.- Parameters:
text- the text to fold.direction- the direction table to apply.delimited- seesubstitute(CharSequence, Direction, boolean).- Returns:
- the folded text with its alignment.
-
matchAt
private static long matchAt(CharSequence text, int i, List<EmojiEmoticons.Mapping> candidates, boolean delimited) Finds the winning candidate at positioni. Candidates are pre-sorted longest source first, so the first acceptable region match wins. In the pictographic direction (delimited == false) a match is rejected when the source adjoins a ZWJ sequence or is followed by U+FE0E, and a trailing U+FE0F joins the consumed region.- Parameters:
text- the text being scanned.i- the scan position.candidates- the candidates whose source starts with the code point ati, ornullif there are none.delimited- whether the whitespace-delimited boundary rule applies.- Returns:
- the winning candidate encoded as
(candidateIndex << 32) | consumedChars, or-1when nothing folds here.
-
boundaryBefore
Returns whether positioniis preceded by the text boundary or whitespace.- Parameters:
text- the text being scanned.i- the scan position.- Returns:
- whether position
iis preceded by the text boundary or whitespace
-
boundaryAfter
Returns whether positionendis the text boundary or followed by whitespace.- Parameters:
text- the text being scanned.end- the position just past a candidate match.- Returns:
- whether position
endis the text boundary or followed by whitespace
-
loadBundled
Returns the tables parsed from the bundledemoji-emoticons.txtresource.- Returns:
- the tables parsed from the bundled
emoji-emoticons.txtresource - Throws:
IllegalStateException- Thrown if the resource is missing.UncheckedIOException- Thrown if the resource cannot be read.
-
parse
Parses rows ofsource ; target ; fold_type ; standard ; unicode_version ; noteswith space-separated hexadecimal code points;'#'starts a comment line.EMOJIrows load into the emoji-to-emoticon table,EMOTICONrows into the emoticon-to-emoji table. Package-private so the malformed-data handling can be exercised without the bundled resource.- Parameters:
in- the stream to parse. Must not benull.- Returns:
- the two direction tables.
- Throws:
IOException- Thrown if the stream cannot be read.IllegalArgumentException- Thrown if the data is malformed.
-
direction
Builds aEmojiEmoticons.Directionfrom a parsed table: sorts each candidate list longest source first, so the scan inmatchAt(java.lang.CharSequence, int, java.util.List<opennlp.tools.util.normalizer.EmojiEmoticons.Mapping>, boolean)is longest-match by construction, and computes the first-code-point bounds that feed the no-match short circuit.- Parameters:
table- the mutable table produced byparse(InputStream).- Returns:
- the immutable direction.
-
decode
Decodes a space-separated hexadecimal code point sequence.- Parameters:
hexCodePoints- the field to decode.lineNumber- the line number, for the error message.content- the full line, for the error message.- Returns:
- the decoded sequence.
- Throws:
IllegalArgumentException- Thrown if the field is empty, not valid hexadecimal, or names a value outside[0, U+10FFFF].
-