simplebreadthfirstsearchmanager.java

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

JAVA
631
字号
        if (logger.isLoggable(Level.FINE)) {              int hmms = activeList.size();            totalHmms += hmms;            logger.fine("Frame: " + currentFrameNumber + " Hmms: "                    + hmms + "  total " + totalHmms);        }    }    /**     * Calculate the acoustic scores for the active list. The active list     * should contain only emitting tokens.     *      * @return <code>true</code> if there are more frames to score,     *         otherwise, false     *       */    protected boolean scoreTokens() {        boolean moreTokens;        Token bestToken = null;        scoreTimer.start();        bestToken = (Token) scorer.calculateScores(activeList.getTokens());        scoreTimer.stop();        moreTokens = (bestToken != null);        activeList.setBestToken(bestToken);        curTokensScored.value += activeList.size();        totalTokensScored.value += activeList.size();        tokensPerSecond.value = totalTokensScored.value / getTotalTime();        if (logger.isLoggable(Level.FINE)) {            logger.fine(currentFrameNumber + " " + activeList.size()                    + " " + curTokensScored.value + " "                    + (int) tokensPerSecond.value);        }        return moreTokens;    }    /**     * Returns the total time since we start4ed     *      * @return the total time (in seconds)     */    private double getTotalTime() {        return (System.currentTimeMillis() - startTime) / 1000.0;    }    /**     * Removes unpromising branches from the active list     *       */    protected void pruneBranches() {        int startSize = activeList.size();        pruneTimer.start();        activeList = pruner.prune(activeList);        beamPruned.value += startSize - activeList.size();        pruneTimer.stop();    }    /**     * Gets the best token for this state     *      * @param state     *                the state of interest     *      * @return the best token     */    protected Token getBestToken(SearchState state) {        Token best = (Token) bestTokenMap.get(state);        if (logger.isLoggable(Level.FINER) && best != null) {            logger.finer("BT " + best + " for state " + state);        }        return best;    }    /**     * Sets the best token for a given state     *      * @param token     *                the best token     *      * @param state     *                the state     *      * @return the previous best token for the given state, or null if no     *         previous best token     */    protected Token setBestToken(Token token, SearchState state) {        return (Token) bestTokenMap.put(state, token);    }    /**     * 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     */    protected void collectSuccessorTokens(Token token) {        SearchState state = token.getSearchState();        // If this is a final state, add it to the final list        if (token.isFinal()) {            resultList.add(token);        }        if (token.getScore() < threshold) {            return;        }        if (state instanceof WordSearchState                && token.getScore() < wordThreshold) {            return;        }        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 SearchState and frame then create a new token, add        // it to the lattice and the SearchState.        // 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();            // We're actually multiplying the variables, but since            // these come in log(), multiply gets converted to add            float logEntryScore = token.getScore() + arc.getProbability();            if (wantEntryPruning) {                if (logEntryScore < threshold) {                    continue;                }                if (nextState instanceof WordSearchState                        && logEntryScore < wordThreshold) {                    continue;                }            }            Token bestToken = getBestToken(nextState);            boolean firstToken = bestToken == null;            if (firstToken || bestToken.getScore() <= logEntryScore) {                Token newToken = token.child(nextState, logEntryScore, arc                        .getLanguageProbability(), arc                        .getInsertionProbability(), currentFrameNumber);                tokensCreated.value++;                setBestToken(newToken, nextState);                if (!newToken.isEmitting()) {                    // if not emitting, check to see if we've already visited                    // this state during this frame. Expand the token only if we                    // haven't visited it already. This prevents the search                    // from getting stuck in a loop of states with no                    // intervening emitting nodes. This can happen with nasty                    // jsgf grammars such as ((foo*)*)*                    if (!isVisited(newToken)) {                        collectSuccessorTokens(newToken);                    }                } else {                    if (firstToken) {                        activeList.add(newToken);                    } else {                        activeList.replace(bestToken, newToken);                        viterbiPruned.value++;                    }                }            } else {                viterbiPruned.value++;            }        }    }    /**     * Determines whether or not we've visited the state associated with this     * token since the previous frame.     *     * @param t the token to check     * @return true if we've visted the search state since the last frame     */    private boolean isVisited(Token t) {        SearchState curState = t.getSearchState();        t = t.getPredecessor();        while (t != null && !t.isEmitting()) {            if (curState.equals(t.getSearchState())) {                return true;            }            t = t.getPredecessor();        }        return false;    }    /**     * Counts all the tokens in the active list (and displays them). This is an     * expensive operation.     */    private void showTokenCount() {        if (logger.isLoggable(Level.INFO)) {            Set tokenSet = new HashSet();            for (Iterator i = activeList.iterator(); i.hasNext();) {                Token token = (Token) i.next();                while (token != null) {                    tokenSet.add(token);                    token = token.getPredecessor();                }            }            logger.info("Token Lattice size: " + tokenSet.size());            tokenSet = new HashSet();            for (Iterator i = resultList.iterator(); i.hasNext();) {                Token token = (Token) i.next();                while (token != null) {                    tokenSet.add(token);                    token = token.getPredecessor();                }            }            logger.info("Result Lattice size: " + tokenSet.size());        }    }    /**     * Returns the best token map.     *      * @return the best token map     */    protected Map getBestTokenMap() {        return bestTokenMap;    }    /**     * Sets the best token Map.     *      * @param bestTokenMap     *                the new best token Map     */    protected void setBestTokenMap(Map bestTokenMap) {        this.bestTokenMap = bestTokenMap;    }    /**     * Returns the result list.     *      * @return the result list     */    public List getResultList() {        return resultList;    }    /**     * Returns the current frame number.     *      * @return the current frame number     */    public int getCurrentFrameNumber() {        return currentFrameNumber;    }    /**     * Returns the Timer for growing.     *      * @return the Timer for growing     */    public Timer getGrowTimer() {        return growTimer;    }    /**     * Returns the tokensCreated StatisticsVariable.     *      * @return the tokensCreated StatisticsVariable.     */    public StatisticsVariable getTokensCreated() {        return tokensCreated;    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.decoder.search.SearchManager#allocate()     */    public void allocate() throws IOException {        totalTokensScored = StatisticsVariable                .getStatisticsVariable("totalTokensScored");        tokensPerSecond = StatisticsVariable                .getStatisticsVariable("tokensScoredPerSecond");        curTokensScored = StatisticsVariable                .getStatisticsVariable("curTokensScored");        tokensCreated = StatisticsVariable                .getStatisticsVariable("tokensCreated");        viterbiPruned = StatisticsVariable                .getStatisticsVariable("viterbiPruned");        beamPruned = StatisticsVariable.getStatisticsVariable("beamPruned");        linguist.allocate();        pruner.allocate();        scorer.allocate();        scoreTimer = Timer.getTimer("scoring");         pruneTimer = Timer.getTimer("pruning");        growTimer = Timer.getTimer("growing");    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.decoder.search.SearchManager#deallocate()     */    public void deallocate() {        scorer.deallocate();        pruner.deallocate();        linguist.deallocate();    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.util.props.Configurable#getName()     */    public String getName() {        return name;    }}

⌨️ 快捷键说明

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