hultig.util
Class Toolkit

java.lang.Object
  extended by hultig.util.Toolkit

public class Toolkit
extends Object

The Toolkit class contains various static methods being used within this library. It gathers a set of auxiliary functions, ranging from string manipulation to basic mathematical functions, that are used in a variety of classes, throughout the library.

University of Beira Interior (UBI)
Centre For Human Language Technology and Bioinformatics (HULTIG)


Field Summary
static PrintStream out
          A shortcut for the standard output.
 
Constructor Summary
Toolkit()
          Default constructor.
Toolkit(String encoding)
          Constructor with encoding definition.
 
Method Summary
static int countLines(String filename)
          Counts the number of lines in a given file.
static long countPattern(String pattern, String filename)
          Counts the occurrence of a regular expression in a given text file.
static int[] createRandomSample(int size, int max)
          Creates an ordered random sample of integers, between 0 and max, uniformly distributed.
static double dotProduct(double[] u, double[] v)
          Computes the dot product between two vectors, represented by two arrays.
static boolean execute(String command)
          Executes a specific command in the operating system.
static double Fmeasure(double precision, double recall)
          Computes the F-measure, for a given precision and recall.
static double Fmeasure(double precision, double recall, double beta)
          Computes the F-measure, for a given precision, recall, and beta parameter.
static void formatedWrite(String s, int nmx, String sleft, String sright)
          Outputs a string with a maximum number of columns.
static String joinStrings(String[] vs)
          Concatenates the strings contained in an array, joining them through the space character, as a string separator.
static String joinStrings(String[] vs, char separator)
          Concatenates the strings contained in an array, joining them through a given separator character.
static double log(double x, double base)
          Computes the log value in a given base.
static double log2(double x)
          Gives the logarithm in base 2.
static double maximum(double a, double b, double c)
          The maximum, among three values.
static int maximum(int a, int b, int c)
          The maximum, among three values.
static int minGreaterThan(int infimum, int[] v)
          Gives the smallest value from an array, which is greater than a given infimum value.
static float minimum(float a, float b, float c)
          The minimum, among three values.
static int minimum(int a, int b, int c)
          The minimum, among three values.
static String moment()
          Gives a string containing the current moment, in terms of year, month, day, hour, minute, and second.
static boolean nulity(Object... oset)
          Tests the nullity of an array of objects.
static String padLeft(String s, int n)
          Transforms a string into one with a given length, by filling its left hand side with spaces.
static String padRight(String s, int n)
          Transforms a string into one with a given length, by filling its right hand side with spaces.
static void pause()
          Makes a pause in the standard output flow, waiting for the user to hit the RETURN key.
static void pause(String message)
          Makes a pause in the standard output flow, waiting for the user to hit the RETURN key, and prompting him with a message.
static void print(double[] v)
          Outputs an array of double values.
static void print(int[] v)
          Outputs an array of integer values.
static void print(String s)
          A shortcut for the standard print.
static void print(String[] v)
          Outputs a static array of strings.
static void print(String[] v, boolean indexes)
          Outputs a static array of strings.
static void printf(String format, Object... args)
          A shortcut for the printf function.
static void printHistogram(int[] v, int maxbars)
          Outputs the histogram corresponding to a given array of integers.
static void println(String s)
          A shortcut for the standard println.
static void printVector(String prefix, int[] v)
          Outputs an array of integers.
static double probGoodText(String s)
          Computes the likelihood to have text, for a given string.
static String[] readFile(String filename, String encoding, int limit)
          Reads a certain number of lines from a given file, through a specific encoding format.
static String readLn()
          A static method for reading a string from the standard input.
static void sclean(String[] vs, String chset)
          Cleans all strings in a given array, by eliminating any character from a given set.
static String sclean(String s, String chset)
          Cleans a string by eliminating any character contained in a givens set of characters.
static String sline(char c, int size)
          Constructs a string of repeated characters, with a given size.
static String sline(int size)
          Generates a '-' string with a given size.
static String sprintf(String format, Object... args)
          The sprintf function, similar to the one existing in the C language.
static double sqr(double x)
          Gives the square of a value.
