Class EmojiEmoticons

java.lang.Object
opennlp.tools.util.normalizer.EmojiEmoticons

final class EmojiEmoticons extends Object
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.
  • Field Details

  • Constructor Details

  • Method Details

    • getInstance

      static EmojiEmoticons getInstance()
      Returns the shared instance over the bundled emoji-emoticons.txt data. 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.txt data
      Throws:
      IllegalArgumentException - if the bundled data is malformed.
      IllegalStateException - if the resource is missing.
      UncheckedIOException - if the resource cannot be read.
    • emojiToEmoticon

      String emojiToEmoticon(CharSequence text)
      Folds mapped pictographs in text to their ASCII emoticons.
      Parameters:
      text - the text to fold. Must not be null.
      Returns:
      the folded text.
    • emojiToEmoticonAligned

      AlignedText emojiToEmoticonAligned(CharSequence text)
      Folds mapped pictographs in text to their ASCII emoticons, producing the Alignment back 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 be null.
      Returns:
      the folded text with its alignment.
    • emoticonToEmoji

      String emoticonToEmoji(CharSequence text)
      Folds whitespace-delimited ASCII emoticons in text to their pictographs.
      Parameters:
      text - the text to fold. Must not be null.
      Returns:
      the folded text.
    • emoticonToEmojiAligned

      AlignedText emoticonToEmojiAligned(CharSequence text)
      Folds whitespace-delimited ASCII emoticons in text to their pictographs, producing the Alignment back to the original text.
      Parameters:
      text - the text to fold. Must not be null.
      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 - if true, a source matches only when delimited by the text boundary or Unicode White_Space on both sides (the emoticon direction). If false, 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)
      Like substitute(CharSequence, Direction, boolean) but also produces the Alignment back 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 - see substitute(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 position i. 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 at i, or null if there are none.
      delimited - whether the whitespace-delimited boundary rule applies.
      Returns:
      the winning candidate encoded as (candidateIndex << 32) | consumedChars, or -1 when nothing folds here.
    • boundaryBefore

      private static boolean boundaryBefore(CharSequence text, int i)
      Returns whether position i is preceded by the text boundary or whitespace.
      Parameters:
      text - the text being scanned.
      i - the scan position.
      Returns:
      whether position i is preceded by the text boundary or whitespace
    • boundaryAfter

      private static boolean boundaryAfter(CharSequence text, int end)
      Returns whether position end is the text boundary or followed by whitespace.
      Parameters:
      text - the text being scanned.
      end - the position just past a candidate match.
      Returns:
      whether position end is the text boundary or followed by whitespace
    • loadBundled

      private static EmojiEmoticons.Tables loadBundled()
      Returns the tables parsed from the bundled emoji-emoticons.txt resource.
      Returns:
      the tables parsed from the bundled emoji-emoticons.txt resource
      Throws:
      IllegalStateException - Thrown if the resource is missing.
      UncheckedIOException - Thrown if the resource cannot be read.
    • parse

      static EmojiEmoticons.Tables parse(InputStream in) throws IOException
      Parses rows of source ; target ; fold_type ; standard ; unicode_version ; notes with space-separated hexadecimal code points; '#' starts a comment line. EMOJI rows load into the emoji-to-emoticon table, EMOTICON rows 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 be null.
      Returns:
      the two direction tables.
      Throws:
      IOException - Thrown if the stream cannot be read.
      IllegalArgumentException - Thrown if the data is malformed.
    • direction

      private static EmojiEmoticons.Direction direction(Map<Integer,List<EmojiEmoticons.Mapping>> table)
      Builds a EmojiEmoticons.Direction from a parsed table: sorts each candidate list longest source first, so the scan in matchAt(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 by parse(InputStream).
      Returns:
      the immutable direction.
    • decode

      private static String decode(String hexCodePoints, int lineNumber, String content)
      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].