flatlinguist.java

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

JAVA
1,628
字号
        totalStates.value = stateSet.size();    }    /**     * Allocates the acoustic model.     */    protected void allocateAcousticModel() throws IOException {        acousticModel.allocate();        if (addOutOfGrammarBranch) {            phoneLoopAcousticModel.allocate();        }    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.linguist.Linguist#deallocate()     */    public void deallocate() {        if (acousticModel != null) {            acousticModel.deallocate();        }        grammar.deallocate();    }    /**     *      * Called before a recognition     */    public void startRecognition() {        if (grammarHasChanged()) {            stateSet = compileGrammar();            totalStates.value = stateSet.size();        }    }    /**     * Called after a recognition     */    public void stopRecognition() {    }    /**     * Returns the LogMath used.     *     * @return the logMath used     */    public LogMath getLogMath() {        return logMath;    }    /**     * Returns the log silence insertion probability.     *     * @return the log silence insertion probability.     */    public float getLogSilenceInsertionProbability() {        return logSilenceInsertionProbability;    }    /**     * Compiles the grammar into a sentence hmm. A GrammarJob is created for     * the initial grammar node and added to the GrammarJob queue. While there     * are jobs left on the grammar job queue, a job is removed from the queue     * and the associated grammar node is expanded and attached to the tails.     * GrammarJobs for the successors are added to the grammar job queue.     */    protected Collection compileGrammar() {        initialGrammarState = grammar.getInitialNode();        nodeStateMap = new HashMap();        arcPool = new HashMap();        List gstateList = new ArrayList();        Timer.start("compile");        // get the nodes from the grammar and create states        // for them. Add the non-empty gstates to the gstate list.        Timer.start("  createGStates");        for (Iterator i = grammar.getGrammarNodes().iterator(); i.hasNext();) {            GState gstate = createGState((GrammarNode) i.next());            gstateList.add(gstate);        }        Timer.stop("  createGStates");        addStartingPath(); // ensures an initial path to the start state        // Prep all the gstates, by gathering all of the contexts up        // this allows each gstate to know about its surrounding        // contexts        Timer.start("  collectContexts");        for (Iterator i = gstateList.iterator(); i.hasNext();) {            GState gstate = (GState) i.next();            gstate.collectContexts();        }        Timer.stop("  collectContexts");        // now all gstates know all about their contexts, we can        // expand them fully        Timer.start("  expandStates");        for (Iterator i = gstateList.iterator(); i.hasNext();) {            GState gstate = (GState) i.next();            gstate.expand();        }        Timer.stop("  expandStates");        // now that all states are expanded fully, we can connect all        // the states up        Timer.start("  connectNodes");        for (Iterator i = gstateList.iterator(); i.hasNext();) {            GState gstate = (GState) i.next();            gstate.connect();        }        Timer.stop("  connectNodes");        SentenceHMMState initialState = findStartingState();        // add an out-of-grammar branch if configured to do so        if (addOutOfGrammarBranch) {            CIPhoneLoop phoneLoop  = new CIPhoneLoop(phoneLoopAcousticModel,                    logPhoneInsertionProbability);            SentenceHMMState firstBranchState = (SentenceHMMState)                phoneLoop.getSearchGraph().getInitialState();            initialState.connect(getArc(firstBranchState, logOne, logOne,                                        logOutOfGrammarBranchProbability));        }        searchGraph = new FlatSearchGraph(initialState);        Timer.stop("compile");        // Now that we are all done, dump out some interesting        // information about the process        if (dumpGStates) {            for (Iterator i = grammar.getGrammarNodes().iterator();                 i.hasNext();) {                GState gstate = getGState((GrammarNode) i.next());                gstate.dumpInfo();            }        }        nodeStateMap = null;        arcPool = null;        return SentenceHMMState.collectStates(initialState);    }    /**     * Returns a new GState for the given GrammarNode.     *      * @return a new GState for the given GrammarNode     */    protected GState createGState(GrammarNode grammarNode) {        return (new GState(grammarNode));    }    /**     * Ensures that there is a starting path by adding an empty left context to     * the strating gstate     */    // TODO: Currently the FlatLinguist requires that the initial    // grammar node returned by the Grammar contains a "sil" word    private void addStartingPath() {        // guarantees a starting path into the initial node by        // adding an empty left context to the starting gstate        GrammarNode node = grammar.getInitialNode();        GState gstate = getGState(node);        gstate.addLeftContext(UnitContext.SILENCE);    }    /**     * Determines if the underlying grammar has changed since we last     * compiled the search graph     *     * @return true if the grammar has changed     */    private boolean grammarHasChanged() {        return initialGrammarState == null ||                initialGrammarState != grammar.getInitialNode();    }    /**     * Finds the starting state     *      * @return the starting state     */    private SentenceHMMState findStartingState() {        GrammarNode node = grammar.getInitialNode();        GState gstate = getGState(node);        return (SentenceHMMState) gstate.getEntryPoint();    }    /**     * Gets a SentenceHMMStateArc. The arc is drawn from a pool of arcs.     *      * @param nextState     *                the next state     * @param logAcousticProbability     *                the log acoustic probability     * @param logLanguageProbability     *                the log language probability     * @param logInsertionProbability     *                the log insertion probability     *       */    private SentenceHMMStateArc getArc(SentenceHMMState nextState,            float logAcousticProbability, float logLanguageProbability,            float logInsertionProbability) {        SentenceHMMStateArc arc = new SentenceHMMStateArc(nextState,                logAcousticProbability,                logLanguageProbability * languageWeight,                logInsertionProbability);        SentenceHMMStateArc pooledArc = (SentenceHMMStateArc) arcPool.get(arc);        if (pooledArc == null) {            arcPool.put(arc, arc);            pooledArc = arc;            actualArcs.value++;        }        totalArcs.value++;        return pooledArc;    }    /**     * Given a grammar node, retrieve the grammar state     *      * @param node     *                the grammar node     *      * @return the grammar state associated with the node     *       */    private GState getGState(GrammarNode node) {        return (GState) nodeStateMap.get(node);    }    /**     * The search graph that is produced by the flat linguist.     */    class FlatSearchGraph implements SearchGraph {        /**         * An array of classes that represents the order in which the states         * will be returned.         */        private SearchState initialState;        /**         * Constructs a flast search graph with the given initial state         *          * @param initialState         *                the initial state         */        FlatSearchGraph(SearchState initialState) {            this.initialState = initialState;        }        /*         * (non-Javadoc)         *          * @see edu.cmu.sphinx.linguist.SearchGraph#getInitialState()         */        public SearchState getInitialState() {            return initialState;        }        /*         * (non-Javadoc)         *          * @see edu.cmu.sphinx.linguist.SearchGraph#getNumStateOrder()         */        public int getNumStateOrder() {            return 7;        }    }    /**     * This is a nested class that is used to manage the construction of the     * states in a grammar node. There is one GState created for each grammar     * node. The GState is used to collect the entry and exit points for the     * grammar node and for connecting up the grammar nodes to each other.     */    protected class GState {        private GrammarNode node;        private Set rightContexts = new HashSet();        private Set leftContexts = new HashSet();        private Set startingContexts;        private Map entryPoints = new HashMap();        private Map exitPoints = new HashMap();        private Map existingStates = new HashMap();        private int exitConnections = 0;        private GrammarArc[] successors = null;        /**         * Creates a GState for a grammar ndoe         *          * @param node         *                the grammar node         */        protected GState(GrammarNode node) {            this.node = node;            nodeStateMap.put(node, this);        }        /**         * Retrieves the set of starting contexts for this node. The starting         * contexts are the set of Unit[] with a size equal to the maximum         * right context size.         *          * @return the set of starting contexts acrosss nodes.         */        private Set getStartingContexts() {            if (startingContexts == null) {                startingContexts = new HashSet();                // if this is an empty node, the starting context is                // the set of starting contexts for all successor                // nodes, otherwise, it is built up from each                // pronunciation of this word                if (node.isEmpty()) {                    GrammarArc[] arcs = getSuccessors();                    for (int i = 0; i < arcs.length; i++) {                        GState gstate = getGState((GrammarNode) arcs[i]                                .getGrammarNode());                        startingContexts.addAll(gstate.getStartingContexts());                    }                } else {                    int maxSize = getRightContextSize();                    Word word = node.getWord();                    Pronunciation[] prons = word.getPronunciations(null);                    for (int i = 0; i < prons.length; i++) {                        UnitContext startingContext = getStartingContext(prons[i]);                        startingContexts.add(startingContext);                    }                }            }            return startingContexts;        }        /**         * Retrieves the starting UnitContext for the given pronunciation         *          * @param pronunciation         *                the pronunciation         *          * @return a UnitContext representing the starting context of the         *         pronunciation         */        private UnitContext getStartingContext(Pronunciation pronunciation) {            int maxSize = getRightContextSize();            Unit[] units = pronunciation.getUnits();

⌨️ 快捷键说明

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