static String underscore(int size)
          Generates a string of underscores, with a given length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

out

public static PrintStream out
A shortcut for the standard output.

Constructor Detail

Toolkit

public Toolkit()
Default constructor.


Toolkit

public Toolkit(String encoding)
Constructor with encoding definition.

Method Detail

readLn

public static String readLn()
A static method for reading a string from the standard input.

Returns:
The obtained string.

pause

public static void pause()
Makes a pause in the standard output flow, waiting for the user to hit the RETURN key.


pause

public static void pause(String message)
Makes a pause in the standard output flow, waiting for the user to hit the RETURN key, and prompting him with a message.

Parameters:
message - The pause prompt message.

print

public static void print(String s)
A shortcut for the standard print.

Parameters:
s - The string to be written.

println

public static void println(String s)
A shortcut for the standard println.

Parameters:
s - The string to be written.

sprintf

public static String sprintf(String format,
                             Object... args)
The sprintf function, similar to the one existing in the C language.

Parameters:
format - The format string.
args - The array of objects to be inserted in the corresponding positions, defined by the format string.
Returns:
The composed string by combining the format and the objects.

printf

public static void printf(String format,
                          Object... args)
A shortcut for the printf function.

Parameters:
format - The format string.
args - The array of objects to be inserted in the corresponding positions, defined by the format string.

print

public static void print(String[] v,
                         boolean indexes)
Outputs a static array of strings.

Parameters:
v - String[] The array of strings to be printed.
indexes - If true, array indexes will be printed.

print

public static void print(String[] v)
Outputs a static array of strings.

Parameters:
v - The array of strings to be printed.

print

public static void print(int[] v)
Outputs an array of integer values.

Parameters:
v - The array to be printed.

print

public static void print(double[] v)
Outputs an array of double values.

Parameters:
v - The array to be printed.

printVector

public static void printVector(String prefix,
                               int[] v)
Outputs an array of integers.

Parameters:
prefix - The prefix string.
v - The array to be printed.

sline

public static String sline(char c,
                           int size)
Constructs a string of repeated characters, with a given size.

Parameters:
c - The string character.
size - The string length.
Returns:
The constructed string.

sline

public static String sline(int size)
Generates a '-' string with a given size.

Parameters:
size - The string length.
Returns:
The constructed string.

underscore

public static String underscore(int size)
Generates a string of underscores, with a given length.

Parameters:
size - The length of the generated string
Returns:
The generated string.

padLeft

public static String padLeft(String s,
                             int n)
Transforms a string into one with a given length, by filling its left hand side with spaces.

Parameters:
s - The string to be transformed.
n - The string's output length.
Returns:
The transformed string.

padRight

public static String padRight(String s,
                              int n)
Transforms a string into one with a given length, by filling its right hand side with spaces.

Parameters:
s - The string to be transformed.
n - The string's output length.
Returns:
The transformed string.

joinStrings

public static String joinStrings(String[] vs)
Concatenates the strings contained in an array, joining them through the space character, as a string separator.

Parameters:
vs - The array of strings
Returns:
The concatenated string.

joinStrings

public static String joinStrings(String[] vs,
                                 char separator)
Concatenates the strings contained in an array, joining them through a given separator character.

Parameters:
vs - The array of strings.
separator - The separator.
Returns:
The concatenated string.

sclean

public static String sclean(String s,
                            String chset)
Cleans a string by eliminating any character contained in a givens set of characters.

Parameters:
s - The string to be cleaned.
chset - The set of characters to eliminate.
Returns:
The cleaned string.

sclean

public static void sclean(String[] vs,
                          String chset)
Cleans all strings in a given array, by eliminating any character from a given set.

Parameters:
vs - The array of strings to be cleaned.
chset - The character set to eliminate.

moment

public static String moment()
Gives a string containing the current moment, in terms of year, month, day, hour, minute, and second. For example: "2011/06/02 09:11:12".

Returns:
The moment string.

formatedWrite

public static void formatedWrite(String s,
                                 int nmx,
                                 String sleft,
                                 String sright)
Outputs a string with a maximum number of columns. If it is a textual string, the division is made on spaces, otherwise the division is made strictly at the given length.

Parameters:
s - The string to be written
nmx - The maximum number of columns.
sleft - The left context string, possibly a sequence of spaces.
sright - The right context string.

countPattern

public static long countPattern(String pattern,
                                String filename)
Counts the occurrence of a regular expression in a given text file.

Parameters:
pattern - The regular expression to be counted.
filename - The name of the text file.
Returns:
The number of counts.

readFile

public static String[] readFile(String filename,
                                String encoding,
                                int limit)
Reads a certain number of lines from a given file, through a specific encoding format.

Parameters:
filename - The name of the file to be read.
encoding - The encoding string.
limit - The number of lines.
Returns:
The array of strings read.

countLines

public static int countLines(String filename)
Counts the number of lines in a given file.

Parameters:
filename - The file name.
Returns:
The number of lines.

createRandomSample

public static int[] createRandomSample(int size,
                                       int max)
Creates an ordered random sample of integers, between 0 and max, uniformly distributed.

Parameters:
size - int The sample size.
max - int The sample maximum value.
Returns:
int[] The generated sample.

execute

public static boolean execute(String command)
Executes a specific command in the operating system.

Parameters:
command - The command string to be executed.

nulity

public static boolean nulity(Object... oset)
Tests the nullity of an array of objects.

Parameters:
oset - The variable array of objects.
Returns:
Is true if one object is null.

minimum

public static int minimum(int a,
                          int b,
                          int c)
The minimum, among three values.

Parameters:
a - value
b - value
c - value
Returns:
The minimum.

minimum

public static float minimum(float a,
                            float b,
                            float c)
The minimum, among three values.

Parameters:
a - value
b - value
c - value
Returns:
The minimum.

maximum

public static int maximum(int a,
                          int b,
                          int c)
The maximum, among three values.

Parameters:
a - value
b - value
c - value
Returns:
The minimum.

maximum

public static double maximum(double a,
                             double b,
                             double c)
The maximum, among three values.

Parameters:
a - value
b - value
c - value
Returns:
The minimum.

minGreaterThan

public static int minGreaterThan(int infimum,
                                 int[] v)
Gives the smallest value from an array, which is greater than a given infimum value.

Parameters:
infimum - The infimum value.
v - The array from which to extract the value.
Returns:
The searched value.

dotProduct

public static double dotProduct(double[] u,
                                double[] v)
Computes the dot product between two vectors, represented by two arrays.

Parameters:
u - Represents one vector.
v - Represents the other vector.
Returns:
The dot product value.

probGoodText

public static double probGoodText(String s)
Computes the likelihood to have text, for a given string. It is simply based on the number of letters and spaces contained in the string.

Parameters:
s - The string to be measured.
Returns:
A value in the unitary interval: [0,1].

sqr

public static double sqr(double x)
Gives the square of a value.

Parameters:
x - The value to be squared.
Returns:
The square.

log

public static double log(double x,
                         double base)
Computes the log value in a given base.

Parameters:
x - The input value.
base - The base value.
Returns:
The log value.

log2

public static double log2(double x)
Gives the logarithm in base 2.

Parameters:
x - The input value.
Returns:
The base 2 logarithm

Fmeasure

public static double Fmeasure(double precision,
                              double recall)
Computes the F-measure, for a given precision and recall.

Parameters:
precision - The precision value.
recall - the recall value.
Returns:
The F-measure value.

Fmeasure

public static double Fmeasure(double precision,
                              double recall,
                              double beta)
Computes the F-measure, for a given precision, recall, and beta parameter.

Parameters:
precision - The precision value;
recall - The recall value
beta - The beta parameter.
Returns:
The F-measure value.

printHistogram

public static void printHistogram(int[] v,
                                  int maxbars)
Outputs the histogram corresponding to a given array of integers. For each value, the corresponding number of bars printed is relative to a maximum value, provided as a parameter.

Parameters:
v - The array of values for the histogram.
maxbars - The maximum number of bars printed. That is, for the maximum value of maxbars bars will be printed.