wordpruningbreadthfirstsearchmanager.java

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

JAVA
1,685
字号
     */    private boolean isVisited(Token t) {        SearchState curState = t.getSearchState();        t = t.getPredecessor();        while (t != null && !t.isEmitting()) {            if (curState.equals(t.getSearchState())) {                System.out.println("CS " + curState + " match " + t.getSearchState());                return true;            }            t = t.getPredecessor();        }        return false;    }    protected void activeListAdd(Token token) {        activeListManager.add(token);    }    protected void activeListReplace(Token old, Token newToken) {        activeListManager.replace(old, newToken);    }    // FRAME Skew    // this is a set of experimental code used to test out a frame    // skew algorithm. This code is currently disabled.    private final static int SKEW = 0;    /**     * Apply frame skew. Determine if the given token should be expanded based     * upon frame skew     *      * @param t     *                the token to test     *      * @return <code>true</code> if the token should be expanded     */    private boolean skewPrune(Token t) {        return true; // currently disabled        // return skewPruneWord(t);    }    /**     * Frame skew based on HMM states     *      * @param t     *                the token to test     *      * @return <code>true</code> if the token should be expanded     */    private boolean skewPruneHMM(Token t) {        boolean keep = true;        SearchState ss = t.getSearchState();        if (SKEW > 0 && ss instanceof HMMSearchState) {            if (!t.isEmitting()) {                // HMMSearchState hss = (HMMSearchState) ss;                Token lastToken = (Token) skewMap.get(ss);                if (lastToken != null) {                    int lastFrame = lastToken.getFrameNumber();                    if (t.getFrameNumber() - lastFrame > SKEW                            || t.getScore() > lastToken.getScore()) {                        keep = true;                    } else {                        if (false) {                            System.out.println("Dropped " + t + " in favor of "                                    + lastToken);                        }                        keep = false;                    }                } else {                    keep = true;                }                if (keep) {                    skewMap.put(ss, t);                }            }        }        return keep;    }    /**     * Frame skew based on word states     *      * @param t     *                the token to test     *      * @return <code>true</code> if the token should be expanded     */    private boolean skewPruneWord(Token t) {        boolean keep = true;        SearchState ss = t.getSearchState();        if (SKEW > 0 && ss instanceof WordSearchState) {            Token lastToken = (Token) skewMap.get(ss);            if (lastToken != null) {                int lastFrame = lastToken.getFrameNumber();                if (t.getFrameNumber() - lastFrame > SKEW                        || t.getScore() > lastToken.getScore()) {                    keep = true;                } else {                    if (false) {                        System.out.println("Dropped " + t + " in favor of "                                + lastToken);                    }                    keep = false;                }            } else {                keep = true;            }            if (keep) {                skewMap.put(ss, t);            }        }        return keep;    }    /**     * Counts all the tokens in the active list (and displays them). This is an     * expensive operation.     */    private void showTokenCount() {        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();            }        }        System.out.println("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();            }        }        System.out.println("Result Lattice size: " + tokenSet.size());    }    /**     * Returns the Linguist.     *      * @return the Linguist     */    public Linguist getLinguist() {        return linguist;    }    /**     * Returns the Pruner.     *      * @return the Pruner     */    public Pruner getPruner() {        return pruner;    }    /**     * Returns the AcousticScorer.     *      * @return the AcousticScorer     */    public AcousticScorer getAcousticScorer() {        return scorer;    }    /**     * Returns the LogMath used.     *      * @return the LogMath used     */    public LogMath getLogMath() {        return logMath;    }    /**     * 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 ActiveList.     *      * @return the ActiveList     */    public ActiveList getActiveList() {        return activeList;    }    /**     * Sets the ActiveList.     *      * @param activeList     *                the new ActiveList     */    public void setActiveList(ActiveList activeList) {        this.activeList = activeList;    }    /**     * Returns the result list.     *      * @return the result list     */    public List getResultList() {        return resultList;    }    /**     * Sets the result list.     *      * @param resultList     *                the new result list     */    public void setResultList(List resultList) {        this.resultList = 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;    }}// ---------------------------------------------------------// Experimental code// ---------------------------------------------------------// There's some experimental code here used to track tokens// and word beams./** * A 'best token' key. This key will allow hmm states that have identical word * histories and are in the same HMM state to be treated equivalently. When * used as the best token key, only the best scoring token with a given word * history survives per HMM. */class SinglePathThroughHMMKey {    private HMMSearchState hmmSearchState;    public SinglePathThroughHMMKey(HMMSearchState hmmSearchState) {        this.hmmSearchState = hmmSearchState;    }    public int hashCode() {        return hmmSearchState.getLexState().hashCode() * 13                + hmmSearchState.getWordHistory().hashCode();    }    public boolean equals(Object o) {        if (this == o) {            return true;        } else if (o instanceof SinglePathThroughHMMKey) {            SinglePathThroughHMMKey other = (SinglePathThroughHMMKey) o;            boolean equal = hmmSearchState.getLexState().equals(                    other.hmmSearchState.getLexState())                    && hmmSearchState.getWordHistory().equals(                            other.hmmSearchState.getWordHistory());            if (equal && false) {                System.out.println("SPTHK A: " + hmmSearchState);                System.out.println("SPTHK B: " + other.hmmSearchState);            }            return equal;        }        return false;    }}/** * A quick and dirty token heap that allows us to perform token stack * experiments. It is not very efficient. We will likely replace this with * something better once we figure out how we want to prune things. */class TokenHeap {    Token[] tokens;    int curSize = 0;    /**     * Creates a token heap with the maximum size     *      * @param maxSize     *                the maximum size of the heap     */    TokenHeap(int maxSize) {        tokens = new Token[maxSize];    }    /**     * Adds a token to the heap     *      * @param token     *                the token to add     */    void add(Token token) {        // first, if an identical state exists, replace        // it.        if (!tryReplace(token)) {            if (curSize < tokens.length) {                tokens[curSize++] = token;            } else if (token.getScore() > tokens[curSize - 1].getScore()) {                tokens[curSize - 1] = token;            }        }        fixupInsert();    }    /**     * Returns the smallest scoring token on the heap     *      * @return the smallest scoring token     */    Token getSmallest() {        if (curSize == 0) {            return null;        } else {            return tokens[curSize - 1];        }    }    /**     * Determines if the heap is ful     *      * @return <code>true</code> if the heap is full     */    boolean isFull() {        return curSize == tokens.length;    }    /**     * Checks to see if there is already a token t on the heap that has the     * same search state. If so, this token replaces that one     *      * @param t     *                the token to try to add to the heap     *      * @return <code>true</code> if the token was added     */private boolean tryReplace(Token t) {        for (int i = 0; i < curSize; i++) {            if (t.getSearchState().equals(tokens[i].getSearchState())) {                assert t.getScore() > tokens[i].getScore();                tokens[i] = t;                return true;            }        }        return false;    }    /**     * Orders the heap after an insert     */    private void fixupInsert() {        Arrays.sort(tokens, 0, curSize - 1, Token.COMPARATOR);    }    /**     * returns a token on the heap that matches the given search state     *      * @param s     *                the search state     *      * @return the token that matches, or null     */    Token get(SearchState s) {        for (int i = 0; i < curSize; i++) {            if (tokens[i].getSearchState().equals(s)) {                return tokens[i];            }        }        return null;    }}/** * A class that keeps track of word histories

⌨️ 快捷键说明

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