marytts.util
Class MaryUtils

java.lang.Object
  extended by marytts.util.MaryUtils

public class MaryUtils
extends java.lang.Object

A collection of useful static little utility methods.

Author:
Marc Schröder

Field Summary
protected static char[] germanToAsciiLetterTable
           
static java.lang.String LOGPREFIX
           
protected static char[] punctuationTable
           
protected static char[] toGermanLetterTable
           
 
Constructor Summary
MaryUtils()
           
 
Method Summary
static java.util.Map ArrayToMap(java.lang.Object[] a)
          Create a map from an Object array which contains paired entries (key, value, key, value, ....).
static long availableMemory()
          Determine the amount of available memory.
static java.io.File createSelfDeletingTempFile(int lifetimeInSeconds)
          Create a temporary file that will be deleted after a specified number of seconds.
static java.lang.String getExtension(java.io.File f)
          Get the extension of a file.
static org.apache.log4j.Logger getLogger(java.lang.Class clazz)
          Provide a Logger object whose name is built from MaryUtils.LOGPREFIX and the given nameSuffix.
static org.apache.log4j.Logger getLogger(java.lang.String nameSuffix)
          Provide a Logger object whose name is built from MaryUtils.LOGPREFIX and the given nameSuffix.
static int getNumber(java.lang.String string)
          For a string containing a number as judged by isNumber(), return the numerical value, rounded to an integer.
static int getNumberDelta(java.lang.String string)
          For a string containing a number delta as judged by isNumberDelta(), return the numerical value, rounded to an integer.
static int getPercentageDelta(java.lang.String string)
          For a string containing a percentage delta as judged by isPercentageDelta(), return the numerical value, rounded to an integer.
static double getSemitonesDelta(java.lang.String string)
          For a string containing a semitones delta as judged by isSemitonesDelta(), return the numerical value, as a double.
static java.lang.String getThrowableAndCausesAsString(java.lang.Throwable t)
          Apply the toString() method recursively to this throwable and all its causes.
static int getUnsignedNumber(java.lang.String string)
          For a string containing an unsigned number as judged by isUnsignedNumber(), return the numerical value, rounded to an integer.
static double getUnsignedSemitones(java.lang.String string)
          For a string containing an unsigned semitones expression as judged by isUnsignedSemitones(), return the numerical value as a double.
static int indexOf(byte[] bytes, byte[] pattern)
          Check if bytes contains a subsequence identical with pattern, and return the index position.
static java.lang.Object instantiateObject(java.lang.String objectInitInfo)
          Instantiate an object by calling one of its constructors.
static boolean isLittleEndian()
           
static boolean isLog4jConfigured()
          Returns true if it appears that log4j have been previously configured.
static boolean isNumber(java.lang.String string)
          Tell whether the string contains a number.
static boolean isNumberDelta(java.lang.String string)
          Tell whether the string contains a positive or negative number delta, i.e., a number with an obligatory + or - sign.
static boolean isPercentageDelta(java.lang.String string)
          Tell whether the string contains a positive or negative percentage delta, i.e., a percentage number with an obligatory + or - sign.
static boolean isSemitonesDelta(java.lang.String string)
          Tell whether the string contains a positive or negative semitones delta, i.e., a semitones number with an obligatory + or - sign, such as "+3.2st" or "-13.2st".
static boolean isUnsignedNumber(java.lang.String string)
          Tell whether the string contains an unsigned number.
static boolean isUnsignedSemitones(java.lang.String string)
          Tell whether the string contains an unsigned semitones expression, such as "12st" or "5.4st".
static boolean isWindows()
           
static java.lang.String joinStrings(java.util.Collection<java.lang.String> strings)
          Join a collection of strings into a single String object, in the order indicated by the collection's iterator.
static java.lang.String locale2xmllang(java.util.Locale locale)
          Convert a locale into a string that is conform with XML's xml:lang attribute.
static boolean lowMemoryCondition()
          Verify if the java virtual machine is in a low memory condition.
static java.lang.String normaliseUnicodeLetters(java.lang.String unicodeText, java.util.Locale targetLocale)
          Normalise the Unicode text by mapping "exotic" letter characters to "standard" ones.
static java.lang.String normaliseUnicodePunctuation(java.lang.String unicodeText)
          Normalise the Unicode text by mapping "exotic" punctuation characters to "standard" ones.
static void plot(double[] x)
           
static void plot(double[] x, int startInd, int endInd)
           
static void plot(double[] x, int startInd, int endInd, java.lang.String strTitle)
           
static void plot(double[] x, int startInd, int endInd, java.lang.String strTitle, boolean bAutoClose)
           
static void plot(double[] xIn, int startInd, int endInd, java.lang.String strTitle, boolean bAutoClose, int milliSecondsToClose)
           
static void plot(double[] x, java.lang.String strTitle)
           
static void plot(float[] x)
           
static void plot(float[] x, int startInd, int endInd)
           
static void plot(float[] x, int startInd, int endInd, java.lang.String strTitle)
           
static void plot(float[] x, int startInd, int endInd, java.lang.String strTitle, boolean bAutoClose)
           
static void plot(float[] xIn, int startInd, int endInd, java.lang.String strTitle, boolean bAutoClose, int milliSecondsToClose)
           
static void plot(float[] x, java.lang.String strTitle)
           
static void plotZoomed(double[] x, java.lang.String strTitle, double minVal)
           
static void plotZoomed(double[] x, java.lang.String strTitle, double minVal, double maxVal)
           
static void plotZoomed(double[] x, java.lang.String strTitle, double minVal, double maxVal, boolean bAutoClose)
           
static void plotZoomed(double[] x, java.lang.String strTitle, double minVal, double maxVal, boolean bAutoClose, int milliSecondsToClose)
           
static void plotZoomed(float[] x, java.lang.String strTitle, double minVal)
           
static void plotZoomed(float[] x, java.lang.String strTitle, double minVal, double maxVal)
           
static void plotZoomed(float[] x, java.lang.String strTitle, double minVal, double maxVal, boolean bAutoClose)
           
static void plotZoomed(float[] x, java.lang.String strTitle, double minVal, double maxVal, boolean bAutoClose, int milliSecondsToClose)
           
static int romanToInt(java.lang.String romanNumber)
           
static int shellExecute(java.lang.String strCommand)
           
static int shellExecute(java.lang.String strCommand, boolean bDisplayProgramOutput)
           
static java.lang.String[] splitIntoSensibleXMLUnits(java.lang.String s)
           
static java.util.Locale string2locale(java.lang.String localeString)
           
static java.lang.Character[] StringToCharacterArray(java.lang.String s)
           
static boolean subsumes(java.util.Locale general, java.util.Locale specific)
          Test for lax Locale equality.
static java.lang.String toHumanReadableSize(long byteCount)
           
static boolean veryLowMemoryCondition()
          Verify if the java virtual machine is in a very low memory condition.
static void writeCopyrightNotice(java.io.PrintWriter out, java.lang.String commentChar)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGPREFIX

public static final java.lang.String LOGPREFIX
See Also:
Constant Field Values

punctuationTable

protected static final char[] punctuationTable

toGermanLetterTable

protected static final char[] toGermanLetterTable

germanToAsciiLetterTable

protected static final char[] germanToAsciiLetterTable
Constructor Detail

MaryUtils

public MaryUtils()
Method Detail

ArrayToMap

public static java.util.Map ArrayToMap(java.lang.Object[] a)
Create a map from an Object array which contains paired entries (key, value, key, value, ....).


instantiateObject

public static java.lang.Object instantiateObject(java.lang.String objectInitInfo)
                                          throws java.lang.ClassNotFoundException,
                                                 java.lang.IllegalArgumentException,
                                                 java.lang.InstantiationException,
                                                 java.lang.IllegalAccessException,
                                                 java.lang.reflect.InvocationTargetException,
                                                 java.lang.SecurityException,
                                                 java.lang.NoSuchMethodException
Instantiate an object by calling one of its constructors.

Parameters:
objectInitInfo - a string description of the object to instantiate. The objectInitInfo is expected to have one of the following forms:
  1. my.cool.Stuff
  2. my.cool.Stuff(any,string,args,without,spaces)
  3. my.cool.Stuff(arguments,$my.special.property,other,args)
where 'my.special.property' is a property in one of the MARY config files.
Returns:
the newly instantiated object.
Throws:
java.lang.ClassNotFoundException
java.lang.IllegalArgumentException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
java.lang.SecurityException
java.lang.NoSuchMethodException

StringToCharacterArray

public static java.lang.Character[] StringToCharacterArray(java.lang.String s)

joinStrings

public static java.lang.String joinStrings(java.util.Collection<java.lang.String> strings)
Join a collection of strings into a single String object, in the order indicated by the collection's iterator.

Parameters:
strings - a collection containing exclusively String objects
Returns:
a single String object

indexOf

public static int indexOf(byte[] bytes,
                          byte[] pattern)
Check if bytes contains a subsequence identical with pattern, and return the index position. Assumes that pattern.length ^< bytes.length.

Parameters:
bytes -
pattern -
Returns:
the index position in bytes where pattern starts, or -1 if bytes does not contain pattern.

splitIntoSensibleXMLUnits

public static java.lang.String[] splitIntoSensibleXMLUnits(java.lang.String s)

getExtension

public static java.lang.String getExtension(java.io.File f)
Get the extension of a file.


romanToInt

public static int romanToInt(java.lang.String romanNumber)

isPercentageDelta

public static boolean isPercentageDelta(java.lang.String string)
Tell whether the string contains a positive or negative percentage delta, i.e., a percentage number with an obligatory + or - sign.


getPercentageDelta

public static int getPercentageDelta(java.lang.String string)
For a string containing a percentage delta as judged by isPercentageDelta(), return the numerical value, rounded to an integer.

Returns:
the numeric part of the percentage, rounded to an integer, or 0 if the string is not a valid percentage delta.

isSemitonesDelta

public static boolean isSemitonesDelta(java.lang.String string)
Tell whether the string contains a positive or negative semitones delta, i.e., a semitones number with an obligatory + or - sign, such as "+3.2st" or "-13.2st".


getSemitonesDelta

public static double getSemitonesDelta(java.lang.String string)
For a string containing a semitones delta as judged by isSemitonesDelta(), return the numerical value, as a double.

Returns:
the numeric part of the semitones delta, or 0 if the string is not a valid semitones delta.

isNumberDelta

public static boolean isNumberDelta(java.lang.String string)
Tell whether the string contains a positive or negative number delta, i.e., a number with an obligatory + or - sign.


getNumberDelta

public static int getNumberDelta(java.lang.String string)
For a string containing a number delta as judged by isNumberDelta(), return the numerical value, rounded to an integer.

Returns:
the numeric value, rounded to an integer, or 0 if the string is not a valid number delta.

isUnsignedSemitones

public static boolean isUnsignedSemitones(java.lang.String string)
Tell whether the string contains an unsigned semitones expression, such as "12st" or "5.4st".


getUnsignedSemitones

public static double getUnsignedSemitones(java.lang.String string)
For a string containing an unsigned semitones expression as judged by isUnsignedSemitones(), return the numerical value as a double.

Returns:
the numeric part of the semitones expression, or 0 if the string is not a valid unsigned semitones expression.

isUnsignedNumber

public static boolean isUnsignedNumber(java.lang.String string)
Tell whether the string contains an unsigned number.


getUnsignedNumber

public static int getUnsignedNumber(java.lang.String string)
For a string containing an unsigned number as judged by isUnsignedNumber(), return the numerical value, rounded to an integer.

Returns:
the numeric value, rounded to an integer, or 0 if the string is not a valid unsigned number.

isNumber

public static boolean isNumber(java.lang.String string)
Tell whether the string contains a number.


getNumber

public static int getNumber(java.lang.String string)
For a string containing a number as judged by isNumber(), return the numerical value, rounded to an integer.

Returns:
the numeric value, rounded to an integer, or 0 if the string is not a valid number.

string2locale

public static java.util.Locale string2locale(java.lang.String localeString)

locale2xmllang

public static java.lang.String locale2xmllang(java.util.Locale locale)
Convert a locale into a string that is conform with XML's xml:lang attribute. Basically it is language-COUNTRY, e.g. en-US.

Parameters:
locale - a locale, must not be null
Returns:
Throws:
java.lang.IllegalArgumentException - if locale is null

subsumes

public static boolean subsumes(java.util.Locale general,
                               java.util.Locale specific)
Test for lax Locale equality. More precisely, returns true if (a) both are equal; (b) general only specifies language, and specific has the same language; (c) general specifies language and country, and specific has the same language and country. Else returns false.


availableMemory

public static long availableMemory()
Determine the amount of available memory. "Available" memory is calculated as (max - total) + free.

Returns:
the number of bytes of memory available according to the above algorithm.

lowMemoryCondition

public static boolean lowMemoryCondition()
Verify if the java virtual machine is in a low memory condition. The memory is considered low if less than a specified value is still available for processing. "Available" memory is calculated using availableMemory().The threshold value can be specified as the Mary property mary.lowmemory (in bytes). It defaults to 20000000 bytes.

Returns:
a boolean indicating whether or not the system is in low memory condition.

veryLowMemoryCondition

public static boolean veryLowMemoryCondition()
Verify if the java virtual machine is in a very low memory condition. The memory is considered very low if less than half a specified value is still available for processing. "Available" memory is calculated using availableMemory().The threshold value can be specified as the Mary property mary.lowmemory (in bytes). It defaults to 20000000 bytes.

Returns:
a boolean indicating whether or not the system is in very low memory condition.

createSelfDeletingTempFile

public static java.io.File createSelfDeletingTempFile(int lifetimeInSeconds)
                                               throws java.io.IOException
Create a temporary file that will be deleted after a specified number of seconds. The file will be deleted regardless of whether it is still used or not, so be sure to specify a sufficiently large value.

Parameters:
lifetimeInSeconds - the number of seconds after which the file will be deleted -- e.g., 3600 means that the file will be deleted one hour after creation.
Returns:
the File that was created.
Throws:
java.io.IOException

normaliseUnicodePunctuation

public static java.lang.String normaliseUnicodePunctuation(java.lang.String unicodeText)
Normalise the Unicode text by mapping "exotic" punctuation characters to "standard" ones.

Parameters:
unicodeText - a string that may include "exotic" punctuation characters
Returns:
the string in which exotic characters have been replaced with their closest relative that can be handled by the TTS.

normaliseUnicodeLetters

public static java.lang.String normaliseUnicodeLetters(java.lang.String unicodeText,
                                                       java.util.Locale targetLocale)
Normalise the Unicode text by mapping "exotic" letter characters to "standard" ones. Standard is locale-dependent. For GERMAN: ascii plus german umlauts and ß. For other locales: ascii.

Parameters:
unicodeText - a string that may include "exotic" letter characters
targetLocale - the locale against which normalisation is to be performed.
Returns:
the string in which exotic characters have been replaced with their closest relative that can be handled by the TTS.

getThrowableAndCausesAsString

public static java.lang.String getThrowableAndCausesAsString(java.lang.Throwable t)
Apply the toString() method recursively to this throwable and all its causes. The idea is to get cause information as in printStackTrace() without the stack trace.

Parameters:
t - the throwable to print.
Returns:

isWindows

public static boolean isWindows()

plot

public static void plot(float[] x)

plot

public static void plot(double[] x)

plot

public static void plot(float[] x,
                        int startInd,
                        int endInd)

plot

public static void plot(double[] x,
                        int startInd,
                        int endInd)

plot

public static void plot(float[] x,
                        java.lang.String strTitle)

plot

public static void plot(double[] x,
                        java.lang.String strTitle)

plot

public static void plot(float[] x,
                        int startInd,
                        int endInd,
                        java.lang.String strTitle)

plot

public static void plot(double[] x,
                        int startInd,
                        int endInd,
                        java.lang.String strTitle)

plot

public static void plot(float[] x,
                        int startInd,
                        int endInd,
                        java.lang.String strTitle,
                        boolean bAutoClose)

plot

public static void plot(double[] x,
                        int startInd,
                        int endInd,
                        java.lang.String strTitle,
                        boolean bAutoClose)

plotZoomed

public static void plotZoomed(float[] x,
                              java.lang.String strTitle,
                              double minVal)

plotZoomed

public static void plotZoomed(double[] x,
                              java.lang.String strTitle,
                              double minVal)

plotZoomed

public static void plotZoomed(float[] x,
                              java.lang.String strTitle,
                              double minVal,
                              double maxVal)

plotZoomed

public static void plotZoomed(double[] x,
                              java.lang.String strTitle,
                              double minVal,
                              double maxVal)

plotZoomed

public static void plotZoomed(float[] x,
                              java.lang.String strTitle,
                              double minVal,
                              double maxVal,
                              boolean bAutoClose)

plotZoomed

public static void plotZoomed(double[] x,
                              java.lang.String strTitle,
                              double minVal,
                              double maxVal,
                              boolean bAutoClose)

plotZoomed

public static void plotZoomed(float[] x,
                              java.lang.String strTitle,
                              double minVal,
                              double maxVal,
                              boolean bAutoClose,
                              int milliSecondsToClose)

plotZoomed

public static void plotZoomed(double[] x,
                              java.lang.String strTitle,
                              double minVal,
                              double maxVal,
                              boolean bAutoClose,
                              int milliSecondsToClose)

plot

public static void plot(float[] xIn,
                        int startInd,
                        int endInd,
                        java.lang.String strTitle,
                        boolean bAutoClose,
                        int milliSecondsToClose)

plot

public static void plot(double[] xIn,
                        int startInd,
                        int endInd,
                        java.lang.String strTitle,
                        boolean bAutoClose,
                        int milliSecondsToClose)

isLittleEndian

public static boolean isLittleEndian()

shellExecute

public static int shellExecute(java.lang.String strCommand)

shellExecute

public static int shellExecute(java.lang.String strCommand,
                               boolean bDisplayProgramOutput)

writeCopyrightNotice

public static void writeCopyrightNotice(java.io.PrintWriter out,
                                        java.lang.String commentChar)

toHumanReadableSize

public static java.lang.String toHumanReadableSize(long byteCount)

getLogger

public static org.apache.log4j.Logger getLogger(java.lang.String nameSuffix)
Provide a Logger object whose name is built from MaryUtils.LOGPREFIX and the given nameSuffix.

Parameters:
nameSuffix - the suffix to use for the logger name.
Returns:

getLogger

public static org.apache.log4j.Logger getLogger(java.lang.Class clazz)
Provide a Logger object whose name is built from MaryUtils.LOGPREFIX and the given nameSuffix.

Parameters:
nameSuffix - the suffix to use for the logger name.
Returns:

isLog4jConfigured

public static boolean isLog4jConfigured()
Returns true if it appears that log4j have been previously configured. This code checks to see if there are any appenders defined for log4j which is the definitive way to tell if log4j is already initialized