Package opennlp.tools.util
Class StringUtil
java.lang.Object
opennlp.tools.util.StringUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe ten ASCII digit strings"0"to"9", indexed by digit value. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcomputeShortestEditScript(String wordForm, String lemma, int[][] distance, StringBuffer permutations) Computes the Shortest Edit Script (SES) to convert a word into its lemma.static StringdecodeShortestEditScript(String wordForm, String permutations) Reads the predicted Shortest Edit Script (SES) by a lemmatizer model and applies the permutations to obtain the lemma from thewordForm.static StringgetShortestEditScript(String wordForm, String lemma) static booleanisEmpty(CharSequence theString) static booleanisUnicodeBlank(CharSequence input) truewheninputisnull, empty, or consists only of UnicodeWhite_Spacecode points.static booleanisUnicodeWhitespace(char charCode) Determines if the specifiedCharacteris a whitespace under the UnicodeWhite_Spaceproperty; delegates toUnicodeWhitespace.isWhitespace(int).static booleanisUnicodeWhitespace(int charCode) Determines if the specified code point is a whitespace under the UnicodeWhite_Spaceproperty; delegates toUnicodeWhitespace.isWhitespace(int).static booleanisWhitespace(char charCode) Determines if the specifiedCharacteris a whitespace under the activeWhitespaceMode: the UnicodeWhite_Spaceproperty by default, or, when the "opennlp.whitespace.mode" system property selectsWhitespaceMode.LEGACY, the union ofCharacter.isWhitespace(int)and the UnicodeZscategory (Character.SPACE_SEPARATOR) that OpenNLP 1.x and 2.x used.static booleanisWhitespace(int charCode) Determines if the specified code point is a whitespace under the activeWhitespaceMode; seeisWhitespace(char)for details.static int[][]levenshteinDistance(String wordForm, String lemma) Computes the Levenshtein distance of two strings in a matrix.static String[]Splitsinputon runs of UnicodeWhite_Space.static StringtoLowerCase(CharSequence string) Converts aCharSequenceto lower case, independent of the currentLocaleviaCharacter.toLowerCase(int)which uses mapping information from the UnicodeData file.static CharBuffertoLowerCaseCharBuffer(CharSequence sequence) static StringtoUpperCase(CharSequence string) Converts aCharSequenceto upper case, independent of the currentLocaleviaCharacter.toUpperCase(char)which uses mapping information from the UnicodeData file.static StringTrims leading and trailing runs of UnicodeWhite_Space, the same setsplitOnUnicodeWhitespace(CharSequence)breaks terms on.
-
Field Details
-
ASCII_DIGIT_STRINGS
The ten ASCII digit strings"0"to"9", indexed by digit value. Precomputed so code folding digits to ASCII does not allocate a new single-character string per digit; the list is immutable and safe to share.
-
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
isWhitespace
public static boolean isWhitespace(char charCode) Determines if the specifiedCharacteris a whitespace under the activeWhitespaceMode: the UnicodeWhite_Spaceproperty by default, or, when the "opennlp.whitespace.mode" system property selectsWhitespaceMode.LEGACY, the union ofCharacter.isWhitespace(int)and the UnicodeZscategory (Character.SPACE_SEPARATOR) that OpenNLP 1.x and 2.x used.Tokenization, corpus format parsing, and feature generation all resolve whitespace through this method, so they share one
WhitespaceModefor the life of the process; useisUnicodeWhitespace(char)directly where the Unicode definition should apply unconditionally, such as user-text normalization with no trained-model dependency.- Parameters:
charCode- The character to check.- Returns:
trueifcharCoderepresents a white space under the activeWhitespaceMode,falseotherwise.
-
isWhitespace
public static boolean isWhitespace(int charCode) Determines if the specified code point is a whitespace under the activeWhitespaceMode; seeisWhitespace(char)for details.- Parameters:
charCode- An int representation of a character to check.- Returns:
trueifcharCoderepresents a white space under the activeWhitespaceMode,falseotherwise.
-
isUnicodeWhitespace
public static boolean isUnicodeWhitespace(char charCode) Determines if the specifiedCharacteris a whitespace under the UnicodeWhite_Spaceproperty; delegates toUnicodeWhitespace.isWhitespace(int). This is also whatisWhitespace(char)resolves to under the defaultWhitespaceMode.- Parameters:
charCode- The character to check.- Returns:
trueifcharCodehas theWhite_Spaceproperty,falseotherwise.
-
isUnicodeWhitespace
public static boolean isUnicodeWhitespace(int charCode) Determines if the specified code point is a whitespace under the UnicodeWhite_Spaceproperty; delegates toUnicodeWhitespace.isWhitespace(int). This is also whatisWhitespace(int)resolves to under the defaultWhitespaceMode.- Parameters:
charCode- An int representation of a character to check.- Returns:
trueifcharCodehas theWhite_Spaceproperty,falseotherwise.
-
splitOnUnicodeWhitespace
Splitsinputon runs of UnicodeWhite_Space. Leading and trailing runs are ignored, so whitespace-only input yields an empty array. This is a code-point scan, not a regular expression.- Parameters:
input- The text to split. Must not benull.- Returns:
- The non-whitespace terms in order.
- Throws:
IllegalArgumentException- Ifinputisnull.
-
trimUnicodeWhitespace
Trims leading and trailing runs of UnicodeWhite_Space, the same setsplitOnUnicodeWhitespace(CharSequence)breaks terms on.- Parameters:
input- The text to trim. Must not benull.- Returns:
- The trimmed string; may be empty when
inputis whitespace-only. - Throws:
IllegalArgumentException- Ifinputisnull.
-
isUnicodeBlank
truewheninputisnull, empty, or consists only of UnicodeWhite_Spacecode points.- Parameters:
input- The text to test;nullis treated as blank.- Returns:
trueif there is no non-whitespace code point.
-
toLowerCase
Converts aCharSequenceto lower case, independent of the currentLocaleviaCharacter.toLowerCase(int)which uses mapping information from the UnicodeData file.- Parameters:
string- TheCharSequenceto transform.- Returns:
- The lower-cased String.
-
toLowerCaseCharBuffer
-
toUpperCase
Converts aCharSequenceto upper case, independent of the currentLocaleviaCharacter.toUpperCase(char)which uses mapping information from the UnicodeData file.- Parameters:
string- TheCharSequenceto transform.- Returns:
- The upper-cased String
-
isEmpty
- Returns:
trueifCharSequence.length()is0ornull, otherwisefalse- Since:
- 1.5.1
-
levenshteinDistance
Computes the Levenshtein distance of two strings in a matrix.Based on this pseudo-code which in turn is based on the paper Wagner, Robert A.; Fischer, Michael J. (1974), "The String-to-String Correction Problem", Journal of the ACM 21 (1): 168-173
- Parameters:
wordForm- The form as input.lemma- The target lemma.- Returns:
- A 2-dimensional Levenshtein distance matrix.
-
computeShortestEditScript
public static void computeShortestEditScript(String wordForm, String lemma, int[][] distance, StringBuffer permutations) Computes the Shortest Edit Script (SES) to convert a word into its lemma. This is based on Chrupala's PhD thesis (2008).- Parameters:
wordForm- The token.lemma- The target lemma.distance- A 2-dimensional Levenshtein distance matrix.permutations- The number of permutations.
-
decodeShortestEditScript
Reads the predicted Shortest Edit Script (SES) by a lemmatizer model and applies the permutations to obtain the lemma from thewordForm.- Parameters:
wordForm- The wordForm as input.permutations- The permutations predicted by the lemmatizer model.- Returns:
- The decoded lemma.
-
getShortestEditScript
- Parameters:
wordForm- The word as input.lemma- The target lemma.- Returns:
- Retrieves the Shortest Edit Script (SES) required to go from a word to a lemma.
-