Package opennlp.tools.util.normalizer
Class Alignment.Builder
java.lang.Object
opennlp.tools.util.normalizer.Alignment.Builder
- Enclosing class:
Alignment
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
FieldsModifier and TypeFieldDescriptionprivate intprivate static final intprivate int[]private static final intprivate intprivate int[] -
Constructor Summary
ConstructorsConstructorDescriptionBuilder()Creates a builder with a small default capacity ofDEFAULT_CAPACITYentries.Builder(int expectedLength) Creates a builder pre-sized for a normalized text of aboutexpectedLengthcharacters, so recording the edits of a typical pass (one entry per normalized character) does not regrow the backing arrays. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidappend(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) RecordscharCountcharacters copied through unchanged (a one to one run).private voidgrow()Grows the backing arrays by 1.5x, capped at the maximum array size.replace(int originalCount, int normalizedCount) Records a rewritten block:originalCountoriginal characters that producednormalizedCountnormalized characters.
-
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 ofDEFAULT_CAPACITYentries. -
Builder
public Builder(int expectedLength) Creates a builder pre-sized for a normalized text of aboutexpectedLengthcharacters, 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 ifexpectedLengthis negative.
-
-
Method Details
-
equal
RecordscharCountcharacters 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 ifcharCountis negative.
-
replace
Records a rewritten block:originalCountoriginal characters that producednormalizedCountnormalized characters. Each produced character is attributed to the whole original block, since a collapse or expansion cannot be subdivided.0fornormalizedCountis a deletion;0fororiginalCountis 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 iforiginalCountornormalizedCountis negative.
-
build
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 exactlyoriginalLengthoriginal 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.
-