📄 dynamicflatlinguist.java
字号:
for (int i = 0; i < arcs.length; i++) { GrammarNode nextNode = arcs[i].getGrammarNode(); collectNextUnits(nextNode, vistedNodes, unitSet); } int[] nextUnits = new int[unitSet.size()]; int index = 0; for (Iterator i = unitSet.iterator(); i.hasNext(); ) { Unit unit = (Unit) i.next(); nextUnits[index++] = unit.getBaseID(); } nodeToNextUnitArrayMap.put(node, nextUnits); } // collect the set of entry units for this node if (nodeToUnitSetMap.get(node) == null) { Set vistedNodes = new HashSet(); Set unitSet = new HashSet(); collectNextUnits(node, vistedNodes, unitSet); nodeToUnitSetMap.put(node, unitSet); } } /** * For the given grammar node, collect the set of possible next units. * * @param thisNode the grammar node * @param vistedNodes the set of visited grammar nodes, used to ensure * that we don't attempt to expand a particular grammar node more than * once (which could lead to a death spiral) * @param unitSet the entry units are collected here. */ private void collectNextUnits(GrammarNode thisNode, Set vistedNodes, Set unitSet) { if (vistedNodes.contains(thisNode)) { return; } vistedNodes.add(thisNode); if (thisNode.isFinalNode()) { unitSet.add(UnitManager.SILENCE); } else if (!thisNode.isEmpty()) { Word word = thisNode.getWord(); Pronunciation[] pronunciations = word.getPronunciations(); for (int j = 0; j < pronunciations.length; j++) { unitSet.add(pronunciations[j].getUnits()[0]); } } else { GrammarArc[] arcs = thisNode.getSuccessors(); for (int i = 0; i < arcs.length; i++) { GrammarNode nextNode = arcs[i].getGrammarNode(); collectNextUnits(nextNode, vistedNodes, unitSet); } } } Map successorCache = new HashMap(); /** * The base search state for this dynamic flat linguist. */ abstract class FlatSearchState implements SearchState , SearchStateArc { final static int ANY = 0; /** * Gets the set of successors for this state * * @return the set of successors */ public abstract SearchStateArc[] getSuccessors(); /** * Returns a unique string representation of the state. This string is * suitable (and typically used) for a label for a GDL node * * @return the signature */ public abstract String getSignature(); /** * Returns the order of this state type among all of the search states * * @return the order */ public abstract int getOrder(); /** * Determines if this state is an emitting state * * @return true if this is an emitting state */ public boolean isEmitting() { return false; } /** * Determines if this is a final state * * @return true if this is a final state */ public boolean isFinal() { return false; } /** * Returns a lex state associated with the searc state (not applicable * to this linguist) * * @return the lex state (null for this linguist) */ public Object getLexState() { return null; } /** * Returns a well formatted string representation of this state * * @return the formatted string */ public String toPrettyString() { return toString(); } /** * Returns a string representation of this object * * @return a string representation */ public String toString() { return getSignature(); } /** * Returns the word history for this state (not applicable to this * linguist) * @return the word history (null for this linguist) */ public WordSequence getWordHistory() { return null; } /** * Gets a successor to this search state * * @return the successor 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 acoustic 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; } /** * Simple debugging output * * @param msg the debug message */ void debug(String msg) { if (false) { System.out.println(msg); } } /** * Get the arcs from the cache if the exist * * @return the cached arcs or null */ SearchStateArc[] getCachedSuccessors() { return (SearchStateArc[]) successorCache.get(this); } /** * Places the set of successor arcs in the cache * * @param successors the set of arcs to be cached for this state */ void cacheSuccessors(SearchStateArc[] successors) { successorCache.put(this, successors); } } /** * Represents a grammar node in the search graph. A grammar state needs to * keep track of the associated grammar node as well as the left context * and next base unit. */ class GrammarState extends FlatSearchState { private GrammarNode node; private int lc; private int nextBaseID; private float languageProbability; /** * Creates a grammar state for the given node with a silence Lc * * @param node the grammar node */ GrammarState(GrammarNode node) { this(node, logOne, UnitManager.SILENCE.getBaseID()); } /** * Creates a grammar state for the given node and left context. The * path will connect to any possible next base * * @param node the grammar node * @param languageProbability the probability of transistioning to * this word * @param lc the left context for this path */ GrammarState(GrammarNode node, float languageProbability, int lc) { this(node, languageProbability, lc, ANY); } /** * Creates a grammar state for the given node and left context and * next base ID. * * @param node the grammar node * @param languageProbability the probability of transistioning to * this word * @param lc the left context for this path * @param nextBaseID the next base ID */ GrammarState(GrammarNode node, float languageProbability, int lc, int nextBaseID) { this.lc = lc; this.nextBaseID = nextBaseID; this.node = node; this.languageProbability = languageProbability; } /** * Gets the language probability of entering this state * * @return the log probability */ public float getLanguageProbability() { return languageProbability * languageWeight; } /** * Generate a hashcode for an object. Equality for a grammar state * includes the grammar node, the lc and the next base ID * * @return the hashcode */ public int hashCode() { return node.hashCode() * 17 + lc * 7 + nextBaseID; } /** * 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 GrammarState) { GrammarState other = (GrammarState) o; return other.node == node && lc == other.lc && nextBaseID == other.nextBaseID; } else { return false; } } /** * Determines if this is a final state in the search graph * * @return true if this is a final state in the search graph */ public boolean isFinal() { return node.isFinalNode(); } /** * Gets the set of successors for this state * * @return the set of successors */ public SearchStateArc[] getSuccessors() { SearchStateArc[] arcs = getCachedSuccessors(); if (arcs == null) { if (isFinal()) { arcs = EMPTY_ARCS; } else if (node.isEmpty()) { arcs = getNextGrammarStates(lc, nextBaseID); } else { Word word = node.getWord(); Pronunciation[] pronunciations = word.getPronunciations(); pronunciations = filter(pronunciations, nextBaseID); SearchStateArc[] nextArcs = new SearchStateArc[pronunciations.length]; for (int i = 0; i < pronunciations.length; i++) { nextArcs[i] = new PronunciationState(this, pronunciations[i]); } arcs = nextArcs; } cacheSuccessors(arcs); } return arcs; } /** * Gets the set of arcs to the next set of grammar states that match * the given nextBaseID * * @param lc the current left context * @param nextBaseID the desired next base ID */ SearchStateArc[] getNextGrammarStates(int lc, int nextBaseID) { GrammarArc[] nextNodes = node.getSuccessors(); nextNodes = filter(nextNodes, nextBaseID); SearchStateArc[] nextArcs = new SearchStateArc[nextNodes.length]; for (int i = 0; i < nextNodes.length; i++) { GrammarArc arc = nextNodes[i]; nextArcs[i] = new GrammarState(arc.getGrammarNode(), arc.getProbability(), lc, nextBaseID); } return nextArcs; } /** * Returns a unique string representation of the state. This string is * suitable (and typically used) for a label for a GDL node * * @return the signature */ public String getSignature() { return "GS " + node + "-lc-" + hmmPool.getUnit(lc) + "-rc-" + hmmPool.getUnit(nextBaseID); } /** * Returns the order of this state type among all of the search states
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -