token.java

来自「It is the Speech recognition software. 」· Java 代码 · 共 594 行 · 第 1/2 页

JAVA
594
字号
/* * 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.decoder.search;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.Set;import edu.cmu.sphinx.decoder.scorer.Scoreable;import edu.cmu.sphinx.frontend.Data;import edu.cmu.sphinx.linguist.HMMSearchState;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.WordSearchState;import edu.cmu.sphinx.linguist.UnitSearchState;import edu.cmu.sphinx.linguist.acoustic.HMMState;import edu.cmu.sphinx.linguist.acoustic.Unit;import edu.cmu.sphinx.linguist.dictionary.Word;/** * Represents a single state in the recognition trellis. Subclasses of * a token are used to represent the various emitting state. * * All scores are maintained in LogMath log base * */public class Token implements Scoreable {    /**     * a token comparator that is used to order tokens in     * descending order     *     */    public final static Comparator COMPARATOR = new Comparator() {	    public int compare(Object o1, Object o2) {		Token t1 = (Token) o1;		Token t2 = (Token) o2;		if (t1.getScore() > t2.getScore()) {		    return -1;		} else if (t1.getScore() ==  t2.getScore()) {		    return 0;		} else {		    return 1;		}	    }	};    private final static boolean COMBINE_BRANCHES = true;    private static int curCount;    private static int lastCount;    private static DecimalFormat scoreFmt = new DecimalFormat("0.0000000E00");    private static DecimalFormat numFmt = new DecimalFormat("0000");    private Token predecessor;    private int frameNumber;    private float logTotalScore;    private float logLanguageScore;    private float logInsertionProbability;    private float logAcousticScore;    private float logWorkingScore;    private SearchState searchState;    private int location;    private Object appObject;    private static Set predecessorClasses = null;    /**     * Set the predecessor class. Used to modify the behavior of child()      * so that the predecessor backpointer will skip internal states.       * For example, when retaining tokens to form a word lattice,      * it would be inefficient to     * keep any states but WordStates-- other types of states are not used     * and the memory should be saved.  On the other hand, a phoneme recognizer     * would require PronunciationStates to create a suitable result.     *     * @param bpClasses     */    public static void setPredecessorClass(Set bpClasses) {        predecessorClasses = bpClasses;    }    /**     * Constructs a new token that continues the search from the current token.     * If predessorClasses is null or if the class of the state is a member of     * predecessorClasses, the predecessor of the new token is set to the     * current token.  Otherwise it is set to the predecessor of the current     * token.  This behavior is used to save memory when building lattices.     *     * @param state the SentenceHMMState associated with this token     *     * @param logTotalScore the total entry score for this token (in     * LogMath log base)     *     * @param logLanguageScore the language score associated with this     * token (in LogMath log base)     *     * @param logInsertionProbability the insertion probabilty  associated with     * this token (in LogMath log base)     *     * @param frameNumber the frame number associated with this token     */    public Token child(SearchState state,                       float logTotalScore,                       float logLanguageScore,                       float logInsertionProbability,                       int frameNumber) {       if ((predecessorClasses == null) || 	   predecessorClasses.contains(state.getClass())) {            return new Token(this, state, 			     logTotalScore, logLanguageScore, 			     logInsertionProbability, frameNumber);        } else {            return new Token(predecessor, state, 			     logTotalScore, logLanguageScore, 			     logInsertionProbability, frameNumber);        }    }    /**     * Internal constructor for a token.       * Used by classes Token, CombineToken, ParallelToken     *     * @param predecessor the predecessor for this token     * @param state the SentenceHMMState associated with this token     *     * @param logTotalScore the total entry score for this token (in     * LogMath log base)     *     * @param logLanguageScore the language score associated with this     * token (in LogMath log base)     *     * @param logInsertionProbability the insertion probabilty  associated with     * this token (in LogMath log base)     *     * @param frameNumber the frame number associated with this token     *     */    protected Token(Token predecessor,                  SearchState state,                  float logTotalScore,                  float logLanguageScore,                  float logInsertionProbability,                  int frameNumber) {        this.predecessor = predecessor;        this.searchState = state;        this.logTotalScore = logTotalScore;        this.logLanguageScore = logLanguageScore;        this.logInsertionProbability = logInsertionProbability;        this.frameNumber = frameNumber;        this.location = -1;        curCount++;    }    /**     * Creates the initial token with the given word history depth     *     * @param state the SearchState associated with this token     *     * @param frameNumber the frame number for this token     */    public Token(SearchState state, int frameNumber) {        this(null, state, 0.0f, 0.0f, 0.0f, frameNumber);    }    /**     * Creates a Token with the given acoustic and language scores and     * predecessor.     *     * @param logAcousticScore the log acoustic score     * @param logLanguageScore the log language score     * @param predecessor the predecessor Token     */    public Token(float logAcousticScore, float logLanguageScore,                 Token predecessor) {        this.logAcousticScore = logAcousticScore;        this.logLanguageScore = logLanguageScore;        this.predecessor = predecessor;    }    /**     * Returns the predecessor for this token, or null if this token     * has no predecessors     *     * @return the predecessor     */    public Token getPredecessor() {        return predecessor;    }    /**     * Returns the frame number for this token. Note that for tokens that     * are associated with non-emitting states, the frame number     * represents the next frame number.  For emitting states, the     * frame number represents the current frame number.     *     * @return the frame number for this token     */    public int getFrameNumber() {        return frameNumber;    }    /**     * Returns the feature for this Token.     *     * @return the feature for this Token     */    public Data getData() {        if (appObject != null && appObject instanceof Data) {            return (Data) appObject;        } else {            return null;        }    }    /**     * Returns the score for the token. The score is a combination of     * language and acoustic scores     *     * @return the score of this frame (in logMath log base)     */    public float getScore() {        return logTotalScore;    }    /**     * Calculates a score against the given feature. The score can be     * retreived with get score     *     * @param feature the feature to be scored     * @param keepData whether this Scoreable should keep a reference     *    to the given feature     * @param gain gain to apply to the score;     *     * @return the score for the feature     */    public float calculateScore(Data feature, boolean keepData, float gain) {        //assert searchState.isEmitting()         //   : "Attempting to score non-scoreable token: " + searchState;        HMMSearchState hmmSearchState = (HMMSearchState) searchState;        HMMState hmmState = hmmSearchState.getHMMState();        logAcousticScore = hmmState.getScore(feature) * gain;        logTotalScore += logAcousticScore;        if (keepData) {            setAppObject(feature);        }        return logTotalScore;    }    /**     * Normalizes a previously calculated score     *     * @param maxLogScore the score to normalize this score with     *     * @return the normalized score     */    public float normalizeScore(float maxLogScore) {        logTotalScore -= maxLogScore;        logAcousticScore -= maxLogScore;        return logTotalScore;    }    /**     * Gets the working score. The working score is used to maintain     * non-final scores during the search. Some search algorithms such     * as bushderby use the working score     *     * @return the working score (in logMath log base)     */    public float getWorkingScore() {	    return logWorkingScore;    }    /**     * Sets the working score for this token     *     * @param logScore the working score (in logMath log base)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?