wordpruningbreadthfirstsearchmanager.java

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

JAVA
1,685
字号
/* * 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;// a test search manager.import java.io.IOException;import java.util.Arrays;import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.logging.Logger;import java.util.logging.Level;import java.util.Map;import java.util.Set;import edu.cmu.sphinx.decoder.pruner.Pruner;import edu.cmu.sphinx.decoder.scorer.AcousticScorer;import edu.cmu.sphinx.linguist.HMMSearchState;import edu.cmu.sphinx.linguist.Linguist;import edu.cmu.sphinx.linguist.acoustic.HMM;import edu.cmu.sphinx.linguist.acoustic.HMMPosition;import edu.cmu.sphinx.linguist.SearchGraph;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.UnitSearchState;import edu.cmu.sphinx.linguist.SearchStateArc;import edu.cmu.sphinx.linguist.WordSearchState;import edu.cmu.sphinx.linguist.WordSequence;import edu.cmu.sphinx.linguist.dictionary.Word;import edu.cmu.sphinx.result.Result;import edu.cmu.sphinx.util.LogMath;import edu.cmu.sphinx.util.StatisticsVariable;import edu.cmu.sphinx.util.Timer;import edu.cmu.sphinx.util.props.PropertyException;import edu.cmu.sphinx.util.props.PropertySheet;import edu.cmu.sphinx.util.props.PropertyType;import edu.cmu.sphinx.util.props.Registry;/** * 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. *  * All scores and probabilities are maintained in the log math log domain. */public class WordPruningBreadthFirstSearchManager implements SearchManager {    /**     * Sphinx property that defines the name of the linguist to be used by this     * search manager.     */    public final static String PROP_LINGUIST = "linguist";    /**     * Sphinx property that defines the name of the linguist to be used by this     * search manager.     */    public final static String PROP_PRUNER = "pruner";    /**     * Sphinx property that defines the name of the scorer to be used by this     * search manager.     */    public final static String PROP_SCORER = "scorer";    /**     * Sphinx property that defines the name of the logmath to be used by this     * search manager.     */    public final static String PROP_LOG_MATH = "logMath";    /**     * A sphinx property than, when set to <code>true</code> will cause the     * recognizer to count up all the tokens in the active list after every     * frame.     */    public final static String PROP_SHOW_TOKEN_COUNT = "showTokenCount";    /**     * The default value for the PROP_SHOW_TOKEN_COUNT property     */    public final static boolean PROP_SHOW_TOKEN_COUNT_DEFAULT = false;    /**     * The default value for the PROP_WANT_ENTRY_PRUNING property     */    public final static boolean PROP_WANT_ENTRY_PRUNING_DEFAULT = false;    /**     * A sphinx property that controls the number of frames processed for every     * time the decode growth step is skipped. Setting this property to zero     * disables grow skipping. Setting this number to a small integer will     * increase the speed of the decoder but will also decrease its accuracy.     * The higher the number, the less often the grow code is skipped.     */    public final static String PROP_GROW_SKIP_INTERVAL = "growSkipInterval";    /**     * The default value for the PROP_GROW_SKIP_INTERVAL property.     */    public final static int PROP_GROW_SKIP_INTERVAL_DEFAULT = 0;    /**     * Sphinx property that defines the type of active list to use     */    public final static String PROP_ACTIVE_LIST_MANAGER = "activeListManager";    /**     * Sphinx property for checking if the order of states is valid.     */    public final static String PROP_CHECK_STATE_ORDER = "checkStateOrder";    /**     * The default value of the PROP_CHECK_STATE_ORDER property.     */    public final static boolean PROP_CHECK_STATE_ORDER_DEFAULT = false;    /**     * Sphinx property that specifies whether to build a word lattice.     */    public final static String PROP_BUILD_WORD_LATTICE = "buildWordLattice";    /**     * The default value of the PROP_BUILD_WORD_LATTICE property.     */    public final static boolean PROP_BUILD_WORD_LATTICE_DEFAULT = true;    /**     * Sphinx property that specifies the maximum lattice edges     */    public final static String PROP_MAX_LATTICE_EDGES = "maxLatticeEdges";    /**     * The default value of the PROP_MAX_LATTICE_EDGES property.     */    public final static int PROP_MAX_LATTICE_EDGES_DEFAULT = 100;    /**     * A sphinx property that controls the amount of simple acoustic lookahead     * performed. Setting the property to zero (the default) disables simple     * acoustic lookahead. The lookahead need not be an integer.     */    public final static String PROP_ACOUSTIC_LOOKAHEAD_FRAMES = "acousticLookaheadFrames";    /**     * The default value for the PROP_ACOUSTIC_LOOKAHEAD_FRAMES property.     */    public final static float PROP_ACOUSTIC_LOOKAHEAD_FRAMES_DEFAULT = 0F;    /**     * A sphinx property that controls whether or not we keep all tokens. If     * this is set to false, only word tokens are retained, otherwise all     * tokens are retained.     *       */    public final static String PROP_KEEP_ALL_TOKENS = "keepAllTokens";    /**     * The default value for the PROP_ACOUSTIC_LOOKAHEAD_FRAMES property.     */    public final static boolean PROP_KEEP_ALL_TOKENS_DEFAULT = false;    /**     * Sphinx4 property that specifies the relative beam width     */    public final static String PROP_RELATIVE_BEAM_WIDTH = "relativeBeamWidth";    /**     * Sphinx4 property that specifies the default value for the relative beam     * width     */    public final static float PROP_RELATIVE_BEAM_WIDTH_DEFAULT = 0.0f;    // TODO: since the token stacks are permanently disabled,    // we may want to just remove all of the supporting code    private final static boolean wantTokenStacks = false;    // -----------------------------------    // Configured Subcomponents    // -----------------------------------    private Linguist linguist; // Provides grammar/language info    private Pruner pruner; // used to prune the active list    private AcousticScorer scorer; // used to score the active list    private ActiveListManager activeListManager;    private LogMath logMath;    // -----------------------------------    // Configuration data    // -----------------------------------    private String name;    private Logger logger;    private boolean showTokenCount;    private boolean checkStateOrder;    private boolean buildWordLattice;    private boolean keepAllTokens = false;    private int growSkipInterval = 0;    private float relativeBeamWidth;    private float acousticLookaheadFrames = 0.0f;    private int maxTokenHeapSize = 3;    private int maxLatticeEdges = 100;    // -----------------------------------    // Instrumentation    // -----------------------------------    private Timer scoreTimer;    private Timer pruneTimer;    private Timer growTimer;    private StatisticsVariable totalTokensScored;    private StatisticsVariable curTokensScored;    private StatisticsVariable tokensCreated;    private long tokenSum = 0;    private int tokenCount = 0;    // -----------------------------------    // Working data    // -----------------------------------    private int currentFrameNumber; // the current frame number    private ActiveList activeList; // the list of active tokens    private List resultList; // the current set of results    private Map bestTokenMap;    private AlternateHypothesisManager loserManager;    private int numStateOrder;    // private TokenTracker tokenTracker;    // private TokenTypeTracker tokenTypeTracker;    private Map skewMap;    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.util.props.Configurable#register(java.lang.String,     *      edu.cmu.sphinx.util.props.Registry)     */    public void register(String name, Registry registry)            throws PropertyException {        this.name = name;        registry.register(PROP_LOG_MATH, PropertyType.COMPONENT);        registry.register(PROP_LINGUIST, PropertyType.COMPONENT);        registry.register(PROP_PRUNER, PropertyType.COMPONENT);        registry.register(PROP_SCORER, PropertyType.COMPONENT);        registry.register(PROP_ACTIVE_LIST_MANAGER, PropertyType.COMPONENT);        registry.register(PROP_SHOW_TOKEN_COUNT, PropertyType.BOOLEAN);        registry.register(PROP_GROW_SKIP_INTERVAL, PropertyType.INT);        registry.register(PROP_CHECK_STATE_ORDER, PropertyType.BOOLEAN);        registry.register(PROP_BUILD_WORD_LATTICE, PropertyType.BOOLEAN);        registry.register(PROP_MAX_LATTICE_EDGES, PropertyType.INT);        registry.register(PROP_ACOUSTIC_LOOKAHEAD_FRAMES, PropertyType.FLOAT);        registry.register(PROP_KEEP_ALL_TOKENS, PropertyType.BOOLEAN);        registry.register(PROP_RELATIVE_BEAM_WIDTH, PropertyType.DOUBLE);    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.util.props.Configurable#newProperties(edu.cmu.sphinx.util.props.PropertySheet)     */    public void newProperties(PropertySheet ps) throws PropertyException {        logMath = (LogMath) ps.getComponent(PROP_LOG_MATH, LogMath.class);        logger = ps.getLogger();        linguist = (Linguist) ps.getComponent(PROP_LINGUIST, Linguist.class);        pruner = (Pruner) ps.getComponent(PROP_PRUNER, Pruner.class);        scorer = (AcousticScorer) ps.getComponent(PROP_SCORER,                AcousticScorer.class);        activeListManager = (ActiveListManager) ps.getComponent(                PROP_ACTIVE_LIST_MANAGER, ActiveListManager.class);        showTokenCount = ps.getBoolean(PROP_SHOW_TOKEN_COUNT,                PROP_SHOW_TOKEN_COUNT_DEFAULT);        growSkipInterval = ps.getInt(PROP_GROW_SKIP_INTERVAL,                PROP_GROW_SKIP_INTERVAL_DEFAULT);        checkStateOrder = ps.getBoolean(PROP_CHECK_STATE_ORDER,                PROP_CHECK_STATE_ORDER_DEFAULT);        buildWordLattice = ps.getBoolean(PROP_BUILD_WORD_LATTICE,                PROP_BUILD_WORD_LATTICE_DEFAULT);        maxLatticeEdges = ps.getInt(PROP_MAX_LATTICE_EDGES,                PROP_MAX_LATTICE_EDGES_DEFAULT);        acousticLookaheadFrames = ps.getFloat(PROP_ACOUSTIC_LOOKAHEAD_FRAMES,                PROP_ACOUSTIC_LOOKAHEAD_FRAMES_DEFAULT);        keepAllTokens = ps.getBoolean(PROP_KEEP_ALL_TOKENS,                PROP_KEEP_ALL_TOKENS_DEFAULT);        double linearRelativeBeamWidth = ps.getDouble(PROP_RELATIVE_BEAM_WIDTH,                PROP_RELATIVE_BEAM_WIDTH_DEFAULT);        this.relativeBeamWidth = logMath.linearToLog(linearRelativeBeamWidth);    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.util.props.Configurable#getName()     */    public String getName() {        return name;    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.decoder.search.SearchManager#allocate()     */    public void allocate() throws IOException {        // tokenTracker = new TokenTracker();        // tokenTypeTracker = new TokenTypeTracker();        scoreTimer = Timer.getTimer("Score");        pruneTimer = Timer.getTimer("Prune");        growTimer = Timer.getTimer("Grow");        totalTokensScored = StatisticsVariable                .getStatisticsVariable("totalTokensScored");        curTokensScored = StatisticsVariable                .getStatisticsVariable("curTokensScored");        tokensCreated = StatisticsVariable                .getStatisticsVariable("tokensCreated");                linguist.allocate();        pruner.allocate();        scorer.allocate();    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.decoder.search.SearchManager#deallocate()     */    public void deallocate() {        scorer.deallocate();        pruner.deallocate();        linguist.deallocate();    }    /**     * Called at the start of recognition. Gets the search manager ready to     * recognize     */    public void startRecognition() {        linguist.startRecognition();        pruner.startRecognition();        scorer.startRecognition();        localStart();    }    /**     * Performs the recognition for the given number of frames.     *      * @param nFrames     *                the number of frames to recognize     *      * @return the current result     */    public Result recognize(int nFrames) {        boolean done = false;        Result result;        for (int i = 0; i < nFrames && !done; i++) {            // System.out.println("Frame " + currentFrameNumber);            // score the emitting list            // tokenTracker.startFrame();            activeList = activeListManager.getEmittingList();            if (activeList != null) {                do {                    currentFrameNumber++;                    done = !scoreTokens();                } while (!done                        && (growSkipInterval > 1 &&                             ((currentFrameNumber % growSkipInterval) == 0)));                if (!done) {                    bestTokenMap = createBestTokenMap();                    // prune and grow the emitting list                    pruneBranches();                    resultList = new LinkedList();                    growEmittingBranches();                    // prune and grow the non-emitting lists                    // activeListManager.dump();                    growNonEmittingLists();                }            }            // tokenTracker.stopFrame();        }        result = new Result(loserManager, activeList, resultList,                currentFrameNumber, done, logMath);       // tokenTypeTracker.show();       if (showTokenCount) {            showTokenCount();        }        return result;    }    /**     * creates a new best token map with the best size     *      * @return the best token map     */    private Map createBestTokenMap() {        // int mapSize = activeList.size() * 10;        int mapSize = activeList.size() * 4;        if (mapSize == 0) {            mapSize = 1;        }        return new HashMap(mapSize, 0.3F);    }    /**     * Terminates a recognition     */    public void stopRecognition() {        localStop();        scorer.stopRecognition();        pruner.stopRecognition();        linguist.stopRecognition();    }

⌨️ 快捷键说明

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