|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectweka.core.Utils
public final class Utils
Class implementing some simple utility methods.
Field Summary | |
---|---|
static double |
log2
The natural logarithm of 2. |
static double |
SMALL
The small deviation allowed in double comparisons. |
Constructor Summary | |
---|---|
Utils()
|
Method Summary | ||
---|---|---|
static java.lang.String |
arrayToString(java.lang.Object array)
Returns the given Array in a string representation. |
|
static java.lang.String |
backQuoteChars(java.lang.String string)
Converts carriage returns and new lines in a string into \r and \n. |
|
static
|
cast(java.lang.Object x)
Casting an object without "unchecked" compile-time warnings. |
|
static void |
checkForRemainingOptions(java.lang.String[] options)
Checks if the given array contains any non-empty options. |
|
static java.lang.String |
convertNewLines(java.lang.String string)
Converts carriage returns and new lines in a string into \r and \n. |
|
static java.io.File |
convertToRelativePath(java.io.File absolute)
Converts a File's absolute path to a path relative to the user (ie start) directory. |
|
static double |
correlation(double[] y1,
double[] y2,
int n)
Returns the correlation coefficient of two double vectors. |
|
static java.lang.String |
doubleToString(double value,
int afterDecimalPoint)
Rounds a double and converts it into String. |
|
static java.lang.String |
doubleToString(double value,
int width,
int afterDecimalPoint)
Rounds a double and converts it into a formatted decimal-justified String. |
|
static boolean |
eq(double a,
double b)
Tests if a is equal to b. |
|
static java.lang.Object |
forName(java.lang.Class<?> classType,
java.lang.String className,
java.lang.String[] options)
Creates a new instance of an object given it's class name and (optional) arguments to pass to it's setOptions method. |
|
static java.lang.Class |
getArrayClass(java.lang.Class c)
Returns the basic class of an array class (handles multi-dimensional arrays). |
|
static int |
getArrayDimensions(java.lang.Class array)
Returns the dimensions of the given array. |
|
static int |
getArrayDimensions(java.lang.Object array)
Returns the dimensions of the given array. |
|
static boolean |
getFlag(char flag,
java.lang.String[] options)
Checks if the given array contains the flag "-Char". |
|
static boolean |
getFlag(java.lang.String flag,
java.lang.String[] options)
Checks if the given array contains the flag "-String". |
|
static java.lang.String |
getOption(char flag,
java.lang.String[] options)
Gets an option indicated by a flag "-Char" from the given array of strings. |
|
static java.lang.String |
getOption(java.lang.String flag,
java.lang.String[] options)
Gets an option indicated by a flag "-String" from the given array of strings. |
|
static int |
getOptionPos(char flag,
java.lang.String[] options)
Gets the index of an option or flag indicated by a flag "-Char" from the given array of strings. |
|
static int |
getOptionPos(java.lang.String flag,
java.lang.String[] options)
Gets the index of an option or flag indicated by a flag "-String" from the given array of strings. |
|
java.lang.String |
getRevision()
Returns the revision string. |
|
static boolean |
gr(double a,
double b)
Tests if a is greater than b. |
|
static boolean |
grOrEq(double a,
double b)
Tests if a is greater or equal to b. |
|
static double |
info(int[] counts)
Computes entropy for an array of integers. |
|
static boolean |
isMissingValue(double val)
Tests if the given value codes "missing". |
|
static java.lang.String |
joinOptions(java.lang.String[] optionArray)
Joins all the options in an option array into a single string, as might be used on the command line. |
|
static double |
kthSmallestValue(double[] array,
int k)
Returns the kth-smallest value in the array |
|
static double |
kthSmallestValue(int[] array,
int k)
Returns the kth-smallest value in the array. |
|
static double |
log2(double a)
Returns the logarithm of a for base 2. |
|
static double[] |
logs2probs(double[] a)
Converts an array containing the natural logarithms of probabilities stored in a vector back into probabilities. |
|
static void |
main(java.lang.String[] ops)
Main method for testing this class. |
|
static int |
maxIndex(double[] doubles)
Returns index of maximum element in a given array of doubles. |
|
static int |
maxIndex(int[] ints)
Returns index of maximum element in a given array of integers. |
|
static double |
mean(double[] vector)
Computes the mean for an array of doubles. |
|
static int |
minIndex(double[] doubles)
Returns index of minimum element in a given array of doubles. |
|
static int |
minIndex(int[] ints)
Returns index of minimum element in a given array of integers. |
|
static double |
missingValue()
Returns the value used to code a missing value. |
|
static void |
normalize(double[] doubles)
Normalizes the doubles in the array by their sum. |
|
static void |
normalize(double[] doubles,
double sum)
Normalizes the doubles in the array using the given value. |
|
static java.lang.String |
padLeft(java.lang.String inString,
int length)
Pads a string to a specified length, inserting spaces on the left as required. |
|
static java.lang.String |
padRight(java.lang.String inString,
int length)
Pads a string to a specified length, inserting spaces on the right as required. |
|
static java.lang.String[] |
partitionOptions(java.lang.String[] options)
Returns the secondary set of options (if any) contained in the supplied options array. |
|
static int |
probRound(double value,
java.util.Random rand)
Rounds a double to the next nearest integer value in a probabilistic fashion (e.g. |
|
static double |
probToLogOdds(double prob)
Returns the log-odds for a given probabilitiy. |
|
static java.lang.String |
quote(java.lang.String string)
Quotes a string if it contains special characters. |
|
static java.util.Properties |
readProperties(java.lang.String resourceName)
Reads properties that inherit from three locations. |
|
static java.lang.String |
removeSubstring(java.lang.String inString,
java.lang.String substring)
Removes all occurrences of a string from another string. |
|
static java.lang.String |
replaceSubstring(java.lang.String inString,
java.lang.String subString,
java.lang.String replaceString)
Replaces with a new string, all occurrences of a string from another string. |
|
static java.lang.String |
revertNewLines(java.lang.String string)
Reverts \r and \n in a string into carriage returns and new lines. |
|
static int |
round(double value)
Rounds a double to the next nearest integer value. |
|
static double |
roundDouble(double value,
int afterDecimalPoint)
Rounds a double to the given number of decimal places. |
|
static boolean |
sm(double a,
double b)
Tests if a is smaller than b. |
|
static boolean |
smOrEq(double a,
double b)
Tests if a is smaller or equal to b. |
|
static int[] |
sort(double[] array)
Sorts a given array of doubles in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. |
|
static int[] |
sort(int[] array)
Sorts a given array of integers in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. |
|
static java.lang.String[] |
splitOptions(java.lang.String quotedOptionString)
Split up a string containing options into an array of strings, one for each option. |
|
static int[] |
stableSort(double[] array)
Sorts a given array of doubles in ascending order and returns an array of integers with the positions of the elements of the original array in the sorted array. |
|
static double |
sum(double[] doubles)
Computes the sum of the elements of an array of doubles. |
|
static int |
sum(int[] ints)
Computes the sum of the elements of an array of integers. |
|
static java.lang.String |
toCommandLine(java.lang.Object obj)
Generates a commandline of the given object. |
|
static java.lang.String |
unbackQuoteChars(java.lang.String string)
The inverse operation of backQuoteChars(). |
|
static java.lang.String |
unquote(java.lang.String string)
unquotes are previously quoted string (but only if necessary), i.e., it removes the single quotes around it. |
|
static double |
variance(double[] vector)
Computes the variance for an array of doubles. |
|
static double |
xlogx(int c)
Returns c*log2(c) for a given integer value c. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static double log2
public static double SMALL
Constructor Detail |
---|
public Utils()
Method Detail |
---|
public static boolean isMissingValue(double val)
val
- the value to be tested
public static double missingValue()
public static <T> T cast(java.lang.Object x)
public static java.util.Properties readProperties(java.lang.String resourceName) throws java.lang.Exception
resourceName
- the location of the resource that should be
loaded. e.g.: "weka/core/Utils.props". (The use of hardcoded
forward slashes here is OK - see
jdk1.1/docs/guide/misc/resources.html) This routine will also
look for the file (in this case) "Utils.props" in the users home
directory and the current directory.
java.lang.Exception
- if no default properties are defined, or if
an error occurs reading the properties files.public static final double correlation(double[] y1, double[] y2, int n)
y1
- double vector 1y2
- double vector 2n
- the length of two double vectors
public static java.lang.String removeSubstring(java.lang.String inString, java.lang.String substring)
inString
- the string to remove substrings from.substring
- the substring to remove.
public static java.lang.String replaceSubstring(java.lang.String inString, java.lang.String subString, java.lang.String replaceString)
inString
- the string to replace substrings in.subString
- the substring to replace.replaceString
- the replacement substring
public static java.lang.String padLeft(java.lang.String inString, int length)
inString
- the input stringlength
- the desired length of the output string
public static java.lang.String padRight(java.lang.String inString, int length)
inString
- the input stringlength
- the desired length of the output string
public static java.lang.String doubleToString(double value, int afterDecimalPoint)
value
- the double valueafterDecimalPoint
- the (maximum) number of digits permitted
after the decimal point
public static java.lang.String doubleToString(double value, int width, int afterDecimalPoint)
value
- the double valuewidth
- the width of the stringafterDecimalPoint
- the number of digits after the decimal point
public static java.lang.Class getArrayClass(java.lang.Class c)
c
- the array to inspect
public static int getArrayDimensions(java.lang.Class array)
array
- the array to determine the dimensions for
public static int getArrayDimensions(java.lang.Object array)
array
- the array to determine the dimensions for
public static java.lang.String arrayToString(java.lang.Object array)
array
- the array to return in a string representation
public static boolean eq(double a, double b)
a
- a doubleb
- a doublepublic static void checkForRemainingOptions(java.lang.String[] options) throws java.lang.Exception
options
- an array of strings
java.lang.Exception
- if there are any non-empty optionspublic static boolean getFlag(char flag, java.lang.String[] options) throws java.lang.Exception
flag
- the character indicating the flag.options
- the array of strings containing all the options.
java.lang.Exception
- if an illegal option was foundpublic static boolean getFlag(java.lang.String flag, java.lang.String[] options) throws java.lang.Exception
flag
- the String indicating the flag.options
- the array of strings containing all the options.
java.lang.Exception
- if an illegal option was foundpublic static java.lang.String getOption(char flag, java.lang.String[] options) throws java.lang.Exception
flag
- the character indicating the option.options
- the array of strings containing all the options.
java.lang.Exception
- if the option indicated by the flag can't be foundpublic static java.lang.String getOption(java.lang.String flag, java.lang.String[] options) throws java.lang.Exception
flag
- the String indicating the option.options
- the array of strings containing all the options.
java.lang.Exception
- if the option indicated by the flag can't be foundpublic static int getOptionPos(char flag, java.lang.String[] options)
flag
- the character indicating the option.options
- the array of strings containing all the options.
public static int getOptionPos(java.lang.String flag, java.lang.String[] options)
flag
- the String indicating the option.options
- the array of strings containing all the options.
public static java.lang.String quote(java.lang.String string)
string
- the string to be quoted
unquote(String)
public static java.lang.String unquote(java.lang.String string)
string
- the string to process
quote(String)
public static java.lang.String backQuoteChars(java.lang.String string)
string
- the string
unbackQuoteChars(String)
public static java.lang.String convertNewLines(java.lang.String string)
string
- the string
public static java.lang.String revertNewLines(java.lang.String string)
string
- the string
public static java.lang.String[] partitionOptions(java.lang.String[] options)
options
- the input array of options
public static java.lang.String unbackQuoteChars(java.lang.String string)
string
- the string
backQuoteChars(String)
public static java.lang.String[] splitOptions(java.lang.String quotedOptionString) throws java.lang.Exception
quotedOptionString
- the string containing the options
java.lang.Exception
- in case of an unterminated string, unknown character or
a parse errorpublic static java.lang.String joinOptions(java.lang.String[] optionArray)
optionArray
- the array of options
public static java.lang.Object forName(java.lang.Class<?> classType, java.lang.String className, java.lang.String[] options) throws java.lang.Exception
String classifierName = Utils.getOption('W', options);
Classifier c = (Classifier)Utils.forName(Classifier.class,
classifierName,
options);
setClassifier(c);
classType
- the class that the instantiated object should
be assignable to -- an exception is thrown if this is not the caseclassName
- the fully qualified class name of the objectoptions
- an array of options suitable for passing to setOptions. May
be null. Any options accepted by the object will be removed from the
array.
java.lang.Exception
- if the class name is invalid, or if the
class is not assignable to the desired class type, or the options
supplied are not acceptable to the objectpublic static java.lang.String toCommandLine(java.lang.Object obj)
obj
- the object to turn into a commandline
public static double info(int[] counts)
counts
- array of counts
public static boolean smOrEq(double a, double b)
a
- a doubleb
- a doublepublic static boolean grOrEq(double a, double b)
a
- a doubleb
- a doublepublic static boolean sm(double a, double b)
a
- a doubleb
- a doublepublic static boolean gr(double a, double b)
a
- a doubleb
- a doublepublic static double kthSmallestValue(int[] array, int k)
array
- the array of integersk
- the value of k
public static double kthSmallestValue(double[] array, int k)
array
- the array of doublek
- the value of k
public static double log2(double a)
a
- a double
public static int maxIndex(double[] doubles)
doubles
- the array of doubles
public static int maxIndex(int[] ints)
ints
- the array of integers
public static double mean(double[] vector)
vector
- the array
public static int minIndex(int[] ints)
ints
- the array of integers
public static int minIndex(double[] doubles)
doubles
- the array of doubles
public static void normalize(double[] doubles)
doubles
- the array of double
java.lang.IllegalArgumentException
- if sum is Zero or NaNpublic static void normalize(double[] doubles, double sum)
doubles
- the array of doublesum
- the value by which the doubles are to be normalized
java.lang.IllegalArgumentException
- if sum is zero or NaNpublic static double[] logs2probs(double[] a)
a
- an array holding the natural logarithms of the probabilities
public static double probToLogOdds(double prob)
prob
- the probabilitiy
public static int round(double value)
value
- the double value
public static int probRound(double value, java.util.Random rand)
value
- the double valuerand
- the random number generator
public static double roundDouble(double value, int afterDecimalPoint)
value
- the double valueafterDecimalPoint
- the number of digits after the decimal point
public static int[] sort(int[] array)
array
- this array is not changed by the method!
public static int[] sort(double[] array)
array
- this array is not changed by the method!
public static int[] stableSort(double[] array)
array
- this array is not changed by the method!
public static double variance(double[] vector)
vector
- the array
public static double sum(double[] doubles)
doubles
- the array of double
public static int sum(int[] ints)
ints
- the array of integers
public static double xlogx(int c)
c
- an integer value
public static java.io.File convertToRelativePath(java.io.File absolute) throws java.lang.Exception
absolute
- the File to convert to relative path
java.lang.Exception
- if the path cannot be constructedpublic java.lang.String getRevision()
getRevision
in interface RevisionHandler
public static void main(java.lang.String[] ops)
ops
- some dummy options
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |