📄 outofgrammargraph.java
字号:
/* * Copyright 1999-2002 Carnegie Mellon University. * Portions Copyright 2002 Sun Microsystems, Inc. * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. * */package edu.cmu.sphinx.linguist.dflat;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.UnitSearchState;import edu.cmu.sphinx.linguist.dictionary.Pronunciation;import edu.cmu.sphinx.util.LogMath;import edu.cmu.sphinx.linguist.dictionary.Word;import edu.cmu.sphinx.linguist.WordSequence;import edu.cmu.sphinx.linguist.WordSearchState;import edu.cmu.sphinx.linguist.HMMSearchState;import edu.cmu.sphinx.linguist.SearchStateArc;import edu.cmu.sphinx.linguist.acoustic.AcousticModel;import edu.cmu.sphinx.linguist.acoustic.HMM;import edu.cmu.sphinx.linguist.acoustic.HMMPosition;import edu.cmu.sphinx.linguist.acoustic.HMMState;import edu.cmu.sphinx.linguist.acoustic.HMMStateArc;import edu.cmu.sphinx.linguist.acoustic.Unit;/** * Builds a grammar sub-graph that matches all phones. This is suitable * for use as an out-of-grammar detector */public class OutOfGrammarGraph { private AcousticModel acousticModel; private float logOutOfGrammarBranchProbability; private float logPhoneInsertionProbability; private final static SearchStateArc[] EMPTY_ARCS = new SearchStateArc[0]; private FirstBranchState fbs; private LastBranchState lbs; private UnknownWordState uws; private SearchStateArc[] lbsArcSet; /** * Creates an OutOfGrammarGraph * * @param model the acoustic model * @param logOutOfGrammarBranchProbability probability of branching to * this graph * @param logPhoneInsertionProbability probability of inserting a phone */ public OutOfGrammarGraph(AcousticModel model, float logOutOfGrammarBranchProbability, float logPhoneInsertionProbability) { this.acousticModel = model; this.logOutOfGrammarBranchProbability = logOutOfGrammarBranchProbability; this.logPhoneInsertionProbability = logPhoneInsertionProbability; fbs = new FirstBranchState(); lbs = new LastBranchState(); uws = new UnknownWordState(); lbsArcSet = new SearchStateArc[1]; lbsArcSet[0] = lbs; } /** * Returns an arc to this out-of-grammar graph * * @return an arc to the graph */ public SearchStateArc getOutOfGrammarGraph() { return uws; } /** * Represents the unknown word */ class UnknownWordState extends OogSearchState implements WordSearchState { private SearchStateArc[] successors; /** * Creates the unknown word state */ UnknownWordState() { successors = new SearchStateArc[1]; successors[0] = fbs; } /** * Returns the pronunciation for this word * * @return the pronunciation */ public Pronunciation getPronunciation() { return Word.UNKNOWN.getPronunciations()[0]; } /** * Gets the state order for this state * * @return the state order */ public int getOrder() { return 1; } /** * Returns the signature for this state * * @return the signature */ public String getSignature() { return "oogUNK"; } /** * Gets the successor states for this search graph * * @return the successor states */ public SearchStateArc[] getSuccessors() { return successors; } /** * Gets the language probability for transitioning to this state * * @return the language probability */ public float getLanguageProbability() { return logOutOfGrammarBranchProbability; } /** * Returns true if this UnknownWordState indicates the start of a word. * Returns false if this UnknownWordState indicates the end of a word. * * @return true if this UnknownWordState indicates the start of a word, * false if this UnknownWordState indicates the end of a word */ public boolean isWordStart() { return true; } } /** * Represents the first branch state in the grammar */ class FirstBranchState extends OogSearchState { private SearchStateArc[] successors; /** * Creates the first branch state */ FirstBranchState() { List successorList = new ArrayList(); for (Iterator i = acousticModel.getContextIndependentUnitIterator(); i.hasNext();) { Unit unit = (Unit) i.next(); OogHMM hmm = new OogHMM(unit); successorList.add(hmm); } successors = (SearchStateArc[]) successorList.toArray(EMPTY_ARCS); } /** * Gets the state order for this state * * @return the state order */ public int getOrder() { return 2; } /** * Returns the signature for this state * * @return the signature */ public String getSignature() { return "oogFBS"; } /** * Gets the successor states for this search graph * * @return the successor states */ public SearchStateArc[] getSuccessors() { return successors; } } /** * Represents an HMM Unit in the search graph */ class OogHMM extends OogSearchState implements UnitSearchState { private HMM hmm; private SearchStateArc[] successors; /** * Creates an HMM unit state * * @param unit the unit represented by this state */ OogHMM(Unit unit) { hmm = acousticModel.lookupNearestHMM(unit, HMMPosition.UNDEFINED, false); successors = new SearchStateArc[1]; successors[0] = new OogHMMState(hmm.getInitialState(), LogMath.getLogOne()); } /** * Gets the unit * * @return the unit */ public Unit getUnit() { return hmm.getBaseUnit(); } /** * Gets the state order for this state * * @return the state order */ public int getOrder() { return 3; } /** * Returns the signature for this state * * @return the signature */ public String getSignature() { return "oogHMM-" + getUnit(); } /** * Gets the successor states for this search graph * * @return the successor states */ public SearchStateArc[] getSuccessors() { return successors; } /** * Gets the insertion probability of entering this state * * @return the log probability */ public float getInsertionProbability() { return logPhoneInsertionProbability; } } /** * Represents a single hmm state in the search graph */ class OogHMMState extends OogSearchState implements HMMSearchState { HMMState hmmState; float logProbability; /** * Creates an OogHMMState * * @param hmmState the hmm state associated with this search state * @param logProbability the probability of transitioning to this * state */ OogHMMState(HMMState hmmState, float logProbability) { this.hmmState = hmmState; this.logProbability = logProbability; } /** * Returns the signature for this state * * @return the signature */ public String getSignature() { return "oog-" + hmmState; } /** * Returns the hmm state * * @return the hmm state */ public HMMState getHMMState() { return hmmState; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -