parallelsearchmanager.java

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

JAVA
850
字号
/* * Copyright 1999-2004 Carnegie Mellon University.   * Portions Copyright 2004 Sun Microsystems, Inc.   * Portions Copyright 2004 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.parallel;import edu.cmu.sphinx.linguist.acoustic.AcousticModel;import edu.cmu.sphinx.result.Result;import edu.cmu.sphinx.decoder.scorer.AcousticScorer;import edu.cmu.sphinx.decoder.scorer.Scoreable;import edu.cmu.sphinx.decoder.search.ActiveList;import edu.cmu.sphinx.decoder.search.ActiveListFactory;import edu.cmu.sphinx.decoder.search.SearchManager;import edu.cmu.sphinx.decoder.pruner.Pruner;import edu.cmu.sphinx.linguist.Linguist;import edu.cmu.sphinx.linguist.SearchGraph;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.SearchStateArc;import edu.cmu.sphinx.linguist.flat.Color;import edu.cmu.sphinx.linguist.flat.SentenceHMMState;import edu.cmu.sphinx.linguist.flat.SentenceHMMStateArc;import edu.cmu.sphinx.decoder.search.Token;import edu.cmu.sphinx.util.LogMath;import edu.cmu.sphinx.util.SphinxProperties;import edu.cmu.sphinx.util.Timer;import edu.cmu.sphinx.util.props.Configurable;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;import java.io.IOException;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.LinkedList;import java.util.Map;/** * Performs recognition on parallel feature streams. */public class ParallelSearchManager implements SearchManager {    /**     * The sphinx property name for the active list type.     */    public static final String PROP_ACTIVE_LIST_FACTORY = "activeListFactory";    /**     * The sphinx property name for whether to do feature pruning.     */    public static final String PROP_DO_FEATURE_PRUNING = "doFeaturePruning";    /**     * The default value for whether to do feature pruning, which is false.     */    public static final boolean PROP_DO_FEATURE_PRUNING_DEFAULT = false;    /**     * The sphinx property for the feature score pruner.     */    public static final String PROP_FEATURE_SCORE_PRUNER = "featureScorePruner";    /**     * The sphinx property name for whether to do combine pruning.     */    public static final String PROP_DO_COMBINE_PRUNING = "doCombinePruning";    /**     * The default value for whether to do combine pruning, which is false.     */    public static final boolean PROP_DO_COMBINE_PRUNING_DEFAULT = false;    /**     * The sphinx property for the combined score pruner.     */    public static final String PROP_COMBINED_SCORE_PRUNER =         "combinedScorePruner";    /**     * The sphinx property for scorer used.     */    public static final String PROP_SCORER = "scorer";    /**     * The sphinx property for linguist used.     */    public static final String PROP_LINGUIST = "linguist";    /**     * The sphinx property for the log math used.     */    public static final String PROP_LOG_MATH = "logMath";    private String name;    private ParallelSimpleLinguist linguist;    private AcousticScorer scorer;    private Pruner featureScorePruner;    private Pruner combinedScorePruner;    private ScoreCombiner featureScoreCombiner;    private LogMath logMath;    private int currentFrameNumber;           // the current frame number    private ActiveListFactory activeListFactory;    private ActiveList combinedActiveList;    // ActiveList for common states    private ActiveList delayedExpansionList;  // for tokens at CombineStates    private List resultList;    private Map bestTokenMap;    private Timer scoreTimer;    private Timer pruneTimer;    private Timer growTimer;    private boolean doFeaturePruning;    private boolean doCombinePruning;    /* (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_LINGUIST, PropertyType.COMPONENT);        registry.register(PROP_SCORER, PropertyType.COMPONENT);        registry.register(PROP_ACTIVE_LIST_FACTORY, PropertyType.COMPONENT);        registry.register(PROP_DO_FEATURE_PRUNING, PropertyType.BOOLEAN);        registry.register(PROP_DO_COMBINE_PRUNING, PropertyType.BOOLEAN);        registry.register(PROP_FEATURE_SCORE_PRUNER, PropertyType.COMPONENT);        registry.register(PROP_COMBINED_SCORE_PRUNER, PropertyType.COMPONENT);        registry.register(PROP_LOG_MATH, PropertyType.COMPONENT);    }    /*     * (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);        linguist = (ParallelSimpleLinguist) ps.getComponent            (PROP_LINGUIST, ParallelSimpleLinguist.class);        scorer = (AcousticScorer) ps.getComponent            (PROP_SCORER, AcousticScorer.class);	activeListFactory = (ActiveListFactory) ps.getComponent            (PROP_ACTIVE_LIST_FACTORY, ActiveListFactory.class);	this.doFeaturePruning = ps.getBoolean	    (PROP_DO_FEATURE_PRUNING, PROP_DO_FEATURE_PRUNING_DEFAULT);	this.doCombinePruning = ps.getBoolean	    (PROP_DO_COMBINE_PRUNING, PROP_DO_COMBINE_PRUNING_DEFAULT);	if (doFeaturePruning) {	    featureScorePruner = (FeatureScorePruner) ps.getComponent                (PROP_FEATURE_SCORE_PRUNER, FeatureScorePruner.class);        }	if (doCombinePruning) {	    combinedScorePruner = (CombinedScorePruner) ps.getComponent                (PROP_COMBINED_SCORE_PRUNER, CombinedScorePruner.class);	}	        featureScoreCombiner = new FeatureScoreCombiner();        scoreTimer = Timer.getTimer("Score");        pruneTimer = Timer.getTimer("Prune");        growTimer = Timer.getTimer("Grow");            }    /*     * (non-Javadoc)     *     * @see edu.cmu.sphinx.decoder.search.SearchManager#allocate()     */    public void allocate() throws IOException {        bestTokenMap = new HashMap();        linguist.allocate();	if (doFeaturePruning) {	    featureScorePruner.allocate();	}	if (doCombinePruning) {	    combinedScorePruner.allocate();	}        scorer.allocate();    }        /*     * (non-Javadoc)     *     * @see edu.cmu.sphinx.util.props.Configurable#getName()     */    public String getName() {        return name;    }    /**     * Prints a debug message.     *     * @param message the debug message to print     */    private void debugPrint(String message) {	if (false) {	    System.out.println(message);	}    }    /**     * Prepares the SearchManager for recognition.  This method must     * be called before <code> recognize </code> is called.     */    public void startRecognition() {	currentFrameNumber = 0;	linguist.startRecognition();        if (doFeaturePruning) {            featureScorePruner.startRecognition();	}	if (doCombinePruning) {            combinedScorePruner.startRecognition();        }	scorer.startRecognition();	createInitialLists();    }    /**     * Creates the ActiveLists used for decoding. There is one ActiveList     * created for each feature stream (or acoustic model), and also an     * ActiveList to do the overall pruning.     */    private void createInitialLists() {                combinedActiveList = activeListFactory.newInstance();        delayedExpansionList = activeListFactory.newInstance();                SentenceHMMState firstState = (SentenceHMMState)            linguist.getSearchGraph().getInitialState();                // create the first token and grow it, its first parameter        // is null because it has no predecessor        CombineToken firstToken = new CombineToken            (null, firstState, currentFrameNumber);                setBestToken(firstState, firstToken);                for (Iterator i = linguist.getFeatureStreams(); i.hasNext();) {            FeatureStream stream = (FeatureStream) i.next();            stream.setActiveList(activeListFactory.newInstance());                        // add the first ParallelTokens to the CombineToken            ParallelToken token = new ParallelToken                (firstState, stream, currentFrameNumber);            token.setLastCombineTime(currentFrameNumber);            firstToken.addParallelToken(stream, token);        }                // grow the first CombineToken until we've reach emitting states        resultList = new LinkedList();                calculateCombinedScore(firstToken);	        growCombineToken(firstToken);    }    /**     * Performs recognition. Processes no more than the given number     * of frames before returning. This method returns a partial     * result after nFrames have been processed, or a final result if     * recognition completes while processing frames.  If a final     * result is returned, the actual number of frames processed can     * be retrieved from the result.  This method may block while     * waiting for frames to arrive.     *     * @param nFrames the maximum number of frames to process. A     * final result may be returned before all nFrames are processed.     *     * @return the recognition result. The result may be a partial or     * a final result.     */    public Result recognize(int nFrames) {	boolean done = false;	Result result;        for (int i = 0; i < nFrames && !done; i++) {	    done = recognize();	}	result = new Result	    (combinedActiveList, resultList, currentFrameNumber, done, logMath);	return result;    }    /**     * Performs recognition for one frame. Returns true if recognition     * has been completed.     *     * @return <code>true</code> if recognition is completed.     */    private boolean recognize() {        debugPrint("-----\nFrame: " + currentFrameNumber);	boolean moreTokens = score();        if (moreTokens) {	    prune();	    grow();	    currentFrameNumber++;	}        debugPrint("-----");	return !moreTokens;    }    /**     * Calculate the acoustic scores for the active lists for the     * acoustic models, which should contain only emitting tokens.     *     * @return true if there are more tokens, otherwise false     */    private boolean score() {	scoreTimer.start();	debugPrint("Scoring");	boolean moreFeatures = false;	for (Iterator i = linguist.getFeatureStreams(); i.hasNext();) {            FeatureStream stream = (FeatureStream) i.next();	    Scoreable scoreable = 		scorer.calculateScores(stream.getActiveList().getTokens());	    moreFeatures = (scoreable != null);    	}	debugPrint(" done Scoring");	scoreTimer.stop();	return moreFeatures;    }    /**     * Removes unpromising branches from the active list     *     */    private void prune() {	pruneTimer.start();        debugPrint("Pruning");	if (doFeaturePruning) {	    for (Iterator i = linguist.getFeatureStreams(); i.hasNext();) {		FeatureStream stream = (FeatureStream) i.next();	                stream.setActiveList                    (featureScorePruner.prune(stream.getActiveList()));            }	}	debugPrint(" done Pruning");        pruneTimer.stop();    }    /**     * Prints all the active lists.     */    private void printActiveLists() {        debugPrint(" CombinedActiveList: " + combinedActiveList.size());        for (Iterator i = linguist.getFeatureStreams(); i.hasNext();) {            FeatureStream stream = (FeatureStream) i.next();	            debugPrint(" ActiveList, " + stream.getName() + ": " +                       stream.getActiveList().size());        }    }    /**     * 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.     *     */    private void grow() {	growTimer.start();	debugPrint("Growing");        resultList = new LinkedList();        combinedActiveList = activeListFactory.newInstance();	delayedExpansionList = activeListFactory.newInstance();        // grow the ActiveList of each feature stream	for (Iterator i = linguist.getFeatureStreams(); i.hasNext();) {            FeatureStream stream = (FeatureStream) i.next();            // create a new ActiveList for the next frame            ActiveList oldActiveList = stream.getActiveList();            stream.setActiveList(activeListFactory.newInstance());            	    growActiveList(oldActiveList);	}

⌨️ 快捷键说明

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