wordpruningbreadthfirstsearchmanager.java

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

JAVA
1,685
字号
 */class WordTracker {    Map statMap;    int frameNumber;    int stateCount;    int maxWordHistories;    /**     * Creates a word tracker for the given frame number     *      * @param frameNumber     *                the frame number     */    WordTracker(int frameNumber) {        statMap = new HashMap();        this.frameNumber = frameNumber;    }    /**     * Adds a word history for the given token to the word tracker     *      * @param t     *                the token to add     */    void add(Token t) {        stateCount++;        WordSequence ws = getWordSequence(t);        WordStats stats = (WordStats) statMap.get(ws);        if (stats == null) {            stats = new WordStats(ws);            statMap.put(ws, stats);        }        stats.update(t);    }    /**     * Dumps the word histories in the tracker     */    void dump() {        dumpSummary();        Object[] stats = statMap.values().toArray();        Arrays.sort(stats, WordStats.COMPARATOR);        for (int i = 0; i < stats.length; i++) {            System.out.println("   " + stats[i]);        }    }    /**     * Dumps summary information in the tracker     */    void dumpSummary() {        System.out.println("Frame: " + frameNumber + " states: " + stateCount                + " histories " + statMap.size());    }    /**     * Given a token, gets the word sequence represented by the token     *      * @param token     *                the token of interest     *      * @return the word sequence for the token     */    private WordSequence getWordSequence(Token token) {        List wordList = new LinkedList();        while (token != null) {            if (token.isWord()) {                WordSearchState wordState = (WordSearchState) token                        .getSearchState();                Word word = wordState.getPronunciation().getWord();                wordList.add(0, word);            }            token = token.getPredecessor();        }        return WordSequence.getWordSequence(wordList);    }}/** * Keeps track of statistics for a particular word sequence */class WordStats {    public final static Comparator COMPARATOR = new Comparator() {        public int compare(Object o1, Object o2) {            WordStats ws1 = (WordStats) o1;            WordStats ws2 = (WordStats) o2;            if (ws1.maxScore > ws2.maxScore) {                return -1;            } else if (ws1.maxScore == ws2.maxScore) {                return 0;            } else {                return 1;            }        }    };    private int size;    private float maxScore;    private float minScore;    private WordSequence ws;    /**     * Creates a word stat for the given sequence     *      * @param ws     *                the word sequence     */    WordStats(WordSequence ws) {        size = 0;        maxScore = -Float.MAX_VALUE;        minScore = Float.MAX_VALUE;        this.ws = ws;    }    /**     * Updates the stats based upon the scores for the given token     *      * @param t     *                the token     */    void update(Token t) {        size++;        if (t.getScore() > maxScore) {            maxScore = t.getScore();        }        if (t.getScore() < minScore) {            minScore = t.getScore();        }    }    /**     * Returns a string representation of the stats     *      * @return a string representation     */    public String toString() {        return "states:" + size + " max:" + maxScore + " min:" + minScore + " "                + ws;    }}/** * A tool for tracking the types tokens created and placed in the beam * * TODO: Develop a mechanism  for adding trackers such as these in a * more general fashion. */class TokenTypeTracker {    // keep track of the various types of states    private int numWords;    private int numUnits;    private int numHMMs;    private int numOthers;    private int numHMMBegin;    private int numHMMEnd;    private int numHMMSingle;    private int numHMMInternal;    private int numTokens;    /**     * Adds a token to this tracker. Records statistics about     * the type of token.     *     * @param t the token to track     */    void add(Token t) {        numTokens++;        SearchState s = t.getSearchState();        if (s instanceof WordSearchState) {            numWords++;        } else if (s instanceof UnitSearchState) {            numUnits++;        } else if (s instanceof HMMSearchState) {            numHMMs++;            HMM hmm = ((HMMSearchState)s).getHMMState().getHMM();            if (hmm.getPosition() == HMMPosition.BEGIN) {                numHMMBegin++;            } else if (hmm.getPosition() == HMMPosition.END) {                numHMMEnd++;            } else if (hmm.getPosition() == HMMPosition.SINGLE) {                numHMMSingle++;            } else if (hmm.getPosition() == HMMPosition.INTERNAL) {                numHMMInternal++;            }        } else {            numOthers++;        }    }    /**     * Shows the accumulated statistics     */    void show() {        System.out.println("TotalTokens: " + numTokens);        System.out.println("      Words: " + numWords + pc(numWords));        System.out.println("      Units: " + numUnits + pc(numUnits));        System.out.println("      HMM-b: " + numHMMBegin + pc(numHMMBegin));        System.out.println("      HMM-e: " + numHMMEnd + pc(numHMMEnd));        System.out.println("      HMM-s: " + numHMMSingle + pc(numHMMSingle));        System.out.println("      HMM-i: " + numHMMInternal +                pc(numHMMInternal));        System.out.println("     Others: " + numOthers + pc(numOthers));    }    /**     * Utility method for generating iteger percents     * @param num the value to be converted into percent     * @return a string representation as a percent     */    private String pc(int num) {         int percent = ((100 * num) / numTokens);         return " (" + percent + "%)";    }}/** * This debugging class is used to track the number of active tokens per state */class TokenTracker {    private Map stateMap;    private boolean enabled;    private int frame = 0;    private int utteranceStateCount;    private int utteranceMaxStates;    private int utteranceSumStates;    /**     * Enables or disables the token tracker     *      * @param enabled     *                if <code>true</code> the tracker is enabled     */    void setEnabled(boolean enabled) {        this.enabled = enabled;    }    /**     * Starts the per-utterance tracking     */    void startUtterance() {        if (enabled) {            frame = 0;            utteranceStateCount = 0;            utteranceMaxStates = -Integer.MAX_VALUE;            utteranceSumStates = 0;        }    }    /**     * stops the per-utterance tracking     */    void stopUtterance() {        if (enabled) {            dumpSummary();        }    }    /**     * Starts the per-frame tracking     */    void startFrame() {        if (enabled) {            stateMap = new HashMap();        }    }    /**     * Adds a new token to the tracker     *      * @param t     *                the token to add.     */    void add(Token t) {        if (enabled) {            TokenStats stats = getStats(t);            stats.update(t);        }    }    /**     * Stops the per-frame tracking     */    void stopFrame() {        if (enabled) {            frame++;            dumpDetails();        }    }    /**     * Dumps summary info about the tokens     */    void dumpSummary() {        if (enabled) {            float avgStates = 0f;            if (utteranceStateCount > 0) {                avgStates = ((float) utteranceSumStates) / utteranceStateCount;            }            System.out.print("# Utterance stats ");            System.out.print(" States: " + utteranceStateCount / frame);            if (utteranceStateCount > 0) {                System.out.print(" Paths: " + utteranceSumStates / frame);                System.out.print(" Max: " + utteranceMaxStates);                System.out.print(" Avg: " + avgStates);            }            System.out.println();        }    }    /**     * Dumps detailed info about the tokens     */    void dumpDetails() {        if (enabled) {            int maxStates = -Integer.MAX_VALUE;            int hmmCount = 0;            int sumStates = 0;            for (Iterator i = stateMap.values().iterator(); i.hasNext();) {                TokenStats stats = (TokenStats) i.next();                if (stats.isHMM) {                    hmmCount++;                }                sumStates += stats.count;                utteranceSumStates += stats.count;                if (stats.count > maxStates) {                    maxStates = stats.count;                }                if (stats.count > utteranceMaxStates) {                    utteranceMaxStates = stats.count;                }            }            utteranceStateCount += stateMap.size();            float avgStates = 0f;            if (stateMap.size() > 0) {                avgStates = ((float) sumStates) / stateMap.size();            }            System.out.print("# Frame " + frame);            System.out.print(" States: " + stateMap.size());            if (stateMap.size() > 0) {                System.out.print(" Paths: " + sumStates);                System.out.print(" Max: " + maxStates);                System.out.print(" Avg: " + avgStates);                System.out.print(" HMM: " + hmmCount);            }            System.out.println();        }    }    /**     * Gets the stats for a particular token     *      * @param t     *                the token of interest     *      * @return the token stats associated with the given token     */    private TokenStats getStats(Token t) {        TokenStats stats = (TokenStats) stateMap.get(t.getSearchState()                .getLexState());        if (stats == null) {            stats = new TokenStats();            stateMap.put(t.getSearchState().getLexState(), stats);        }        return stats;    }}/** * A class for keeing track of statistics about tokens. Tracks the count, min * and max score for a particular state. */class TokenStats {    int count;    float maxScore;    float minScore;    boolean isHMM;    TokenStats() {        count = 0;        maxScore = -Float.MAX_VALUE;        minScore = Float.MIN_VALUE;    }    /**     * Update this state with the given token     */    public void update(Token t) {        count++;        if (t.getScore() > maxScore) {            maxScore = t.getScore();        }        if (t.getScore() < minScore) {            minScore = t.getScore();        }        isHMM = t.getSearchState() instanceof HMMSearchState;    }}

⌨️ 快捷键说明

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