bushderbysearchmanager.java

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

JAVA
569
字号
		    );                    getTokensCreated().value++;		    newToken = collapseToken(newToken);		    Token oldBestToken = setBestToken(newToken, nextState);		    if (!newToken.isEmitting()) {			if (greenToken && delayedExpansionList != null) {			    if (oldBestToken != null && 				oldBestToken.getScore() <= logCurrentScore) {			        int oldTokenIdx = 				    delayedExpansionList.indexOf(oldBestToken);				if (oldTokenIdx >= 0)				    delayedExpansionList.remove(oldTokenIdx);		            }			    delayedExpansionList.add(newToken);			} else  {			    // System.out.println("Recursing into cst...");			    collectSuccessorTokens(newToken,                                                    delayedExpansionList);			}                    } else if (firstToken) {                        getActiveList().add(newToken);                    } else {			getActiveList().replace(oldBestToken, newToken); 		    }		}	    } 	    // with Bushderby nodes are either 'combine' or 'compete'	    // Green nodes are 'combine' nodes.  If bushderby is not	    // enabled, all nodes are considered to be 'compete'	    // nodes.	    //	    // If bushderby is enabled then we accumulate a working	    // score for this token which combines the working scores	    // for all paths into this token	    if (greenToken) {		Token bestToken = getBestToken(nextState);		if (bestToken != null) {		    logWorkingScore =  getLogMath().addAsLinear(                            logWorkingScore, 			    (float)(logCurrentScore * bushderbyEta));		    bestToken.setWorkingScore(logWorkingScore);		}		if (false) {		    System.out.println("CS: " + logCurrentScore +			     " WS: " + logWorkingScore);		}	    }        }    }    /**     * Deterimines if the transition from the given token (given its     * perfect knowledge of context history) to the nextState is valid     *     * @param token the current token     * @param nextState the next sentence hmm state     *     * @return true if the transition is valid     *     */    private boolean isValidTransition(Token token, SearchState nextState) {	Unit prevUnit;	Unit thisUnit;	Unit[] thisLC;	Unit[] thisRC;	Unit[] prevLC;	Unit[] prevRC;	// if we are not transitioning to a unit state, then it is a	// valid transition	if (! (nextState instanceof UnitSearchState)) {	    return true;	}	// if we are transitioning to a unit state we have to check to	// make sure that the contexts (left and right) of the	// previous unit align properly with those of the next	thisUnit = ((UnitSearchState) nextState).getUnit();	thisLC = getLeftContext(thisUnit);	thisRC = getRightContext(thisUnit);	prevUnit = getPreviousUnit(token);	// if there is no previous unit, then the next unit's left	// context should be empty	if (prevUnit == null) {	    return thisLC.length == 0;	} 	prevLC = getLeftContext(prevUnit);	prevRC = getRightContext(prevUnit);	// we have a transition between units, 	// prev  RC had better not be empty or its not a valid transition	// this  LC had better not be empty or its not a valid transition	if (prevRC != null && (prevRC.length == 0 || thisLC.length == 0)) {	    return false;	}	// if the previous right context is longer than the new unit	// and its right context then its not a valid transition	if (prevRC != null && thisRC != null 		&& prevRC.length > (thisRC.length + 1)) {	    return false;	}	// if this left context is longer than the previous left	// context and unit then its not a valid transition	if (thisLC.length > (prevLC.length + 1)) {	    return false;	}	// now check the the previous right context matches this unit	// and its context	if (prevRC != null && !prevRC[0].getName().equals(thisUnit.getName())) {	    return false;	}	for (int i = 1; prevRC != null && thisRC != null 				&& i < prevRC.length; i++) {	    if (prevRC[i] != thisRC[i - 1]) {		return false;	    }	}	// now check that this left context matches the previous  unit	// and its context	if (!thisLC[0].getName().equals(prevUnit.getName())) {	    return false;	}	for (int i = 1; i < thisLC.length; i++) {	    if (thisLC[i] != prevLC[i - 1]) {		return false;	    }	}	// if we made it to here, its a valid unit-to-unit transition	// so we can return true	return true;    }    /**     * Given a unit return its left context     *     * @param unit the unit of interest     *     * @return the left context     */    private Unit[] getLeftContext(Unit unit) {	return ((LeftRightContext) unit.getContext()).getLeftContext();    }    /**     * Given a unit return its right context     *     * @param unit the unit of interest     *     * @return the right context     */    private Unit[] getRightContext(Unit unit) {	return ((LeftRightContext) unit.getContext()).getRightContext();    }    /**     * Given a token return the previous unit     *     * @param token the token where the unit search is started     *     * @return the unit or null if no unit is found     */    private Unit getPreviousUnit(Token token) {	while (token != null) {	    if (token.getSearchState() instanceof UnitSearchState) {		return ((UnitSearchState) token.getSearchState()).getUnit();	    }	    token = token.getPredecessor();	}	return null;    }    /**     * Given a linguist and an arc to the next token, determine a     * language probability for the next state     *     * @param token the next token     *     * @param arc the arc to the next state     *     * @return the language probability for the transition to the next     * state (in LogMath log domain)     */    private float getLanguageProbability(Token token, 					 SearchStateArc arc) {	float logProbability = arc.getLanguageProbability();	if (languageModel != null && arc.getState() instanceof WordSearchState) {	    WordSearchState state = (WordSearchState) arc.getState();	    int depth = languageModel.getMaxDepth();	    Word word = state.getPronunciation().getWord();            	    if (isWord(word)) {		List wordList = new ArrayList(depth);		wordList.add(word);		while (token != null && wordList.size() < depth) {		    if (token.getSearchState() 				    instanceof WordSearchState) {			WordSearchState prevWord =			    (WordSearchState) token.getSearchState();			Word prevWordObject =			    prevWord.getPronunciation().getWord();			if (isWord(prevWordObject)) {			    wordList.add(prevWordObject);			}		    }		    token = token.getPredecessor();		}		if (token == null && wordList.size() < depth) {                    // bug: should be adding a word instead, but we                    // don't have the dictionary, so we can't get the                    // sentence start word                    wordList.add(null);                    // new Word(Dictionary.SENTENCE_START_SPELLING,                    // null));		}		Collections.reverse(wordList);		logProbability = languageModel.getProbability                    (WordSequence.getWordSequence(wordList));	    }	}	return logProbability;    }    /**     * Determines if a word is a real word and not silence, garbage or     * some other sort of filler     *     * @param word the word to check     *     * @return true if the word is a real word.     */    private boolean isWord(Word word) {	return (word.getSpelling() != Dictionary.SILENCE_SPELLING);    }    /**     * May collapse a set of tokens in a token branch to a single     * high-level token. May also remove references to the     * SentenceHMMState tree     *     * @param token the token to try to collaplse     */    private final Token collapseToken(Token token) {	// TBD: Does nothing now.	return token;    }}

⌨️ 快捷键说明

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