Package com.sun.speech.freetts.cart

# Class CARTImpl

java.lang.Object
com.sun.speech.freetts.cart.CARTImpl

All Implemented Interfaces:
  `CART`

public class CARTImpl extends Object implements CART
Implementation of a Classification and Regression Tree (CART) that is used more like a binary decision tree, with each node containing a decision or a final value. The decision nodes in the CART trees operate on an Item and have the following format:

       NODE feat operand value qfalse

Where `feat` is an string that represents a feature to pass to the `findFeature` method of an item.

The `value` represents the value to be compared against the feature obtained from the item via the `feat` string. The `operand` is the operation to do the comparison. The available operands are as follows:

- < - the feature is less than value
- = - the feature is equal to the value
- > - the feature is greater than the value
- MATCHES - the feature matches the regular expression stored in value
- IN - [[[TODO: still guessing because none of the CART's in Flite seem to use IN]]] the value is in the list defined by the feature.

[[[TODO: provide support for the IN operator.]]]

For < and >, this CART coerces the value and feature to float's. For =, this CART coerces the value and feature to string and checks for string equality. For MATCHES, this CART uses the value as a regular expression and compares the obtained feature to that.

A CART is represented by an array in this implementation. The `qfalse` value represents the index of the array to go to if the comparison does not match. In this implementation, qtrue index is always implied, and represents the next element in the array. The root node of the CART is the first element in the array.

The interpretations always start at the root node of the CART and continue until a final node is found. The final nodes have the following form:

       LEAF value

Where `value` represents the value of the node. Reaching a final node indicates the interpretation is over and the value of the node is the interpretation result.

## Constructor Summary

Constructors
Constructor
Description
`CARTImpl(BufferedReader reader, int nodes)`
Creates a new CART by reading from the given reader.
`CARTImpl(URL url)`
Creates a new CART by reading from the given URL.

## Method Summary

Modifier and Type
Method
Description
`void`
`dumpBinary(DataOutputStream os)`
Dumps this CART to the output stream.
`protected com.sun.speech.freetts.cart.CARTImpl.Node`
`getNode(String type, StringTokenizer tokenizer, int currentNode)`
Gets the node based upon the type and tokenizer.
`Object`
`interpret(Item item)`
Passes the given item through this CART and returns the interpretation.
`static CART`
`loadBinary(DataInputStream is)`
Loads a CART from the input stream.
`static CART`
`loadBinary(ByteBuffer bb)`
Loads a CART from the input byte buffer.
`protected void`
`parseAndAdd(String line)`
Creates a node from the given input line and add it to the CART.
`protected Object`
`parseValue(String string)`
Coerces a string into a value.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### CARTImpl

public CARTImpl(URL url) throws IOException
Creates a new CART by reading from the given URL.

Parameters:
  `url` - the location of the CART data
Throws:
  `IOException` - if errors occur while reading the data

### CARTImpl

public CARTImpl(BufferedReader reader, int nodes) throws IOException
Creates a new CART by reading from the given reader.

Parameters:
  `reader` - the source of the CART data
  `nodes` - the number of nodes to read for this cart
Throws:
  `IOException` - if errors occur while reading the data

## Method Details

### dumpBinary

public void dumpBinary(DataOutputStream os) throws IOException
Dumps this CART to the output stream.

Specified by:
  `dumpBinary` in interface `CART`
Parameters:
  `os` - the output stream
Throws:
  `IOException` - if an error occurs during output

### loadBinary

public static CART loadBinary(ByteBuffer bb) throws IOException
Loads a CART from the input byte buffer.

Parameters:
  `bb` - the byte buffer
Returns:
  the CART
Throws:
  `IOException` - if an error occurs during output Note that cart nodes are really saved as strings that have to be parsed.

### loadBinary

public static CART loadBinary(DataInputStream is) throws IOException
Loads a CART from the input stream.

Parameters:
  `is` - the input stream
Returns:
  the CART
Throws:
  `IOException` - if an error occurs during output Note that cart nodes are really saved as strings that have to be parsed.

### parseAndAdd

protected void parseAndAdd(String line)
Creates a node from the given input line and add it to the CART. It expects the TOTAL line to come before any of the nodes.

Parameters:
  `line` - a line of input to parse

### getNode

protected com.sun.speech.freetts.cart.CARTImpl.Node getNode(String type, StringTokenizer tokenizer, int currentNode)
Gets the node based upon the type and tokenizer.

Parameters:
  `type` - `NODE` or `LEAF`
  `tokenizer` - the StringTokenizer containing the data to get
  `currentNode` - the index of the current node we're looking at
Returns:
  the node

### parseValue

protected Object parseValue(String string)
Coerces a string into a value.

Parameters:
  `string` - of the form "type(value)"; for example, "Float(2.3)"
Returns:
  the value

### interpret

public Object interpret(Item item)
Passes the given item through this CART and returns the interpretation.

Specified by:
  `interpret` in interface `CART`
Parameters:
  `item` - the item to analyze
Returns:
  the interpretation