Class Alignment.Builder

java.lang.Object
opennlp.tools.util.normalizer.Alignment.Builder
Enclosing class:
Alignment

public static final class Alignment.Builder extends Object
Builds an Alignment as the normalized text is produced, by recording each edit in order. Call equal(int) for characters copied through unchanged and replace(int, int) for a block that was rewritten (including deletions and insertions), then build(int).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
     
    private static final int
     
    private int[]
     
    private static final int
     
    private int
     
    private int[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a builder with a small default capacity of DEFAULT_CAPACITY entries.
    Builder(int expectedLength)
    Creates a builder pre-sized for a normalized text of about expectedLength characters, so recording the edits of a typical pass (one entry per normalized character) does not regrow the backing arrays.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    append(int start, int end)
    Appends one normalized character's original range, growing the backing arrays as needed.
    build(int originalLength)
    Finalizes the alignment.
    equal(int charCount)
    Records charCount characters copied through unchanged (a one to one run).
    private void
    Grows the backing arrays by 1.5x, capped at the maximum array size.
    replace(int originalCount, int normalizedCount)
    Records a rewritten block: originalCount original characters that produced normalizedCount normalized characters.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_ARRAY_SIZE

      private static final int MAX_ARRAY_SIZE
      See Also:
    • DEFAULT_CAPACITY

      private static final int DEFAULT_CAPACITY
      See Also:
    • starts

      private int[] starts
    • ends

      private int[] ends
    • count

      private int count
    • originalCursor

      private int originalCursor
  • Constructor Details

    • Builder

      public Builder()
      Creates a builder with a small default capacity of DEFAULT_CAPACITY entries.
    • Builder

      public Builder(int expectedLength)
      Creates a builder pre-sized for a normalized text of about expectedLength characters, so recording the edits of a typical pass (one entry per normalized character) does not regrow the backing arrays. The value is a sizing hint only; it does not limit how many edits can be recorded.
      Parameters:
      expectedLength - The expected normalized length, typically the length of the text being normalized; must not be negative.
      Throws:
      IllegalArgumentException - Thrown if expectedLength is negative.
  • Method Details

    • equal

      public Alignment.Builder equal(int charCount)
      Records charCount characters copied through unchanged (a one to one run).
      Parameters:
      charCount - The number of UTF-16 characters; must not be negative.
      Returns:
      This builder.
      Throws:
      IllegalArgumentException - Thrown if charCount is negative.
    • replace

      public Alignment.Builder replace(int originalCount, int normalizedCount)
      Records a rewritten block: originalCount original characters that produced normalizedCount normalized characters. Each produced character is attributed to the whole original block, since a collapse or expansion cannot be subdivided. 0 for normalizedCount is a deletion; 0 for originalCount is an insertion.
      Parameters:
      originalCount - The number of original characters consumed; must not be negative.
      normalizedCount - The number of normalized characters produced; must not be negative.
      Returns:
      This builder.
      Throws:
      IllegalArgumentException - Thrown if originalCount or normalizedCount is negative.
    • build

      public Alignment build(int originalLength)
      Finalizes the alignment.
      Parameters:
      originalLength - The full length of the original text.
      Returns:
      The immutable Alignment.
      Throws:
      IllegalStateException - Thrown if the recorded edits do not consume exactly originalLength original characters (a sign that some input was not accounted for).
    • append

      private void append(int start, int end)
      Appends one normalized character's original range, growing the backing arrays as needed.
      Parameters:
      start - The inclusive original start offset.
      end - The exclusive original end offset.
    • grow

      private void grow()
      Grows the backing arrays by 1.5x, capped at the maximum array size.
      Throws:
      OutOfMemoryError - Thrown when the required capacity exceeds the maximum array size.