📄 node.java
字号:
while (childrenIt.hasNext()) { Node childNode = getChild(childrenIt.next().toString()); childNode.prune(threshold); if (childNode.totalCount() < threshold) childrenIt.remove(); } } /** * Returns the total number of nodes at least as specific as this * node, including this node. * * @return Total number of nodes at least as specific as this * node. */ public int numNodes() { int count = 1; Iterator children = children().iterator(); while (children.hasNext()) count += getChild(children.next().toString()).numNodes(); return count; } /** * Returns total number of outcome counters on this node and all * more specific nodes. * * @return Number of outcome counters on nodes at least as * specific as this node. */ public int numCounters() { int count = mOutcomes.keySet().size(); Iterator children = children().iterator(); while (children.hasNext()) count += getChild(children.next().toString()).numCounters(); return count; } /** * Returns <code>true</code> if the specified outcome has a * nonzero count for this node. * * @param outcome Outcome to test. * @return <code>true</code> if the specified outcome has a * nonzero count for this node. */ public boolean hasOutcome(String outcome) { return mOutcomes.containsKey(outcome); } /** * Returns the counter representing the specified outcome symbol * or <code>null</code> if it doesn't exist. * * @param outcome Outcome event symbol. * @return Counter representing the specified outcome, or * <code>null</code> if it doesn't exist. */ public OutcomeCounter getOutcome(String outcome) { return (OutcomeCounter) mOutcomes.get(outcome); } /** * Returns <code>true</code> if this node has the more specific * outcome specified. * * @param child Next more specific context event symbol. * @return <code>true</code> if this node has the more specific * outcome specified. */ public boolean hasChild(String child) { return mChildren.containsKey(child); } /** * Returns the node representing the more specific outcome * specified, or <code>null</code> if it doesn't exist. * * @param child Next more specific context event symbol. * @return The node representing the more specific outcome * specified, or <code>null</code> if it doesn't exist. */ public Node getChild(String child) { return (Node) mChildren.get(child); } /** * Returns the next more specific context specified by the symbol, * or creates one and returns the one created. The backoff node * and symbol table compiler to use for the symbol are also * specified. The initial count and number of outcomes will be * <code>0</code>. * * @param child Next more specific context event symbol. * @param backoffNode Backoff node for the returned node in case * it needs to be created. * @param symbolTable Symbol table for the specified symbol in case the * returned node needs to be created. * @return The node representing the more specific outcome * specified. */ public Node getOrCreateChild(String child, Node backoffNode, SymbolTableCompiler symbolTable) { if (hasChild(child)) return getChild(child); Node node = new Node(child, symbolTable, backoffNode); mChildren.put(child,node); return node; } /** * Returns the complete set of outcomes. * * @return Set of outcomes. */ public Set outcomes() { return mOutcomes.keySet(); } /** * Returns the complete set of next more specific contexts. * * @return Set of next more specific contexts. */ public Set children() { return mChildren.keySet(); } /** * Returns the number of times the specified outcome * has been seen, which may be <code>0</code>. * * @param outcome Symbol representing outcome event to test. * @return Number of times outcome has been incremented from this * node. */ public int outcomeCount(String outcome) { OutcomeCounter ctr = getOutcome(outcome); return ctr == null ? 0 : ctr.count(); } /** * Increments the outcome counter specified for this node, using * the supplied symbol table to create a new outcome counter * if the event has not previously been created. * * @param outcome Symbol representing outcome to increment. * @param symbolTable Symbol table for outcome in case it needs to * be created. */ public void incrementOutcome(String outcome, SymbolTableCompiler symbolTable) { ++mTotalCount; if (hasOutcome(outcome)) { getOutcome(outcome).increment(); } else { ++mNumOutcomes; mOutcomes.put(outcome,new OutcomeCounter(outcome,symbolTable,1)); } } /** * Returns the total count of all outcomes for this node. * * @return Total count of all outcomes for this node. */ public int totalCount() { return mTotalCount; } /** * Returns the compiled value of the natural log of * one minus lambda, <code>log (1-lambda)</code>. * * @return Compiled value of <code>log (1-lambda)</code>. */ public float oneMinusLambda() { return mOneMinusLambda; } /** * Compiles all the estimates for this node, its outcomes, and * applies recursively to all more specific nodes. Stores * <code>1-lambda</code> for future use on nodes and estimates * are stored on counters. * * @param lambdaFactor Lambda factor used to compute estimates. */ public void compileEstimates(double lambdaFactor) { mOneMinusLambda = (float) Math.log(1.0 - lambda(lambdaFactor)); Iterator outcomeIterator = outcomes().iterator(); while (outcomeIterator.hasNext()) { String outcome = outcomeIterator.next().toString(); getOutcome(outcome).setEstimate((float)logEstimate(outcome, lambdaFactor)); } Iterator childrenIterator = children().iterator(); while (childrenIterator.hasNext()) { Node child = getChild(childrenIterator.next().toString()); child.compileEstimates(lambdaFactor); } } /** * Return the natural log of the estimate for the specified * outcome, using the specified lambda factor. * * @param outcome Outcome to estimate. * @param lambdaFactor Lambda factor used to compute estimate. * @return Log estimate of the specified outcome. */ public double logEstimate(String outcome, double lambdaFactor) { return Math.log(estimate(outcome,lambdaFactor)); } /** * Returns the next most general node to use for backoff, * or <code>null</code> if none is available. * * @return Next most general node to use for backoff, or * <code>null</code> if none is available. */ public Node backoffNode() { return mBackoffNode; } /** * Return the estimate for the specified outcome, using the * specified lambda factor. * * @param outcome Outcome to estimate. * @param lambdaFactor Lambda factor used to compute estimate. * @return Log estimate of the specified outcome. */ public double estimate(String outcome, double lambdaFactor) { if (mBackoffNode == null) return maxLikelihoodEstimate(outcome); double lambda = lambda(lambdaFactor); return lambda * maxLikelihoodEstimate(outcome) + (1-lambda) * mBackoffNode.estimate(outcome,lambdaFactor); } /** * Returns the maximal likelihood estimate for the outcome given * the counts on this node and the outcome counter. * * @param outcome Outcome to estimate. * @return Maximal likelihood estimate of the specified outcome. */ public double maxLikelihoodEstimate(String outcome) { return ((double)outcomeCount(outcome)) / (double)mTotalCount; } /** * Returns the value of <code>lambda</code> given the specified * lambda factor. Returns <code>0.0</code> if there are no * outcomes with nonzero count. * * @param lambdaFactor Factor used to compute lambda value. */ public double lambda(double lambdaFactor) { if (mTotalCount == 0) return 0.0; return ((double)mTotalCount) / ((double)(mTotalCount + lambdaFactor * mNumOutcomes)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -