📄 lextreelinguist.java
字号:
} /* * (non-Javadoc) * * @see edu.cmu.sphinx.linguist.Linguist#deallocate() */ public void deallocate() { acousticModel = null; logMath = null; dictionary = null; } /* * (non-Javadoc) * * @see edu.cmu.sphinx.linguist.Linguist#getSearchGraph() */ public SearchGraph getSearchGraph() { return searchGraph; } /** * * Called before a recognition */ public void startRecognition() { languageModel.start(); } /** * Called after a recognition */ public void stopRecognition() { languageModel.stop(); } /** * Retrieves the language model for this linguist * * @return the language model (or null if there is none) */ public LanguageModel getLanguageModel() { return languageModel; } /** * retrieves the initial language state * * @return the initial language state */ private SearchState getInitialSearchState() { InitialWordNode node = hmmTree.getInitialNode(); return new LexTreeWordState(node, node.getParent(), WordSequence .getWordSequence(sentenceStartWordArray).trim( languageModel.getMaxDepth() - 1), 0f, logOne, logOne); } /** * Compiles the n-gram into a lex tree that is used during the search */ protected void compileGrammar() { Timer.start("compile"); sentenceEndWord = dictionary.getSentenceEndWord(); sentenceStartWordArray = new Word[1]; sentenceStartWordArray[0] = dictionary.getSentenceStartWord(); hmmPool = new HMMPool(acousticModel, logger, unitManager); hmmTree = new HMMTree(hmmPool, dictionary, languageModel, addFillerWords, languageWeight); hmmPool.dumpInfo(); Timer.stop("compile"); // Now that we are all done, dump out some interesting // information about the process searchGraph = new LexTreeSearchGraph(getInitialSearchState()); } class LexTreeSearchGraph implements SearchGraph { /** * An array of classes that represents the order in which the states * will be returned. */ private SearchState initialState; /** * Constructs a search graph with the given initial state * * @param initialState * the initial state */ LexTreeSearchGraph(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#getSearchStateOrder() */ public int getNumStateOrder() { return 6; } } /** * The LexTreeLinguist returns lanague states to the search manager. This * class forms the base implementation for all language states returned. * This LexTreeState keeps track of the probability of entering this state (a * language+insertion probability) as well as the unit history. The unit * history consists of the LexTree nodes that correspond to the left, * center and right contexts. * * This is an abstract class, subclasses must implement the getSuccessorss * method. */ abstract class LexTreeState implements SearchState, SearchStateArc { private Node node; private WordSequence wordSequence; float currentSmearTerm; float currentSmearProb; /** * Creates a LexTreeState. * * @param node * the node associated with this state * * @param wordSequence * the history of words up until this point * */ LexTreeState(Node node, WordSequence wordSequence, float smearTerm, float smearProb) { this.node = node; this.wordSequence = wordSequence; currentSmearTerm = smearTerm; currentSmearProb = smearProb; } /** * Gets the unique signature for this state. The signature building * code is slow and should only be used for non-time-critical tasks * such as plotting states. * * @return the signature */ public String getSignature() { return "lts-" + node.hashCode() + "-ws-" + wordSequence; } public float getSmearTerm() { return currentSmearTerm; } public float getSmearProb() { return currentSmearProb; } /** * Generate a hashcode for an object * * @return the hashcode */ public int hashCode() { int hashCode = fullWordHistories ? wordSequence.hashCode() * 37 : 37; hashCode += node.hashCode(); return hashCode; } /** * Determines if the given object is equal to this object * * @param o * the object to test * @return <code>true</code> if the object is equal to this */ public boolean equals(Object o) { if (o == this) { return true; } else if (o instanceof LexTreeState) { LexTreeState other = (LexTreeState) o; boolean wordSequenceMatch = fullWordHistories ? wordSequence .equals(other.wordSequence) : true; return node == other.node && wordSequenceMatch; } else { return false; } } /** * Gets a successor to this search state * * @return the sucessor state */ public SearchState getState() { return this; } /** * Gets the composite probability of entering this state * * @return the log probability */ public float getProbability() { return getLanguageProbability() + getAcousticProbability() + getInsertionProbability(); } /** * Gets the language probability of entering this state * * @return the log probability */ public float getLanguageProbability() { return logOne; } /** * Gets the language probability of entering this state * * @return the log probability */ public float getAcousticProbability() { return logOne; } /** * Gets the insertion probability of entering this state * * @return the log probability */ public float getInsertionProbability() { return logOne; } /** * Determines if this is an emitting state * * @return <code>true</code> if this is an emitting state. */ public boolean isEmitting() { return false; } /** * Determines if this is a final state * * @return <code>true</code> if this is an final state. */ public boolean isFinal() { return false; } /** * Gets the hmm tree node representing the unit * * @return the unit lex node */ protected Node getNode() { return node; } /** * Returns the word sequence for this state * * @return the word sequence */ public WordSequence getWordHistory() { return wordSequence; } public Object getLexState() { return node; } /** * Returns the list of successors to this state * * @return a list of SearchState objects */ public SearchStateArc[] getSuccessors() { SearchStateArc[] arcs = getCachedArcs(); if (arcs == null) { arcs = getSuccessors(node); putCachedArcs(arcs); } return arcs; } /** * Returns the list of successors to this state * * @return a list of SearchState objects */ protected SearchStateArc[] getSuccessors(Node theNode) { Collection nodes = theNode.getSuccessors(); SearchStateArc[] arcs = new SearchStateArc[nodes.size()]; Iterator iter = nodes.iterator(); // System.out.println("Arc: "+ this); for (int i = 0; i < arcs.length; i++) { Node nextNode = (Node) iter.next(); // System.out.println(" " + nextNode); if (nextNode instanceof WordNode) { arcs[i] = createWordStateArc((WordNode) nextNode, (HMMNode) getNode(), this); } else if (nextNode instanceof EndNode) { arcs[i] = createEndUnitArc((EndNode) nextNode, this); } else { arcs[i] = createUnitStateArc((HMMNode) nextNode, this); } } return arcs; } /** * Creates a word search state for the given word node * * @param wordNode * the wordNode * * @return the search state for the wordNode */ protected SearchStateArc createWordStateArc(WordNode wordNode, HMMNode lastUnit, LexTreeState previous) { // System.out.println("CWSA " + wordNode + " fup " + fixupProb); float probability = logOne; float arcProbability = logOne; Word nextWord = wordNode.getWord(); WordSequence nextWordSequence = wordSequence; float smearTerm = previous.getSmearTerm(); if (!nextWord.isFiller() || nextWord == sentenceEndWord) { nextWordSequence = wordSequence.addWord(nextWord, languageModel .getMaxDepth()); probability = languageModel.getProbability(nextWordSequence); smearTerm = getSmearTermFromLanguageModel(nextWordSequence); // System.out.println("LP " + nextWordSequence + " " + // logProbability); probability *= languageWeight; // subtract off the previously applied smear probability arcProbability = probability - previous.getSmearProb(); } if (nextWord == sentenceEndWord) { // System.out.println("LP " + nextWordSequence + " " + // logProbability); return new LexTreeEndWordState(wordNode, lastUnit, nextWordSequence.trim(languageModel.getMaxDepth() - 1), smearTerm, logOne, arcProbability); } else { return new LexTreeWordState(wordNode, lastUnit, nextWordSequence.trim(languageModel.getMaxDepth() - 1), smearTerm, logOne, arcProbability); } } /** * Creates a unit search state for the given unit node * * @param hmmNode * the unit node * * @return the search state */ SearchStateArc createUnitStateArc(HMMNode hmmNode, LexTreeState previous) { SearchStateArc arc; // System.out.println("CUSA " + hmmNode); float insertionProbability = calculateInsertionProbability(hmmNode); float probability = getUnigramSmear(hmmNode) + previous.getSmearTerm(); float arcProbability = probability - previous.getSmearProb(); // if we want a unit state create it, otherwise // get the first hmm state of the unit if (generateUnitStates) { arc = new LexTreeUnitState(hmmNode, getWordHistory(), previous .getSmearTerm(), probability, arcProbability, insertionProbability); } else { HMM hmm = hmmNode.getHMM(); arc = new LexTreeHMMState(hmmNode, getWordHistory(), previous .getSmearTerm(), probability, hmm.getInitialState(), arcProbability, insertionProbability, logOne, null); } return arc; } /** * Creates a unit search state for the given unit node
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -