Class UnicodeEmojiSequences

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

final class UnicodeEmojiSequences extends Object
Finds complete emoji at a position of a text, from the bundled inventory of Unicode Emoji 18.0 sequences.

A sequence is fully-qualified in the sense of UTS #51 when every code point that needs an emoji presentation selector has one, and minimally-qualified when only its first code point has it. The inventory holds the fully-qualified and minimally-qualified lines of emoji-test.txt, which cover single emoji, emoji with U+FE0F, flags, keycaps, skin tone modifier sequences, ZWJ sequences and tag sequences, in a trie, plus the Emoji_Component ranges of emoji-data.txt: the joiners, modifiers, selectors, flag letters and tags that occur inside sequences. A component on its own is never an emoji, but a stray one next to an emoji marks a malformed candidate that is kept as it is.

  • Field Details

    • RESOURCE

      private static final String RESOURCE
      See Also:
    • INSTANCE

      private static final BundledUnicodeData.Lazy<UnicodeEmojiSequences> INSTANCE
    • SEQUENCE_RECORD

      private static final String SEQUENCE_RECORD
      Prefix of a record holding one qualified sequence.
      See Also:
    • COMPONENT_RECORD

      private static final String COMPONENT_RECORD
      Prefix of a record holding one Emoji_Component code point range.
      See Also:
    • COMMENT

      private static final char COMMENT
      First character of a comment line of the data file.
      See Also:
    • VARIATION_SELECTOR_TEXT

      static final int VARIATION_SELECTOR_TEXT
      U+FE0E asks for text presentation. It has no Emoji_Component property but it binds to the symbol before it the same way U+FE0F does, so it counts as a structural component.
      See Also:
    • VARIATION_SELECTOR_EMOJI

      static final int VARIATION_SELECTOR_EMOJI
      U+FE0F asks for emoji presentation.
      See Also:
    • KEYCAP_BASES

      private static final CodePointSet KEYCAP_BASES
      The bases of the keycap sequences: #, * and the ASCII digits. They have the Emoji_Component property, but as ordinary text characters they must never make a neighboring emoji a malformed candidate, so they are excluded from the structural components.
    • root

      private final UnicodeEmojiSequences.Node root
    • componentRanges

      private final int[][] componentRanges
    • sequenceCount

      private final int sequenceCount
    • candidateStarts

      private final BitSet candidateStarts
      Every code point a candidate can start with: the first code point of a sequence or a structural component. One bit test rejects nearly every position of ordinary text before the trie is touched.
  • Constructor Details

    • UnicodeEmojiSequences

      private UnicodeEmojiSequences(UnicodeEmojiSequences.Node root, int[][] componentRanges, int sequenceCount)
  • Method Details

    • getInstance

      static UnicodeEmojiSequences getInstance()
      Returns the matcher built from the bundled data, loaded on first use. A failed load is not cached, so the next call loads again.
      Returns:
      the matcher built from the bundled data, loaded on first use
      Throws:
      IllegalStateException - Thrown if the bundled data resource is missing.
      UncheckedIOException - Thrown if the bundled data resource cannot be read.
      IllegalArgumentException - Thrown if the bundled data is malformed.
    • isCandidateStart

      boolean isCandidateStart(int codePoint)
      Returns whether a code point can start a candidate. A cheap pre-check for callers that scan text, so the trie is only touched at positions that can hold an emoji.
      Parameters:
      codePoint - The code point to test.
      Returns:
      whether a code point can start a candidate
    • sequenceCount

      int sequenceCount()
      Returns the number of sequence records that were loaded.
      Returns:
      the number of sequence records that were loaded
    • candidateAt

      UnicodeEmojiSequences.Candidate candidateAt(CharSequence text, int start)
      Finds the emoji candidate that starts at a position.

      The candidate is a run of complete sequences, or a malformed one: an emoji with a stray component such as a trailing joiner or an extra modifier, a leading component connected to an emoji, or a prefix of a sequence that continues with a component. A stray component connects only to the complete sequence right before it and to the one right after it; complete sequences that merely touch the malformed part are not included, so the caller removes them and sees the malformed candidate at a later call. One U+FE0F right after a complete sequence is part of it.

      Parameters:
      text - The text to look into. Must not be null.
      start - The index the candidate must start at.
      Returns:
      null if no candidate starts at start; otherwise a candidate that is valid when it consists of complete sequences only, and invalid when it holds a stray component or an incomplete sequence, with UnicodeEmojiSequences.Candidate.end() as the index after it in both cases.
    • match

      private int match(CharSequence text, int start)
      Returns the index after the longest complete sequence starting at start, or -1 if none starts there.
      Parameters:
      text - The text to look into. Must not be null.
      start - The index the sequence must start at.
      Returns:
      the index after the longest complete sequence starting at start, or -1 if none starts there
    • walk

      private UnicodeEmojiSequences.Walk walk(CharSequence text, int start)
      Walks the trie along the text from a position.
      Parameters:
      text - The text to look into. Must not be null.
      start - The index the walk starts at.
      Returns:
      The index after the longest complete sequence, or -1 if none, and the index after the longest prefix of any sequence, which is start if none.
    • isStructuralComponent

      private boolean isStructuralComponent(int codePoint)
      Returns whether a code point is a joiner, modifier, selector, flag letter or tag that occurs inside sequences. These are the Emoji_Component ranges plus U+FE0E, without the keycap bases.
      Parameters:
      codePoint - The code point to test.
      Returns:
      whether a code point is a joiner, modifier, selector, flag letter or tag that occurs inside sequences
    • load

      private static UnicodeEmojiSequences load()
      Loads the bundled data resource.
      Returns:
      The matcher.
      Throws:
      IllegalStateException - Thrown if the bundled data resource is missing.
      UncheckedIOException - Thrown if the bundled data resource cannot be read.
      IllegalArgumentException - Thrown if the bundled data is malformed.
    • parse

      static UnicodeEmojiSequences parse(InputStream in) throws IOException
      Parses S; sequence and C; component range records into a matcher. Package-visible so the malformed-data handling can be exercised without the bundled resource.
      Parameters:
      in - The records to read, one per line; # lines are comments.
      Returns:
      The matcher.
      Throws:
      IOException - Thrown if reading in fails.
      IllegalArgumentException - Thrown if a record is malformed.