|
|||||||||
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 String[] |
EMPTY_STRING_ARRAY
|
Method Summary | |
---|---|
static Map<String,String[]> |
argsToMap(String[] args)
Parses command line arguments into a Map. |
static Map<String,String[]> |
argsToMap(String[] args,
Map<String,Integer> flagsToNumArgs)
Parses command line arguments into a Map. |
static Properties |
argsToProperties(String[] args)
In this version each flag has zero or one argument. |
static Properties |
argsToProperties(String[] args,
Map<String,Integer> flagsToNumArgs)
Analagous to argsToMap(java.lang.String[]) . |
static String |
capitalize(String s)
Uppercases the first character of a string. |
static String |
chomp(Object o)
Returns the result of calling toString() on the supplied Object, but with any trailing '\n' removed. |
static String |
chomp(String s)
Returns the supplied string with any trailing '\n' removed. |
static int |
editDistance(String s,
String t)
Computes the Levenshtein (edit) distance of the two given Strings. |
static String |
escapeString(String s,
char[] charsToEscape,
char escapeChar)
|
static String |
fileNameClean(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(String str,
String regex)
Say whether this regular expression can be found inside this String. |
static String |
getBaseName(String fileName)
Strip directory from filename. |
static String |
getBaseName(String fileName,
String suffix)
Strip directory and suffix from filename. |
static String |
getShortClassName(Object o)
Returns a short class name for an object. |
static boolean |
isAcronym(String s)
Given a String the method uses Regex to check if the String looks like an acronym |
static boolean |
isAlpha(String s)
Given a String the method uses Regex to check if the String only contains alphabet characters |
static boolean |
isAlphanumeric(String s)
Given a String the method uses Regex to check if the String only contains alphanumeric characters |
static boolean |
isCapitalized(String s)
Check if a string begins with an uppercase. |
static boolean |
isNumeric(String s)
Given a String the method uses Regex to check if the String only contains numeric characters |
static boolean |
isPunct(String s)
Given a String the method uses Regex to check if the String only contains punctuation characters |
static String |
join(Iterable<?> l)
Joins elems with a space. |
static String |
join(Iterable<?> l,
String glue)
Joins each elem in the Collection with the given glue. |
static String |
join(Object[] elements)
Joins elems with a space. |
static String |
join(Object[] elements,
String glue)
Joins each elem in the array with the given glue. |
static int |
longestCommonContiguousSubstring(String s,
String t)
Computes the longest common contiguous substring of s and t. |
static int |
longestCommonSubstring(String s,
String t)
Computes the longest common substring of s and t. |
static boolean |
lookingAt(String str,
String regex)
Say whether this regular expression can be found at the beginning of this String. |
static void |
main(String[] args)
Tests the string edit distance function. |
static String |
makeAsciiTable(Object[][] table,
Object[] rowLabels,
Object[] colLabels,
int padLeft,
int padRight,
boolean tsv)
Returns an HTML table containing the matrix of Strings passed in. |
static String |
makeHTMLTable(String[][] table,
String[] rowLabels,
String[] colLabels)
Returns an HTML table containing the matrix of Strings passed in. |
static String[] |
mapStringToArray(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 boolean |
matches(String str,
String regex)
Say whether this regular expression matches this String. |
static int |
nthIndex(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 String |
pad(Object obj,
int totalChars)
Pads the toString value of the given Object. |
static String |
pad(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 String |
padLeft(double d,
int totalChars)
|
static String |
padLeft(int i,
int totalChars)
|
static String |
padLeft(Object obj,
int totalChars)
|
static String |
padLeft(String str,
int totalChars)
Pads the given String to the left with spaces to ensure that it's at least totalChars long. |
static String |
padLeft(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 String |
padLeftOrTrim(String str,
int num)
Pad or trim so as to produce a string of exactly a certain length. |
static String |
padOrTrim(Object obj,
int totalChars)
Pad or trim the toString value of the given Object. |
static String |
padOrTrim(String str,
int num)
Pad or trim so as to produce a string of exactly a certain length. |
static Map<String,String> |
parseCommandLineArguments(String[] args)
A simpler form of command line argument parsing. |
static Map<String,Object> |
parseCommandLineArguments(String[] args,
boolean parseNumbers)
A simpler form of command line argument parsing. |
static String |
pennPOSToWordnetPOS(String s)
Computes the WordNet 2.0 POS tag corresponding to the PTB POS tag s. |
static void |
printErrInvocationString(String cls,
String[] args)
|
static void |
printStringOneCharPerLine(String s)
|
static void |
printToFile(File file,
String message)
Prints to a file. |
static void |
printToFile(File file,
String message,
boolean append)
Prints to a file. |
static void |
printToFile(File file,
String message,
boolean append,
boolean printLn,
String encoding)
Prints to a file. |
static void |
printToFile(String filename,
String message)
Prints to a file. |
static void |
printToFile(String filename,
String message,
boolean append)
Prints to a file. |
static void |
printToFileLn(File file,
String message,
boolean append)
Prints to a file. |
static void |
printToFileLn(String filename,
String message,
boolean append)
Prints to a file. |
static String |
repeat(String s,
int times)
|
static String |
searchAndReplace(String text,
String from,
String to)
|
static String |
slurpFile(File file)
Returns all the text in the given File. |
static String |
slurpFile(String filename)
Returns all the text in the given file |
static String |
slurpFile(String filename,
String encoding)
Returns all the text in the given file with the given encoding. |
static String |
slurpFileNoExceptions(File file)
Returns all the text in the given File. |
static String |
slurpFileNoExceptions(String filename)
Returns all the text in the given File. |
static String |
slurpFileNoExceptions(String filename,
String encoding)
Returns all the text in the given file with the given encoding. |
static String |
slurpGBFile(String filename)
|
static String |
slurpGBFileNoExceptions(String filename)
|
static String |
slurpGBURL(URL u)
Returns all the text at the given URL. |
static String |
slurpGBURLNoExceptions(URL u)
Returns all the text at the given URL. |
static String |
slurpGZippedFile(File file)
Returns all the text in the given File. |
static String |
slurpGZippedFile(String filename)
Returns all the text in the given File. |
static String |
slurpReader(Reader reader)
Returns all the text from the given Reader. |
static String |
slurpURL(String path)
Returns all the text at the given URL. |
static String |
slurpURL(URL u)
Returns all the text at the given URL. |
static String |
slurpURL(URL u,
String encoding)
Returns all the text at the given URL. |
static String |
slurpURLNoExceptions(String path)
Returns all the text at the given URL. |
static String |
slurpURLNoExceptions(URL u)
Returns all the text at the given URL. |
static String |
slurpURLNoExceptions(URL u,
String encoding)
Returns all the text at the given URL. |
static List<String> |
split(String s)
Splits on whitespace (\\s+). |
static List<String> |
split(String str,
String regex)
Splits the given string using the given regex as delimiters. |
static String[] |
splitOnCharWithQuoting(String s,
char splitChar,
char quoteChar,
char escapeChar)
This function splits the String s into multiple Strings using the splitChar. |
static Properties |
stringToProperties(String str)
This method converts a comma-separated String (with whitespace optionally allowed after the comma) representing properties to a Properties object. |
static Properties |
stringToProperties(String str,
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 String |
stripNonAlphaNumerics(String orig)
|
static String |
stripSGML(String orig)
|
static String |
toAscii(String s)
|
static String |
toCSVString(String[] fields)
|
static String |
toInvocationString(String cls,
String[] args)
|
static String |
tr(String input,
String from,
String to)
Swap any occurances of any characters in the from String in the input String with the corresponding character from the to String. |
static String |
trim(Object obj,
int maxWidth)
|
static String |
trim(String s,
int maxWidth)
Returns s if it's at most maxWidth chars, otherwise chops right side to fit. |
static String |
truncate(int n,
int smallestDigit,
int biggestDigit)
This returns a string from decimal digit smallestDigit to decimal digit biggest digit. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String[] EMPTY_STRING_ARRAY
Method Detail |
---|
public static boolean find(String str, String regex)
str
- String to search for match inregex
- String to compile as the regular expression
public static boolean lookingAt(String str, String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expression
public static String[] mapStringToArray(String map)
map
-
public static boolean matches(String str, String regex)
str
- String to search for match at start ofregex
- String to compile as the regular expression
public static String slurpFile(File file) throws IOException
IOException
public static String slurpGZippedFile(String filename) throws IOException
IOException
public static String slurpGZippedFile(File file) throws IOException
IOException
public static String slurpGBFileNoExceptions(String filename)
public static String slurpFile(String filename, String encoding) throws IOException
IOException
public static String slurpFileNoExceptions(String filename, String encoding)
null
.
public static String slurpGBFile(String filename) throws IOException
IOException
public static String slurpReader(Reader reader)
public static String slurpFile(String filename) throws IOException
IOException
public static String slurpFileNoExceptions(File file)
null
.public static String slurpFileNoExceptions(String filename)
null
.public static String slurpGBURL(URL u) throws IOException
IOException
public static String slurpGBURLNoExceptions(URL u)
public static String slurpURLNoExceptions(URL u, String encoding)
public static String slurpURL(URL u, String encoding) throws IOException
IOException
public static String slurpURL(URL u) throws IOException
IOException
public static String slurpURLNoExceptions(URL u)
public static String slurpURL(String path) throws Exception
Exception
public static String slurpURLNoExceptions(String path)
null
.
public static String join(Iterable<?> l, 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 String join(Object[] elements, String glue)
public static String join(Iterable<?> l)
public static String join(Object[] elements)
public static List<String> split(String s)
s
- String to split
public static List<String> split(String str, String regex)
str
- String to split upregex
- String to compile as the regular expression
public static String pad(String str, int totalChars)
public static String pad(Object obj, int totalChars)
public static String padOrTrim(String str, int num)
str
- The String to be padded or truncatednum
- The desired lengthpublic static String padLeftOrTrim(String str, int num)
str
- The String to be padded or truncatednum
- The desired lengthpublic static String padOrTrim(Object obj, int totalChars)
public static String padLeft(String str, int totalChars, char ch)
public static String padLeft(String str, int totalChars)
public static String padLeft(Object obj, int totalChars)
public static String padLeft(int i, int totalChars)
public static String padLeft(double d, int totalChars)
public static String trim(String s, int maxWidth)
public static String trim(Object obj, int maxWidth)
public static String repeat(String s, int times)
public static String fileNameClean(String s)
public static int nthIndex(String s, char ch, int n)
public static String truncate(int n, int smallestDigit, int biggestDigit)
public static Map<String,String[]> argsToMap(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 Map<String,String[]> argsToMap(String[] args, Map<String,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 Properties argsToProperties(String[] args)
argsToProperties(String[], Map)
for full documentation.
args
- Command line arguments
public static Properties argsToProperties(String[] args, Map<String,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 Properties stringToProperties(String str)
public static Properties stringToProperties(String str, Properties props)
public static void printToFile(File file, String message, boolean append, boolean printLn, String encoding)
append=true
, and overwrites if append=false
.
public static void printToFileLn(File file, String message, boolean append)
append=true
, and overwrites if append=false
.
public static void printToFile(File file, String message, boolean append)
append=true
, and overwrites if append=false
.
public static void printToFile(File file, String message)
public static void printToFile(String filename, String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFileLn(String filename, String message, boolean append)
append=true
, and overwrites if append=false
public static void printToFile(String filename, String message)
public static Map<String,String> parseCommandLineArguments(String[] args)
args
-
public static Map<String,Object> parseCommandLineArguments(String[] args, boolean parseNumbers)
args
-
public static String stripNonAlphaNumerics(String orig)
public static String stripSGML(String orig)
public static void printStringOneCharPerLine(String s)
public static String escapeString(String s, char[] charsToEscape, char escapeChar)
public static String[] splitOnCharWithQuoting(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(String s, String t)
public static int longestCommonContiguousSubstring(String s, String t)
public static int editDistance(String s, String t)
public static String pennPOSToWordnetPOS(String s)
s
- a Penn TreeBank POS tag.public static String getShortClassName(Object o)
ArrayList
public static String capitalize(String s)
s
- a string to capitalize
public static boolean isCapitalized(String s)
s
- a string
public static String searchAndReplace(String text, String from, String to)
public static String makeHTMLTable(String[][] table, String[] rowLabels, String[] colLabels)
public static String makeAsciiTable(Object[][] table, Object[] rowLabels, Object[] colLabels, int padLeft, int padRight, boolean tsv)
public static void main(String[] args)
public static String toAscii(String s)
public static String toCSVString(String[] fields)
public static String tr(String input, String from, String to)
public static String chomp(String s)
public static String chomp(Object o)
public static void printErrInvocationString(String cls, String[] args)
public static String toInvocationString(String cls, String[] args)
public static String getBaseName(String fileName)
getBaseName("/u/wcmac/foo.txt") ==> "foo.txt"
public static String getBaseName(String fileName, 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(String s)
s
- a String to check using regex
public static boolean isNumeric(String s)
s
- a String to check using regex
public static boolean isAlphanumeric(String s)
s
- a String to check using regex
public static boolean isPunct(String s)
s
- a String to check using regex
public static boolean isAcronym(String s)
s
- a String to check using regex
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |