📄 cartimpl.java
字号:
int nodeIndex = 0; Node node = cart[nodeIndex]; DecisionNode decision; // System.out.println(" ---- start cart on " + item); while (!(cart[nodeIndex] instanceof LeafNode)) { decision = (DecisionNode) cart[nodeIndex]; nodeIndex = decision.getNextNode(item); // Utilities.debug(decision.toString()); } Utilities.debug("LEAF " + cart[nodeIndex].getValue()); return ((LeafNode) cart[nodeIndex]).getValue(); } /** * A node for the CART. */ static abstract class Node { /** * The value of this node. */ protected Object value; private String creationLine; /** * Create a new Node with the given value. */ public Node(Object value) { this.value = value; } /** * Get the value. */ public Object getValue() { return value; } /** * Return a string representation of the type of the value. */ public String getValueString() { if (value == null) { return "NULL()"; } else if (value instanceof String) { return "String(" + value.toString() + ")"; } else if (value instanceof Float) { return "Float(" + value.toString() + ")"; } else if (value instanceof Integer) { return "Integer(" + value.toString() + ")"; } else { return value.getClass().toString() + "(" + value.toString() + ")"; } } /** * sets the line of text used to create this node. * @param line the creation line */ public void setCreationLine(String line) { creationLine = line; } /** * Dumps the binary form of this node. * @param os the output stream to output the node on * @throws IOException if an IO error occurs */ final public void dumpBinary(DataOutputStream os) throws IOException { Utilities.outString(os, creationLine); } } /** * A decision node that determines the next Node to go to in the CART. */ abstract static class DecisionNode extends Node { /** * The feature used to find a value from an Item. */ private PathExtractor path; /** * Index of Node to go to if the comparison doesn't match. */ protected int qfalse; /** * Index of Node to go to if the comparison matches. */ protected int qtrue; /** * The feature used to find a value from an Item. */ public String getFeature() { return path.toString(); } /** * Find the feature associated with this DecisionNode * and the given item * @param item the item to start from * @return the object representing the feature */ public Object findFeature(Item item) { return path.findFeature(item); } /** * Returns the next node based upon the * descision determined at this node * @param item the current item. * @return the index of the next node */ public final int getNextNode(Item item) { return getNextNode(findFeature(item)); } /** * Create a new DecisionNode. * @param feature the string used to get a value from an Item * @param value the value to compare to * @param qtrue the Node index to go to if the comparison matches * @param qfalse the Node machine index to go to upon no match */ public DecisionNode(String feature, Object value, int qtrue, int qfalse) { super(value); this.path = new PathExtractorImpl(feature, true); this.qtrue = qtrue; this.qfalse = qfalse; } /** * Get the next Node to go to in the CART. The return * value is an index in the CART. */ abstract public int getNextNode(Object val); } /** * A decision Node that compares two values. */ static class ComparisonNode extends DecisionNode { /** * LESS_THAN */ final static String LESS_THAN = "<"; /** * EQUALS */ final static String EQUALS = "="; /** * GREATER_THAN */ final static String GREATER_THAN = ">"; /** * The comparison type. One of LESS_THAN, GREATER_THAN, or * EQUAL_TO. */ String comparisonType; /** * Create a new ComparisonNode with the given values. * @param feature the string used to get a value from an Item * @param value the value to compare to * @param comparisonType one of LESS_THAN, EQUAL_TO, or GREATER_THAN * @param qtrue the Node index to go to if the comparison matches * @param qfalse the Node index to go to upon no match */ public ComparisonNode(String feature, Object value, String comparisonType, int qtrue, int qfalse) { super(feature, value, qtrue, qfalse); if (!comparisonType.equals(LESS_THAN) && !comparisonType.equals(EQUALS) && !comparisonType.equals(GREATER_THAN)) { throw new Error("Invalid comparison type: " + comparisonType); } else { this.comparisonType = comparisonType; } } /** * Compare the given value and return the appropriate Node index. * IMPLEMENTATION NOTE: LESS_THAN and GREATER_THAN, the Node's * value and the value passed in are converted to floating point * values. For EQUAL, the Node's value and the value passed in * are treated as String compares. This is the way of Flite, so * be it Flite. * @param val the value to compare */ public int getNextNode(Object val) { boolean yes = false; int ret; if (comparisonType.equals(LESS_THAN) || comparisonType.equals(GREATER_THAN)) { float cart_fval; float fval; if (value instanceof Float) { cart_fval = ((Float) value).floatValue(); } else { cart_fval = Float.parseFloat(value.toString()); } if (val instanceof Float) { fval = ((Float) val).floatValue(); } else { fval = Float.parseFloat(val.toString()); } if (comparisonType.equals(LESS_THAN)) { yes = (fval < cart_fval); } else { yes = (fval > cart_fval); } } else { // comparisonType = "=" String sval = val.toString(); String cart_sval = value.toString(); yes = sval.equals(cart_sval); } if (yes) { ret = qtrue; } else { ret = qfalse; } Utilities.debug(trace(val, yes, ret)); return ret; } private String trace(Object value, boolean match, int next) { return "NODE " + getFeature() + " [" + value + "] " + comparisonType + " [" + getValue() + "] " + (match ? "Yes" : "No") + " next " + next; } /** * Get a string representation of this Node. */ public String toString() { return "NODE " + getFeature() + " " + comparisonType + " " + getValueString() + " " + Integer.toString(qtrue) + " " + Integer.toString(qfalse); } } /** * A Node that checks for a regular expression match. */ static class MatchingNode extends DecisionNode { Pattern pattern; /** * Create a new MatchingNode with the given values. * @param feature the string used to get a value from an Item * @param regex the regular expression * @param qtrue the Node index to go to if the comparison matches * @param qfalse the Node index to go to upon no match */ public MatchingNode(String feature, String regex, int qtrue, int qfalse) { super(feature, regex, qtrue, qfalse); this.pattern = Pattern.compile(regex); } /** * Compare the given value and return the appropriate CART index. * @param val the value to compare -- this must be a String */ public int getNextNode(Object val) { return pattern.matcher((String) val).matches() ? qtrue : qfalse; } /** * Get a string representation of this Node. */ public String toString() { StringBuffer buf = new StringBuffer( NODE + " " + getFeature() + " " + OPERAND_MATCHES); buf.append(getValueString() + " "); buf.append(Integer.toString(qtrue) + " "); buf.append(Integer.toString(qfalse)); return buf.toString(); } } /** * The final Node of a CART. This just a marker class. */ static class LeafNode extends Node { /** * Create a new LeafNode with the given value. * @param the value of this LeafNode */ public LeafNode(Object value) { super(value); } /** * Get a string representation of this Node. */ public String toString() { return "LEAF " + getValueString(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -