bushderbysearchmanager.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 569 行 · 第 1/2 页
JAVA
569 行
/* * 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.research.bushderby;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import edu.cmu.sphinx.decoder.pruner.Pruner;import edu.cmu.sphinx.decoder.scorer.AcousticScorer;import edu.cmu.sphinx.decoder.search.ActiveList;import edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager;import edu.cmu.sphinx.decoder.search.Token;import edu.cmu.sphinx.linguist.HMMSearchState;import edu.cmu.sphinx.linguist.Linguist;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.SearchStateArc;import edu.cmu.sphinx.linguist.UnitSearchState;import edu.cmu.sphinx.linguist.WordSearchState;import edu.cmu.sphinx.linguist.WordSequence;import edu.cmu.sphinx.linguist.acoustic.LeftRightContext;import edu.cmu.sphinx.linguist.acoustic.Unit;import edu.cmu.sphinx.linguist.dictionary.Dictionary;import edu.cmu.sphinx.linguist.dictionary.Word;import edu.cmu.sphinx.linguist.language.ngram.LanguageModel;import edu.cmu.sphinx.util.LogMath;import edu.cmu.sphinx.util.SphinxProperties;/** * Provides the breadth first search. To perform recognition * an application should call initialize before * recognition begins, and repeatedly call <code> recognize </code> * until Result.isFinal() returns true. Once a final result has been * obtained, <code> terminate </code> should be called. * * Note that all scores and probabilities are maintained internally in * the LogMath log domain. */public class BushderbySearchManager extends SimpleBreadthFirstSearchManager { private final static String PROP_PREFIX = "edu.cmu.sphinx.research.bushderby.BushderbySearchManager."; /** * The sphinx property for the Bushderby eta value. */ public final static String PROP_BUSHDERBY_ETA = PROP_PREFIX + "bushderbyEta"; /** * The default value for the Bushderby eta value, which is 1e99. */ public final static double PROP_BUSHDERBY_ETA_DEFAULT = 1E99; /** * The sphinx property that defines whether to filter successor * states during the search. */ public final static String PROP_FILTER_SUCCESSORS = PROP_PREFIX + "filterSuccessors"; /** * The default value for whether to filter success states during * the search, which is true. */ public final static boolean PROP_FILTER_SUCCESSORS_DEFAULT = false; private LanguageModel languageModel; private boolean filterSuccessors; private double bushderbyEta; /** * Initializes this BreadthFirstSearchManager with the given * context, linguist, scorer and pruner. * * @param context the context to use * @param linguist the Linguist to use * @param scorer the AcousticScorer to use * @param pruner the Pruner to use */ public void initialize(String context, Linguist linguist, AcousticScorer scorer, Pruner pruner) { super.initialize(context, linguist, scorer, pruner); SphinxProperties props = getSphinxProperties(); bushderbyEta = props.getDouble(PROP_BUSHDERBY_ETA, PROP_BUSHDERBY_ETA_DEFAULT); System.out.println("bushderbyEta is " + bushderbyEta); filterSuccessors = props.getBoolean(PROP_FILTER_SUCCESSORS, PROP_FILTER_SUCCESSORS_DEFAULT); if (getLinguist() != null) { languageModel = getLinguist().getLanguageModel(); } } /** * Grow branches using Bushderby. * * Goes through the active list of tokens and expands each * token, finding the set of successor tokens until all the successor * tokens are emitting tokens. With bushderby, non-emitting green * nodes are collected into a listed called the * delayedExpansionList. This is a list of green nodes that are * non-emitting. Non-emitting nodes are usually grown immediately * until we find emitting nodes, but with bushderby, we have to * delay this expansion until the node is completely scored. To do * this, the non-emitting green nodes are placed in a * 'delayedExpansionList'. After all normal tokens are grown, the * tokens in this delayedExpansionList are given the second * bushderby pass and then grown. This process repeats until the * delayedExpansionList is empty. * */ protected void growBranches() { getGrowTimer().start(); setBestTokenMap(new HashMap(getActiveList().size() * 10)); int pass = 0; boolean moreTokensToExpand = true; ActiveList oldActiveList = getActiveList(); Iterator iterator = getActiveList().iterator(); setResultList(new LinkedList()); setActiveList(getActiveList().createNew()); while (moreTokensToExpand) { pass++; List delayedExpansionList = new ArrayList(); while (iterator.hasNext()) { Token token = (Token) iterator.next(); collectSuccessorTokens(token, delayedExpansionList); } if (delayedExpansionList != null && delayedExpansionList.size() > 0) { finalizeBushderby(delayedExpansionList.iterator()); iterator = delayedExpansionList.iterator(); if (false) { System.out.println("Pass " + pass + " Processing " + delayedExpansionList.size() + " delayed tokens"); } } else { moreTokensToExpand = false; } } finalizeBushderby(getActiveList().iterator()); getGrowTimer().stop(); } /** * Chase through the list, find all Green nodes and convert the * scores to the final bushderby score */ private void finalizeBushderby(Iterator iterator) { while (iterator.hasNext()) { Token token = (Token) iterator.next(); SearchState state = token.getSearchState(); if (isGreenState(state)) { float logNewScore = (float) (token.getWorkingScore() / bushderbyEta); if (false) { System.out.println("OS: " + token.getScore() + " NS: " + logNewScore); } token.setScore(logNewScore); } } } /** * Returns true if the given SentenceHMMState is considered a GREEN * state by Bushderby. * * @param state the SentenceHMMState to be tested * * @return true if the given SearchState is a GREEN state, false otherwise */ private boolean isGreenState(SearchState state) { return state instanceof HMMSearchState; } /** * Collects the next set of emitting tokens from a token * and accumulates them in the active or result lists * * @param token the token to collect successors from * @param delayedExpansionList the place where tokens that cannot * be immediately expaned are placed. Null if we should always * expand all nodes. */ private void collectSuccessorTokens(Token token, List delayedExpansionList) { // System.out.println("Entering cst..."); // If this is a final state, add it to the final list if (token.isFinal()) { getResultList().add(token); return; } SearchState state = token.getSearchState(); SearchStateArc[] arcs = state.getSuccessors(); // For each successor // calculate the entry score for the token based upon the // predecessor token score and the transition probabilities // if the score is better than the best score encountered for // the SentenceHMMState and frame then create a new token, add // it to the lattice and the SentenceHMMState. // If the token is an emitting token add it to the list, // othewise recursively collect the new tokens successors. for (int i = 0; i < arcs.length; i++) { SearchStateArc arc = arcs[i]; SearchState nextState = arc.getState(); if (filterSuccessors && !isValidTransition(token, nextState)) { continue; } float logLanguageProbability = getLanguageProbability(token, arc); // We're actually multiplying the variables, but since // these come in log(), multiply gets converted to add float logCurrentScore = token.getScore() + logLanguageProbability + arc.getAcousticProbability() + arc.getInsertionProbability(); boolean firstToken = (getBestToken(nextState) == null); boolean greenToken = isGreenState(nextState); float logWorkingScore = firstToken ? LogMath.getLogZero() : getBestToken(nextState).getWorkingScore(); if (firstToken || getBestToken(nextState).getScore() <= logCurrentScore) { // we may want to create green tokens all the time if (greenToken) { Token newToken = token.child( nextState, // the SentenceHMMState logCurrentScore, // the score on entry logLanguageProbability, // entry lang score arc.getInsertionProbability(), // insertion prob getCurrentFrameNumber() // the frame number
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?