|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.stanford.nlp.util.StringUtils
public class StringUtils
StringUtils is a class for random String things, including output formatting and command line argument parsing.
Field Summary | |
---|---|
static java.lang.String[] |
EMPTY_STRING_ARRAY
|
Method Summary | ||
---|---|---|
static java.util.Map<java.lang.String,java.lang.String[]> |
argsToMap(java.lang.String[] args)
Parses command line arguments into a Map. |
|
static java.util.Map<java.lang.String,java.lang.String[]> |
argsToMap(java.lang.String[] args,
java.util.Map<java.lang.String,java.lang.Integer> flagsToNumArgs)
Parses command line arguments into a Map. |
|
static java.util.Properties |
argsToProperties(java.lang.String[] args)
In this version each flag has zero or one argument. |
|
static java.util.Properties |
argsToProperties(java.lang.String[] args,
java.util.Map<java.lang.String,java.lang.Integer> flagsToNumArgs)
Analogous to argsToMap(java.lang.String[]) . |
|
static java.lang.String |
capitalize(java.lang.String s)
Uppercases the first character of a string. |
|
static java.lang.String |
checkRequiredProperties(java.util.Properties props,
java.lang.String... requiredProps)
If any of the given list of properties are not found, returns the name of that property. |
|
static java.lang.String |
chomp(java.lang.Object o)
Returns the result of calling toString() on the supplied Object, but with any trailing '\n' removed. |
|
static java.lang.String |
chomp(java.lang.String s)
Returns the supplied string with any trailing '\n' removed. |
|
static
|
columnStringToObject(java.lang.Class<?> objClass,
java.lang.String str,
java.util.regex.Pattern delimiterPattern,
java.lang.String[] fieldNames)
Converts a tab delimited string into an object with given fields Requires the object has public access for the specified fields |
|
static
|
columnStringToObject(java.lang.Class objClass,
java.lang.String str,
java.lang.String delimiterRegex,
java.lang.String[] fieldNames)
Converts a tab delimited string into an object with given fields Requires the object has setXxx functions for the specified fields |
|
static boolean |
containsIgnoreCase(java.util.Collection<java.lang.String> c,
java.lang.String s)
Convenience method: a case-insensitive variant of Collection.contains |
|
static int |
editDistance(java.lang.String s,
java.lang.String t)
Computes the Levenshtein (edit) distance of the two given Strings. |
|
static java.lang.String |
escapeString(java.lang.String s,
char[] charsToEscape,
char escapeChar)
|
|
static java.lang.String |
fileNameClean(java.lang.String s)
Returns a "clean" version of the given filename in which spaces have been converted to dashes and all non-alphanumeric chars are underscores. |
|
static boolean |
find(java.lang.String str,
java.lang.String regex)
Say whether this regular expression can be found inside this String. |
|
static java.lang.String |
getBaseName(java.lang.String fileName)
Strip directory from filename. |
|
static java.lang.String |
getBaseName(java.lang.String fileName,
java.lang.String suffix)
Strip directory and suffix from filename. |
|
static java.lang.String |
getNotNullString(java.lang.String s)
|
|
static java.lang.String |
getShortClassName(java.lang.Object o)
Returns a short class name for an object. |
|
static boolean |
isAcronym(java.lang.String s)
Given a String the method uses Regex to check if the String looks like an acronym |
|
static boolean |
isAlpha(java.lang.String s)
Given a String the method uses Regex to check if the String only contains alphabet characters |
|
static boolean |
isAlphanumeric(java.lang.String s)
Given a String the method uses Regex to check if the String only contains alphanumeric characters |
|
static boolean |
isCapitalized(java.lang.String s)
Check if a string begins with an uppercase. |
|
static boolean |
isNumeric(java.lang.String s)
Given a String the method uses Regex to check if the String only contains numeric characters |
|
static boolean |
isPunct(java.lang.String s)
Given a String the method uses Regex to check if the String only contains punctuation characters |
|
static java.lang.String |
join(java.lang.Iterable<?> l)
Joins elems with a space. |
|
static
|
join(java.lang.Iterable<X> l,
java.lang.String glue)
Joins each elem in the Collection with the given glue. |
|
static java.lang.String |
join(java.lang.Object[] elements)
Joins elements with a space. |
|
static java.lang.String |
join(java.lang.Object[] elements,
java.lang.String glue)
Joins each elem in the array with the given glue. |
|
static java.lang.String |
joinWithOriginalWhiteSpace(java.util.List<CoreLabel> tokens)
Joins all the tokens together (more or less) according to their original whitespace. |
|
static java.lang.String |
joinWords(java.lang.Iterable<? extends HasWord> l,
java.lang.String glue)
|
|
static int |
longestCommonContiguousSubstring(java.lang.String s,
java.lang.String t)
Computes the longest common contiguous substring of s and t. |
|
static int |
longestCommonSubstring(java.lang.String s,
java.lang.String t)
Computes the longest common substring of s and t. |
|
static boolean |
lookingAt(java.lang.String str,
java.lang.String regex)
Say whether this regular expression can be found at the beginning of this String. |
|
static void |
main(java.lang.String[] args)
Tests the string edit distance function. |
|
static java.lang.String |
makeAsciiTable(java.lang.Object[][] table,
java.lang.Object[] rowLabels,
java.lang.Object[] colLabels,
int padLeft,
int padRight,
boolean tsv)
Returns an text table containing the matrix of Strings passed in. |
|
static java.lang.String |
makeHTMLTable(java.lang.String[][] table,
java.lang.String[] rowLabels,
java.lang.String[] colLabels)
Returns an HTML table containing the matrix of Strings passed in. |
|
static java.lang.String[] |
mapStringToArray(java.lang.String map)
Takes a string of the form "x1=y1,x2=y2,..." such that each y is an integer and each x is a key. |
|
static java.util.Map<java.lang.String,java.lang.String> |
mapStringToMap(java.lang.String map)
Takes a string of the form "x1=y1,x2=y2,..." and returns Map |
|
static boolean |
matches(java.lang.String str,
java.lang.String regex)
Say whether this regular expression matches this String. |
|
static int |
nthIndex(java.lang.String s,
char ch,
int n)
Returns the index of the nth occurrence of ch in s, or -1 if there are less than n occurrences of ch. |
|
static java.lang.String |
objectToColumnString(java.lang.Object object,
java.lang.String delimiter,
java.lang.String[] fieldNames)
Converts an object into a tab delimited string with given fields Requires the object has public access for the specified fields |
|
static java.lang.String |
pad(java.lang.Object obj,
int totalChars)
Pads the toString value of the given Object. |
|
static java.lang.String |
pad(java.lang.String str,
int totalChars)
Return a String of length a minimum of totalChars characters by padding the input String str at the right end with spaces. |
|
static java.lang.String |
padLeft(double d,
int totalChars)
|
|
static java.lang.String |
padLeft(int i,
int totalChars)
|
|
static java.lang.String |
padLeft(java.lang.Object obj,
int totalChars)
|
|
static java.lang.String |
padLeft(java.lang.String str,
int totalChars)
Pads the given String to the left with spaces to ensure that it's at least totalChars long. |
|
static java.lang.String |
padLeft(java.lang.String str,
int totalChars,
char ch)
Pads the given String to the left with the given character to ensure that it's at least totalChars long. |
|
static java.lang.String |
padLeftOrTrim(java.lang.String str,
int num)
Pad or trim so as to produce a string of exactly a certain length. |
|
static java.lang.String |
padOrTrim(java.lang.Object obj,
int totalChars)
Pad or trim the toString value of the given Object. |
|
static java.lang.String |
padOrTrim(java.lang.String str,
int num)
Pad or trim so as to produce a string of exactly a certain length. |
|
static java.util.Map<java.lang.String,java.lang.String> |
parseCommandLineArguments(java.lang.String[] args)
A simpler form of command line argument parsing. |
|
static java.util.Map<java.lang.String,java.lang.Object> |
parseCommandLineArguments(java.lang.String[] args,
boolean parseNumbers)
A simpler form of command line argument parsing. |
|
static java.lang.String |
pennPOSToWordnetPOS(java.lang.String s)
Computes the WordNet 2.0 POS tag corresponding to the PTB POS tag s. |
|
static void |
printErrInvocationString(java.lang.String cls,
java.lang.String[] args)
|
|
static void |
printStringOneCharPerLine(java.lang.String s)
|
|
static void |
printToFile(java.io.File file,
java.lang.String message)
Prints to a file. |
|
static void |
printToFile(java.io.File file,
java.lang.String message,
boolean append)
Prints to a file. |
|
static void |
printToFile(java.io.File file,
java.lang.String message,
boolean append,
boolean printLn,
java.lang.String encoding)
Prints to a file. |
|
static void |
printToFile(java.lang.String filename,
java.lang.String message)
Prints to a file. |
|
static void |
printToFile(java.lang.String filename,
java.lang.String message,
boolean append)
Prints to a file. |
|
static void |
printToFileLn(java.io.File file,
java.lang.String message,
boolean append)
Prints to a file. |
|
static void |
printToFileLn(java.lang.String filename,
java.lang.String message,
boolean append)
Prints to a file. |
|
static java.util.Properties |
propFileToProperties(java.lang.String filename)
This method reads in properties listed in a file in the format prop=value, one property per line. |
|
static java.util.List<java.util.regex.Pattern> |
regexesToPatterns(java.lang.Iterable<java.lang.String> regexes)
|
|
static java.util.List<java.lang.String> |
regexGroups(java.util.regex.Pattern regex,
java.lang.String str)
Given a pattern and a string, returns a list with the values of the captured groups in the pattern. |
|
static java.lang.String |
repeat(char ch,
int times)
|
|
static java.lang.String |
repeat(java.lang.String s,
int times)
|
|
static java.lang.String |
searchAndReplace(java.lang.String text,
java.lang.String from,
java.lang.String to)
|
|
static java.util.List<java.lang.String> |
split(java.lang.String s)
Splits on whitespace (\\s+). |
|
static java.util.List<java.lang.String> |
split(java.lang.String str,
java.lang.String regex)
Splits the given string using the given regex as delimiters. |
|
static java.lang.String[] |
splitOnCharWithQuoting(java.lang.String s,
char splitChar,
char quoteChar,
char escapeChar)
This function splits the String s into multiple Strings using the splitChar. |
|
static java.util.Properties |
stringToProperties(java.lang.String str)
This method converts a comma-separated String (with whitespace optionally allowed after the comma) representing properties to a Properties object. |
|
static java.util.Properties |
stringToProperties(java.lang.String str,
java.util.Properties props)
This method updates a Properties object based on a comma-separated String (with whitespace optionally allowed after the comma) representing properties to a Properties object. |
|
static java.util.Set<java.lang.String> |
stringToSet(java.lang.String str,
java.lang.String delimiter)
|
|
static java.lang.String |
stripNonAlphaNumerics(java.lang.String orig)
|
|
static java.lang.String |
stripSGML(java.lang.String orig)
|
|
static java.lang.String |
toAscii(java.lang.String s)
|
|
static java.lang.String |
toCSVString(java.lang.String[] fields)
|
|
static java.lang.String |
toInvocationString(java.lang.String cls,
java.lang.String[] args)
|
|
static java.lang.String |
tr(java.lang.String input,
java.lang.String from,
java.lang.String to)
Swap any occurrences of any characters in the from String in the input String with the corresponding character from the to String. |
|
static java.lang.String |
trim(java.lang.Object obj,
int maxWidth)
|
|
static java.lang.String |
trim(java.lang.String s,
int maxWidth)
Returns s if it's at most maxWidth chars, otherwise chops right side to fit. |
|
static java.lang.String |
truncate(int n,
int smallestDigit,
int biggestDigit)
This returns a string from decimal digit smallestDigit to decimal digit biggest digit. |
|
static java.util.List<java.lang.String> |
valueSplit(java.lang.String str,
java.lang.String valueRegex,
java.lang.String separatorRegex)
Split a string into tokens. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String[] EMPTY_STRING_ARRAY
Method Detail |
---|
public static boolean find(java.lang.String str, java.lang.String regex)
str
- String to search for match inregex
- String to compile as the regular expression
public static boolean containsIgnoreCase(java.util.Collection<java.lang.String> c, java.lang.String s)
c
- Collection<String>s
- String
public static boolean lookingAt(java.lang.String str, java.lang.String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expression
public static java.lang.String[] mapStringToArray(java.lang.String map)
map
- A string of the form "x1=y1,x2=y2,..." such
that each y is an integer and each x is a key.
public static java.util.Map<java.lang.String,java.lang.String> mapStringToMap(java.lang.String map)
map
- A string of the form "x1=y1,x2=y2,..."
public static java.util.List<java.util.regex.Pattern> regexesToPatterns(java.lang.Iterable<java.lang.String> regexes)
public static java.util.List<java.lang.String> regexGroups(java.util.regex.Pattern regex, java.lang.String str)
public static boolean matches(java.lang.String str, java.lang.String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expression
public static java.util.Set<java.lang.String> stringToSet(java.lang.String str, java.lang.String delimiter)
public static java.lang.String joinWords(java.lang.Iterable<? extends HasWord> l, java.lang.String glue)
public static java.lang.String joinWithOriginalWhiteSpace(java.util.List<CoreLabel> tokens)
tokens
- list of tokens which implement HasOffset
and HasWord
public static <X> java.lang.String join(java.lang.Iterable<X> l, java.lang.String glue)
Collection
with the given glue.
For example, given a list of Integers
, you can create
a comma-separated list by calling join(numbers, ", ")
.
public static java.lang.String join(java.lang.Object[] elements, java.lang.String glue)
join(numbers, ", ")
.
public static java.lang.String join(java.lang.Iterable<?> l)
public static java.lang.String join(java.lang.Object[] elements)
public static java.util.List<java.lang.String> split(java.lang.String s)
s
- String to split
public static java.util.List<java.lang.String> split(java.lang.String str, java.lang.String regex)
str
- String to split upregex
- String to compile as the regular expression
public static java.util.List<java.lang.String> valueSplit(java.lang.String str, java.lang.String valueRegex, java.lang.String separatorRegex)
str
- The String to splitvalueRegex
- Must match a token. You may wish to let it match the empty StringseparatorRegex
- Must match a separator
java.lang.IllegalArgumentException
- if str cannot be tokenized by the two regexpublic static java.lang.String pad(java.lang.String str, int totalChars)
public static java.lang.String pad(java.lang.Object obj, int totalChars)
public static java.lang.String padOrTrim(java.lang.String str, int num)
str
- The String to be padded or truncatednum
- The desired lengthpublic static java.lang.String padLeftOrTrim(java.lang.String str, int num)
str
- The String to be padded or truncatednum
- The desired lengthpublic static java.lang.String padOrTrim(java.lang.Object obj, int totalChars)
public static java.lang.String padLeft(java.lang.String str, int totalChars, char ch)
public static java.lang.String padLeft(java.lang.String str, int totalChars)
public static java.lang.String padLeft(java.lang.Object obj, int totalChars)
public static java.lang.String padLeft(int i, int totalChars)
public static java.lang.String padLeft(double d, int totalChars)
public static java.lang.String trim(java.lang.String s, int maxWidth)
public static java.lang.String trim(java.lang.Object obj, int maxWidth)
public static java.lang.String repeat(java.lang.String s, int times)
public static java.lang.String repeat(char ch, int times)
public static java.lang.String fileNameClean(java.lang.String s)
public static int nthIndex(java.lang.String s, char ch, int n)
public static java.lang.String truncate(int n, int smallestDigit, int biggestDigit)
public static java.util.Map<java.lang.String,java.lang.String[]> argsToMap(java.lang.String[] args)
String
[] containing
the optional arguments (if present). The non-flag values not
captured as flag arguments are collected into a String[] array
and returned as the value of null
in the Map. In
this invocation, flags cannot take arguments, so all the String
array values other than the value for null
will be zero-length.
args
- A command-line arguments array
Map
of flag names to flag argument String
arrays.public static java.util.Map<java.lang.String,java.lang.String[]> argsToMap(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Integer> flagsToNumArgs)
String
[] containing
the optional arguments (if present). The non-flag values not
captured as flag arguments are collected into a String[] array
and returned as the value of null
in the Map. In
this invocation, the maximum number of arguments for each flag
can be specified as an Integer
value of the appropriate
flag key in the flagsToNumArgs
Map
argument. (By default, flags cannot take arguments.)
Example of usage:
Map flagsToNumArgs = new HashMap();
flagsToNumArgs.put("-x",new Integer(2));
flagsToNumArgs.put("-d",new Integer(1));
Map result = argsToMap(args,flagsToNumArgs);
If a given flag appears more than once, the extra args are appended to
the String[] value for that flag.
args
- the argument array to be parsedflagsToNumArgs
- a Map
of flag names to Integer
values specifying the maximum number of
allowed arguments for that flag (default 0).
Map
of flag names to flag argument String
arrays.public static java.util.Properties argsToProperties(java.lang.String[] args)
argsToProperties(String[], Map)
for full documentation.
args
- Command line arguments
public static java.util.Properties argsToProperties(java.lang.String[] args, java.util.Map<java.lang.String,java.lang.Integer> flagsToNumArgs)
argsToMap(java.lang.String[])
. However, there are several key differences between this method and argsToMap(java.lang.String[])
:
args
- Command line argumentsflagsToNumArgs
- Map of how many arguments flags should have. The keys are without the minus signs.
public static java.util.Properties propFileToProperties(java.lang.String filename)
Properties.load(InputStream)
exists, I implemented this method to trim the lines,
something not implemented in the load()
method.
filename
- A properties file to read
public static java.util.Properties stringToProperties(java.lang.String str)
public static java.util.Properties stringToProperties(java.lang.String str, java.util.Properties props)
public static java.lang.String checkRequiredProperties(java.util.Properties props, java.lang.String... requiredProps)
public static void printToFile(java.io.File file, java.lang.String message, boolean append, boolean printLn, java.lang.String encoding)
append=true
, and overwrites if append=false
.
public static void printToFileLn(java.io.File file, java.lang.String message, boolean append)
append=true
, and overwrites if append=false
.
public static void printToFile(java.io.File file, java.lang.String message, boolean append)
append=true
, and overwrites if append=false
.
public static void printToFile(java.io.File file, java.lang.String message)
public static void printToFile(java.lang.String filename, java.lang.String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFileLn(java.lang.String filename, java.lang.String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFile(java.lang.String filename, java.lang.String message)
public static java.util.Map<java.lang.String,java.lang.String> parseCommandLineArguments(java.lang.String[] args)
public static java.util.Map<java.lang.String,java.lang.Object> parseCommandLineArguments(java.lang.String[] args, boolean parseNumbers)
public static java.lang.String stripNonAlphaNumerics(java.lang.String orig)
public static java.lang.String stripSGML(java.lang.String orig)
public static void printStringOneCharPerLine(java.lang.String s)
public static java.lang.String escapeString(java.lang.String s, char[] charsToEscape, char escapeChar)
public static java.lang.String[] splitOnCharWithQuoting(java.lang.String s, char splitChar, char quoteChar, char escapeChar)
s
- The String to splitsplitChar
- The character to split onquoteChar
- The character to quote items withescapeChar
- The character to escape the quoteChar with
public static int longestCommonSubstring(java.lang.String s, java.lang.String t)
public static int longestCommonContiguousSubstring(java.lang.String s, java.lang.String t)
public static int editDistance(java.lang.String s, java.lang.String t)
public static java.lang.String pennPOSToWordnetPOS(java.lang.String s)
s
- a Penn TreeBank POS tag.public static java.lang.String getShortClassName(java.lang.Object o)
ArrayList
public static <T> T columnStringToObject(java.lang.Class objClass, java.lang.String str, java.lang.String delimiterRegex, java.lang.String[] fieldNames) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException
T
- type to returnobjClass
- Class of object to be createdstr
- string to convertdelimiterRegex
- delimiter regular expressionfieldNames
- fieldnames
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.NoSuchFieldException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
public static <T> T columnStringToObject(java.lang.Class<?> objClass, java.lang.String str, java.util.regex.Pattern delimiterPattern, java.lang.String[] fieldNames) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.NoSuchFieldException, java.lang.reflect.InvocationTargetException
T
- type to returnobjClass
- Class of object to be createdstr
- string to convertdelimiterPattern
- delimiterfieldNames
- fieldnames
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.NoSuchFieldException
java.lang.reflect.InvocationTargetException
public static java.lang.String objectToColumnString(java.lang.Object object, java.lang.String delimiter, java.lang.String[] fieldNames) throws java.lang.IllegalAccessException, java.lang.NoSuchFieldException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException
object
- Object to convertdelimiter
- delimiterfieldNames
- fieldnames
java.lang.IllegalAccessException
java.lang.NoSuchFieldException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
public static java.lang.String capitalize(java.lang.String s)
s
- a string to capitalize
public static boolean isCapitalized(java.lang.String s)
s
- a string
public static java.lang.String searchAndReplace(java.lang.String text, java.lang.String from, java.lang.String to)
public static java.lang.String makeHTMLTable(java.lang.String[][] table, java.lang.String[] rowLabels, java.lang.String[] colLabels)
public static java.lang.String makeAsciiTable(java.lang.Object[][] table, java.lang.Object[] rowLabels, java.lang.Object[] colLabels, int padLeft, int padRight, boolean tsv)
public static void main(java.lang.String[] args)
public static java.lang.String toAscii(java.lang.String s)
public static java.lang.String toCSVString(java.lang.String[] fields)
public static java.lang.String tr(java.lang.String input, java.lang.String from, java.lang.String to)
Note: This is now optimized to not allocate any objects if the input is returned unchanged.
public static java.lang.String chomp(java.lang.String s)
public static java.lang.String chomp(java.lang.Object o)
public static void printErrInvocationString(java.lang.String cls, java.lang.String[] args)
public static java.lang.String toInvocationString(java.lang.String cls, java.lang.String[] args)
public static java.lang.String getBaseName(java.lang.String fileName)
getBaseName("/u/wcmac/foo.txt") ==> "foo.txt"
public static java.lang.String getBaseName(java.lang.String fileName, java.lang.String suffix)
getBaseName("/u/wcmac/foo.txt", "") ==> "foo.txt"
getBaseName("/u/wcmac/foo.txt", ".txt") ==> "foo"
getBaseName("/u/wcmac/foo.txt", ".pdf") ==> "foo.txt"
public static boolean isAlpha(java.lang.String s)
s
- a String to check using regex
public static boolean isNumeric(java.lang.String s)
s
- a String to check using regex
public static boolean isAlphanumeric(java.lang.String s)
s
- a String to check using regex
public static boolean isPunct(java.lang.String s)
s
- a String to check using regex
public static boolean isAcronym(java.lang.String s)
s
- a String to check using regex
public static java.lang.String getNotNullString(java.lang.String s)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |