Class LetterToSoundImpl
- All Implemented Interfaces:
LetterToSound
The LTS rules are a simple state machine, with one entry point for each letter of the alphabet (lower case letters are always assumed, and the rules keep an array with one entry per letter that point into the state machine).
The state machine consists of a huge array, with most entries containing a decision and the indices of two other entries. The first of these two indices represents where to go if the decision is true, and the second represents where to go if the decision is false. All entries that do not contain a decision are final entries, and these contain a phone.
The decision in this case is a simple character comparison, but it is done in the context of a window around the character in the word. The decision consists of a index into the context window and a character value. If the character in the context window matches the character value, then the decision is true.
The machine traversal for each letter starts at that letter's entry in the state machine and ends only when it reaches a final state. If there is no phone that can be mapped, the phone in the final state is set to 'epsilon.'
The context window for a character is generated in the following way:
- Pad the original word on either side with '#' and '0' characters the size of the window for the LTS rules (in this case, the window size is 4). The "#" is used to indicate the beginning and end of the word. So, the word "monkey" would turn into "000#monkey#000".
- For each character in the word, the context window consists of the characters in the padded form the preceed and follow the word. The number of characters on each side is dependent upon the window size. So, for this implementation, the context window for the 'k' in monkey is "#money#0".
Here's how the phone for 'k' in 'monkey' might be determined:
- Create the context window "#money#0".
- Start at the state machine entry for 'k' in the state machine.
- Grab the 'index' from the current state. This represents an index into the context window.
- Compare the value of the character at the index in the context window to the character from the current state. If there is a match, the next state is the qtrue value. If there isn't a match, the next state is the qfalse state.
- Keep on working through the machine until you read a final state.
- When you get to the final state, the phone is the character in that state.
This implementation will either read from a straight ASCII file or a binary file. When reading from an ASCII file, you can specify when the input line is tokenized: load, lookup, or never. If you specify 'load', the entire file will be parsed when it is loaded. If you specify 'lookup', the file will be loaded, but the parsing for each line will be delayed until it is referenced and the parsed form will be saved away. If you specify 'never', the lines will parsed each time they are referenced. The default is 'load'. To specify the load type, set the system property as follows:
-Dcom.sun.speech.freetts.lexicon.LTSTokenize=load
[[[TODO: This implementation uses ASCII 'a'-'z', which is not internationalized.]]]
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected HashMap
The indexes of the starting points for letters in the state machine.protected boolean
If true, the state string is tokenized when it is first read.protected boolean
If true, the state string is tokenized the first time it is referenced. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
compare
(LetterToSoundImpl other) Compares this LTS to another for debugging purposes.void
dumpBinary
(String path) Dumps a binary form of the letter to sound rules.protected char[]
getFullBuff
(String word) Makes a character array that looks like "000#word#000".String[]
Calculates the phone list for a given word.protected com.sun.speech.freetts.lexicon.LetterToSoundImpl.State
getState
(int i) Gets theState
at the given index.protected com.sun.speech.freetts.lexicon.LetterToSoundImpl.State
Gets theState
based upon theString
.protected com.sun.speech.freetts.lexicon.LetterToSoundImpl.State
getState
(String type, StringTokenizer tokenizer) Gets theState
based upon thetype
andtokenizer
.
static void
Translates between text and binary forms of the CMU6 LTS rules.protected void
parseAndAdd
(String line) Creates a word from the given input line and add it to the state machine.
-
Field Details
-
tokenizeOnLoad
protected boolean tokenizeOnLoadIf true, the state string is tokenized when it is first read. The side effects of this are quicker lookups, but more memory usage and a longer startup time. -
tokenizeOnLookup
protected boolean tokenizeOnLookupIf true, the state string is tokenized the first time it is referenced. The side effects of this are quicker lookups, but more memory usage. -
letterIndex
The indexes of the starting points for letters in the state machine.
-
-
Constructor Details
-
LetterToSoundImpl
Class constructor.- Parameters:
ltsRules
- a URL pointing to the text containing the letter to sound rulesbinary
- if true, the URL is a binary source- Throws:
NullPointerException
- if the ltsRules are nullIOException
- if errors are encountered while reading the compiled form or the addenda
-
-
Method Details
-
parseAndAdd
Creates a word from the given input line and add it to the state machine. It expects the TOTAL line to come before any of the states.- Parameters:
line
- the line of text from the input file
-
dumpBinary
Dumps a binary form of the letter to sound rules. This method is not thread-safe.Binary format is:
MAGIC VERSION NUM STATES for each state ...
- Parameters:
path
- the path to dump the file to- Throws:
IOException
- if a problem occurs during the dump
-
getState
protected com.sun.speech.freetts.lexicon.LetterToSoundImpl.State getState(int i) Gets theState
at the given index. This may replace aString
at the current spot with an actualState
instance.- Parameters:
i
- the index into the state machine- Returns:
- the
State
at the given index.
-
getState
Gets theState
based upon theString
.- Parameters:
s
- the string to parse- Returns:
- the parsed
State
-
getState
protected com.sun.speech.freetts.lexicon.LetterToSoundImpl.State getState(String type, StringTokenizer tokenizer) Gets theState
based upon thetype
andtokenizer
.
- Parameters:
type
- one ofSTATE
orPHONE
tokenizer
- aStringTokenizer
containing theState
- Returns:
- the parsed
State
-
getFullBuff
Makes a character array that looks like "000#word#000".- Parameters:
word
- the original word- Returns:
- the padded word
-
getPhones
Calculates the phone list for a given word. If a phone list cannot be determined,null
is returned. This particular implementation ignores the part of speech.- Specified by:
getPhones
in interfaceLetterToSound
- Parameters:
word
- the word to findpartOfSpeech
- the part of speech.- Returns:
- the list of phones for word or
null
-
compare
Compares this LTS to another for debugging purposes.- Parameters:
other
- the other LTS to compare to- Returns:
true
if these are equivalent
-
main
Translates between text and binary forms of the CMU6 LTS rules.
-