📄 result.java
字号:
/* * Copyright 1999-2002 Carnegie Mellon University. * Portions Copyright 2002 Sun Microsystems, Inc. * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. * */package edu.cmu.sphinx.result;import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import edu.cmu.sphinx.decoder.search.ActiveList;import edu.cmu.sphinx.decoder.search.AlternateHypothesisManager;import edu.cmu.sphinx.decoder.search.Token;import edu.cmu.sphinx.frontend.Data;import edu.cmu.sphinx.frontend.FloatData;import edu.cmu.sphinx.linguist.dictionary.Word;import edu.cmu.sphinx.util.LogMath;/** * Provides recognition results. Results can be partial or final. A * result should not be modified before it is a final result. Note * that a result may not contain all possible information. * * The following methods are not yet defined but should be: * <pre> public Result getDAG(int compressionLevel); </pre> * */public class Result { private ActiveList activeList; private List resultList; private AlternateHypothesisManager alternateHypothesisManager; private boolean isFinal = false; private int currentFrameNumber; private String reference; private LogMath logMath; /** * Creates a result * * @param activeList the active list associated with this result * @param resultList the result list associated with this result * @param frameNumber the frame number for this result. * @param isFinal if true, the result is a final result */ public Result(AlternateHypothesisManager alternateHypothesisManager, ActiveList activeList, List resultList, int frameNumber, boolean isFinal, LogMath logMath) { this(activeList, resultList, frameNumber, isFinal, logMath); this.alternateHypothesisManager = alternateHypothesisManager; } /** * Creates a result * * @param activeList the active list associated with this result * @param resultList the result list associated with this result * @param frameNumber the frame number for this result. * @param isFinal if true, the result is a final result */ public Result(ActiveList activeList, List resultList, int frameNumber, boolean isFinal, LogMath logMath) { this.activeList = activeList; this.resultList = resultList; this.currentFrameNumber = frameNumber; this.isFinal = isFinal; this.logMath = logMath; } /** * Determines if the result is a final result. A final result is * guaranteed to no longer be modified by the SearchManager that * generated it. Non-final results can be modifed by a * <code>SearchManager.recognize</code> calls. * * @return true if the result is a final result */ public boolean isFinal() { return isFinal; } /** * Returns the log math used for this Result. * * @return the log math used */ public LogMath getLogMath() { return logMath; } /** * Returns a list of active tokens for this result. The list * contains zero or active <code>Token</code> objects that * represents the leaf nodes of all active branches in the result * (sometimes referred to as the 'lattice'). * * The lattice is live and may be modified by a * SearchManager during a recognition. Once the Result is final, * the lattice is fixed and will no longer be modified by the * SearchManager. Applications can modify the lattice (to prepare * for a re-recognition, for example) only after * <code>isFinal</code> returns <code>true</code> * * @return a list containing the active tokens for this result * * @see Token */ public ActiveList getActiveTokens() { return activeList; } /** * Returns a list of result tokens for this result. The list * contains zero or more result <code>Token</code> objects that * represents the leaf nodes of all final branches in the result * (sometimes referred to as the 'lattice'). * * The lattice is live and may be modified by a * SearchManager during a recognition. Once the Result is final, * the lattice is fixed and will no longer be modified by the * SearchManager. Applications can modify the lattice (to prepare * for a re-recognition, for example) only after * <code>isFinal</code> returns <code>true</code> * * @return a list containing the final result tokens for this result * * @see Token */ public List getResultTokens() { return resultList; } /** * Returns the AlternateHypothesisManager * Used to construct a Lattice * * @return the AlternateHypothesisManager */ public AlternateHypothesisManager getAlternateHypothesisManager() { return alternateHypothesisManager; } /** * Returns the current frame number * * @return the frame number */ public int getFrameNumber() { return currentFrameNumber; } /** * Returns the best scoring final token in the result. A final * token is a token that has reached a final state in the current * frame. * * @return the best scoring final token or null */ public Token getBestFinalToken() { Token bestToken = null; for (Iterator i = resultList.iterator(); i.hasNext();) { Token token = (Token) i.next(); if (bestToken == null || token.getScore() > bestToken.getScore()) { bestToken = token; } } return bestToken; } /** * Returns the best scoring token in the result. First, the best * final token is retrieved. A final token is one that has reached the * final state in the search space. If no final tokens can be found, then * the best, non-final token is returned. * * @return the best scoring token or null */ public Token getBestToken() { Token bestToken = getBestFinalToken(); if (bestToken == null) { bestToken = getBestActiveToken(); } return bestToken; } /** * Returns the best scoring token in the active set * * @return the best scoring token or null */ public Token getBestActiveToken() { Token bestToken = null; for (Iterator i = activeList.iterator(); i.hasNext();) { Token token = (Token) i.next(); if (bestToken == null || token.getScore() > bestToken.getScore()) { bestToken = token; } } return bestToken; } /** * Searches through the n-best list to find the * the branch that matches the given string * * @param text the string to search for * @return the token at the head of the branch or null */ public Token findToken(String text) { text = text.trim(); for (Iterator i = resultList.iterator(); i.hasNext();) { Token token = (Token) i.next(); if (text.equals(token.getWordPathNoFiller())) { return token; } } return null; } /** * Searches through the n-best list to find the * the branch that matches the beginning of the given string * * @param text the string to search for * @return the list token at the head of the branch */ public List findPartialMatchingTokens(String text) { List list = new ArrayList(); text = text.trim(); for (Iterator i = activeList.iterator(); i.hasNext();) { Token token = (Token) i.next(); if (text.startsWith(token.getWordPathNoFiller())) { list.add(token); } } return list; } /** * Returns the best scoring token that matches the beginning of * the given text. * * @param text the text to match */ public Token getBestActiveParitalMatchingToken(String text) { List matchingList = findPartialMatchingTokens(text); Token bestToken = null; for (Iterator i = matchingList.iterator(); i.hasNext();) { Token token = (Token) i.next(); if (bestToken == null || token.getScore() > bestToken.getScore()) { bestToken = token; } } return bestToken; } /** * Returns detailed frame statistics for this result * * @return frame statistics for this result as an array, with one * element per frame or <code>null</code> if no frame statistics * are available.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -