parallelsearchmanager.java

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

JAVA
850
字号
        // now expand the delayedExpansionList, which contains the        // CombineTokens created when transitioning from GREEN states        // to RED states (i.e., feature stream states to shared states)	growDelayedExpansionList();        // remove all pruned tokens from the active list of all streams	for (Iterator i = linguist.getFeatureStreams(); i.hasNext();) {            FeatureStream stream = (FeatureStream) i.next();            // remove all the pruned tokens            ActiveList prunedActiveList = activeListFactory.newInstance();            for (Iterator t = stream.getActiveList().iterator();                  t.hasNext();) {                ParallelToken token = (ParallelToken) t.next();                if (!token.isPruned()) {                    prunedActiveList.add(token);                }            }            stream.setActiveList(prunedActiveList);        }	debugPrint(" done Growing");	growTimer.stop();    }    /**     * Grow the delayedExpansionList by first pruning it, and then     * grow it.     */    private void growDelayedExpansionList() {	Iterator iterator = delayedExpansionList.iterator();        while (iterator.hasNext()) {            CombineToken token = (CombineToken) iterator.next();            calculateCombinedScore(token);        }	if (doCombinePruning) {	    delayedExpansionList =		combinedScorePruner.prune(delayedExpansionList);	}	iterator = delayedExpansionList.iterator();		while (iterator.hasNext()) {	    CombineToken token = (CombineToken) iterator.next();            token.setLastCombineTime(currentFrameNumber);            growCombineToken(token);        }    }    /**     * Calculates the combined score of all the ParallelTokens     * in the given CombineToken.     *     * @param token the CombineToken to calculate the combined score of     */    private void calculateCombinedScore(CombineToken token) {	featureScoreCombiner.combineScore(token);    }    /**     * Grows the Tokens in the given ActiveList. Returns a new ActiveList     * of successors.     *     * @param activeList the ActiveList to grow     */    private void growActiveList(ActiveList activeList) {		Iterator iterator = activeList.iterator();		while (iterator.hasNext()) {	    ParallelToken token = (ParallelToken) iterator.next();            growParallelToken(token);	}    }    /**     * Grows the given ParallelToken, put the successor Token(s) into the      * given ActiveList, and any possible results in the given resultList.     *     * @param token the Token to grow     */    private void growParallelToken(ParallelToken token) {        /*        debugPrint("Entering growParallelToken: " +                    token.getSearchState().toString());        */	// If this is a final state, add it to the result list.	assert !token.isFinal();	int nextFrameNumber = token.getFrameNumber();	// If this is an emitting state, we increase the frame number	if (token.isEmitting()) {	    nextFrameNumber++;	}	SentenceHMMState state = (SentenceHMMState) token.getSearchState();	SearchStateArc[] arcs = state.getSuccessors();	// expand into each successor states	for (int i = 0; i < arcs.length; i++) {	    SearchStateArc arc = arcs[i];	    SentenceHMMState nextState = (SentenceHMMState) arc.getState();	    // debugPrint("  Entering " + nextState.toString());	    float logEntryScore = token.getScore() + arc.getProbability();            Token oldNextToken = getBestToken(nextState);	    boolean firstToken = oldNextToken == null ||                oldNextToken.getFrameNumber() != nextFrameNumber;            // RED states are the unsplitted states, or the non-feature            // stream states            // GREEN states are the splitted states, or the feature stream            // states                        if (nextState.getColor() == Color.RED) {                // debugPrint(". RED state");                CombineToken nextToken;                if (firstToken) {                    // if this is the first incoming ParallelToken,                     // create a CombineToken and set it as the best                    // (and only) CombineToken of the state                    nextToken = new CombineToken                        (token, nextState, nextFrameNumber);                    setBestToken(nextState, nextToken);                    delayedExpansionList.add(nextToken);                } else {                    // get the combine token at the next state                    nextToken = (CombineToken) getBestToken(nextState);                }                assert (nextToken.getFrameNumber() == nextFrameNumber);                ParallelToken oldParallelToken =                     nextToken.getParallelToken(token.getFeatureStream());                // if this is the first token, or if this score is                // greater than the old one in the next CombineToken                // add this token or replace the old one with this token                if (firstToken || oldParallelToken == null ||                    oldParallelToken.getScore() <= logEntryScore) {                    ParallelToken newToken = new ParallelToken                        (token,                         nextState,                         logEntryScore,                         token.getCombinedScore(),                         nextFrameNumber,			 token.getLastCombineTime());                    // add this ParallelToken to the CombineToken.                    nextToken.addParallelToken(newToken.getFeatureStream(),					       newToken);                }            } else if (nextState.getColor() == Color.GREEN) {                // debugPrint("  . GREEN state");                                if (firstToken ||                     getBestToken(nextState).getScore() <= logEntryScore) {                    // debugPrint("  . adding parallel token");                    ParallelToken newToken = new ParallelToken                        (token,                         nextState,                         logEntryScore,                         token.getCombinedScore(),                          nextFrameNumber,			 token.getLastCombineTime());                    if (newToken.isEmitting()) {                        // this is an emitting token (or an emitting state)                        replaceParallelToken(nextState, newToken);                        combinedActiveList.add(newToken);                    } else {                        growParallelToken(newToken);                    }		}	    } else {		throw new IllegalStateException		    ("Color of state " + nextState.getName() +		     " not RED or GREEN, its " + 		     nextState.getColor().toString() + "!");	    }	}    }    /**     * Returns the best Token for the given SearchState.     *     * @param state the SearchState to look for     *     * @return the best Token for the given SearchState     */    private Token getBestToken(SearchState state) {        return (Token) bestTokenMap.get(state);    }    /**     * Sets the best Token for the given SearchState     *     * @param state the SearchState     * @param token the best Token for the given SearchState     */    private Token setBestToken(SearchState state, Token token) {        return (Token) bestTokenMap.put(state, token);    }    /**     * Grows the given CombineToken and puts any results into the given     * list.     *     * @param token the CombineToken to grow     */    private void growCombineToken(CombineToken token) {        // debugPrint("Entering growCombineToken");	// If this is a final state, add it to the result list.	if (token.isFinal()) {	    resultList.add(token);	    // debugPrint("FINAL RESULT found!");	}        int nextFrameNumber = token.getFrameNumber();	// make sure that this state is non-emitting	assert !token.isEmitting();	SearchState state = (SearchState) token.getSearchState();	SearchStateArc[] arcs = (SearchStateArc[]) state.getSuccessors();	// expand into each successor states	for (int a = 0; a < arcs.length; a++) {	    SentenceHMMStateArc arc = (SentenceHMMStateArc) arcs[a];	    SentenceHMMState nextState = (SentenceHMMState) arc.getState();	    // debugPrint("Entering " + nextState.toString());            Token oldNextToken = getBestToken(nextState);	    boolean firstToken =                 (oldNextToken == null ||                 oldNextToken.getFrameNumber() != nextFrameNumber);            // RED states are the unsplitted states, or the non-feature            // stream states            // GREEN states are the splitted states, or the feature stream            // states            if (nextState.getColor() == Color.RED) {                                float logEntryScore = token.getScore() + arc.getProbability();                if (firstToken || oldNextToken.getScore() <= logEntryScore) {                                        // create the next CombineToken for a RED state                    CombineToken nextToken = new CombineToken                        (token, nextState, nextFrameNumber);                    // propagate the combined score unchanged                    nextToken.setScore(logEntryScore);                    // propagate the individual ParallelTokens, taking                    // into account the new transition score                    transitionParallelTokens                        (token, nextToken, arc.getProbability());                                        setBestToken(nextState, nextToken);                    // finally grow the new CombineToken                    growCombineToken(nextToken);                }            } else if (nextState.getColor() == Color.GREEN) {                ParallelState pState = (ParallelState) nextState;                                ParallelToken parallelToken = token.getParallelToken                    (pState.getFeatureStream());                // we continue into a GREEN/feature stream state only                // if there is a ParallelToken for that feature stream		if (parallelToken != null) {		    		    float logEntryScore = arc.getProbability() + 			parallelToken.getFeatureScore();		    ParallelToken oldToken = (ParallelToken)oldNextToken;		    if (firstToken ||                         oldToken.getFeatureScore() <= logEntryScore) {						ParallelToken nextToken = new ParallelToken			    (parallelToken, 			     nextState,			     logEntryScore,                             parallelToken.getCombinedScore(),			     nextFrameNumber,			     parallelToken.getLastCombineTime());                        // replace the oldBestToken with this new token			if (nextState.isEmitting()) {			    // call the replaceParallelToken method			    replaceParallelToken(nextState, nextToken);			    combinedActiveList.add(nextToken);			} else {			    growParallelToken(nextToken);			}		    }		}	    } else {		throw new IllegalStateException		    ("Color of state not RED or GREEN!");	    }        }    }    /**     * Propagates all the ParallelToken(s) within the given old CombineToken     * to the given new CombineToken. The new ParallelToken(s) in the     * new CombineToken will have the transition score incorporated.     *     * @param oldToken the previous CombineToken     * @param newToken the new CombineToken     * @param transitionScore the transition score from oldToken to newToken     */    private void transitionParallelTokens(CombineToken oldToken,                                          CombineToken newToken,                                          float transitionScore) {        // propagate the individual ParallelTokens, taking        // into account the new transition scores        for (Iterator i = oldToken.getTokenIterator(); i.hasNext();) {                        ParallelToken pToken = (ParallelToken) i.next();            ParallelToken newParallelToken = new ParallelToken                (pToken,                 (SentenceHMMState) newToken.getSearchState(),                 pToken.getFeatureScore() + transitionScore,                 pToken.getCombinedScore(),                 pToken.getFrameNumber(),		 pToken.getLastCombineTime());                        newToken.addParallelToken(newParallelToken.getFeatureStream(),				      newParallelToken);        }    }    /**     * Replaces the token in the given SentenceHMMState with      * the given newToken.     * This method will automatically find the correct ActiveList.     *     * @param state the state of the current token     * @param newToken the new Token     */    private void replaceParallelToken(SentenceHMMState state,                                      ParallelToken newToken) {        ParallelToken oldToken = (ParallelToken) setBestToken(state, newToken);                ActiveList activeList = newToken.getFeatureStream().getActiveList();        activeList.add(newToken);                        if (oldToken != null) {            oldToken.setPruned(true);        }    }    /**     * Performs post-recognition cleanup. This method should be called     * after recognize returns a final result.     */    public void stopRecognition() {	scorer.stopRecognition();        if (doFeaturePruning) {            featureScorePruner.stopRecognition();	}	if (doCombinePruning) {            combinedScorePruner.stopRecognition();        }	linguist.stopRecognition();        bestTokenMap = new HashMap();    }    /*     * (non-Javadoc)     *     * @see edu.cmu.sphinx.decoder.search.SearchManager#deallocate()     */    public void deallocate() {        scorer.deallocate();	if (doFeaturePruning) {	    featureScorePruner.deallocate();	}	if (doCombinePruning) {	    combinedScorePruner.deallocate();	}        linguist.deallocate();    }    }

⌨️ 快捷键说明

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