hmmtree.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 1,331 行 · 第 1/3 页
JAVA
1,331 行
void addSuccessor(WordNode wordNode) { putSuccessor(wordNode, wordNode); } /** * Adds an EndNode to the set of successors for this node * If a node similar to * the child has already been added, we use the previously * added node, otherwise we add this. * * @param child the endNode to add * @param probability probability for this transition * * @return the node that holds the endNode (new or old) */ EndNode addSuccessor(EndNode child, float probability) { Unit baseUnit = child.getBaseUnit(); EndNode matchingChild = (EndNode) getSuccessor(baseUnit); if (matchingChild == null) { putSuccessor(baseUnit, child); } else { if (matchingChild.getUnigramProbability() < probability) { matchingChild.setUnigramProbability(probability); } child = matchingChild; } return child; } /** * Gets a word node associated with the pronunciation. * * @param p the pronunciation * * @return the word node */ private WordNode getWordNode(Pronunciation p, float probability) { WordNode node = (WordNode) wordNodeMap.get(p); if (node == null) { node = new WordNode(p, probability); wordNodeMap.put(p, node); } return node; } /** * Adds a child node to the successor. If a node similar to * the child has already been added, we use the previously * added node, otherwise we add this. Also, we record the base * unit of the child in the set of right context * * @param child the child to add * @return the node (may be different than child if there was * already a node attached holding the hmm held by child) * */ UnitNode addSuccessor(UnitNode child) { UnitNode matchingChild = (UnitNode) getSuccessor(child.getKey()); if (matchingChild == null) { putSuccessor(child.getKey(), child); } else { child = matchingChild; } return child; } /** * Returns the successors for this node * * @return the set of successor nodes */ Collection getSuccessors() { if (successors instanceof List) { return (List) successors; } else { return getSuccessorMap().values(); } } /** * Returns the string representation fo this object * * @return the string representation of the object */ public String toString() { return "Node "; }}/** * A node representing a word in the hmm tree * */class WordNode extends Node { private Pronunciation pronunciation; /** * Creates a word node * * @param pronunciation the pronunciation to wrap in this node * @param probability the word unigram probability */ WordNode(Pronunciation pronunciation, float probability) { super(probability); this.pronunciation = pronunciation; } /** * Gets the word associated with this node * * @return the word */ Word getWord() { return pronunciation.getWord(); } /** * Gets the pronunciation associated with this node * * @return the pronunciation */ Pronunciation getPronunciation() { return pronunciation; } /** * Gets the last unit for this word * * @return the last unit */ Unit getLastUnit() { Unit[] units = pronunciation.getUnits(); return units[units.length - 1]; } /** * Returns the successors for this node * * @return the set of successor nodes */ Collection getSuccessors() { throw new Error("Not supported"); } /** * Returns a string representation for this object * * @return a string representation */ public String toString() { return "WordNode " + pronunciation +" p " + getUnigramProbability(); }}/** * A class that represents the initial word in the search space. * It is treated specially because we need to keep track of the * context as well. The context is embodied in the parent node */class InitialWordNode extends WordNode { HMMNode parent; /** * Creates an InitialWordNode * * @param pronunciation the pronunciation * @param parent the parent node */ InitialWordNode(Pronunciation pronunciation, HMMNode parent) { super(pronunciation, LogMath.getLogOne()); this.parent = parent; } /** * Gets the parent for this word node * * @return the parent */ HMMNode getParent() { return parent; }}abstract class UnitNode extends Node { public final static int SIMPLE_UNIT = 1; public final static int WORD_BEGINNING_UNIT = 2; public final static int SILENCE_UNIT = 3; public final static int FILLER_UNIT = 4; private int type; /** * Creates the UnitNode * * @param probablilty the probablilty for the node */ UnitNode(float probablilty) { super(probablilty); } /** * Returns the base unit for this hmm node * * @return the base unit */ abstract Unit getBaseUnit(); abstract Object getKey(); abstract HMMPosition getPosition(); /** * Gets the unit type (one of SIMPLE_UNIT, WORD_BEGINNING_UNIT, * SIMPLE_UNIT or FILLER_UNIT * * @return the unit type */ int getType() { return type; } /** * Sets the unit type * * @param type the unit type */ void setType(int type) { this.type = type; }}/** * A node that represents an HMM in the hmm tree */class HMMNode extends UnitNode { private HMM hmm; // There can potentially be a large number of nodes (millions), // therefore it is important to conserve space as much as // possible. While building the HMMNOdes, we keep right contexts // in a set to allow easy pruning of duplicates. Once the tree is // entirely built, we no longer need to manage the right contexts // as a set, a simple array will do. The freeze method converts // the set to the array of units. This rcSet object holds the set // during contruction and the array after the freeze. private Object rcSet; /** * Creates the node, wrapping the given hmm * * @param hmm the hmm to hold */ HMMNode(HMM hmm, float probablilty) { super(probablilty); this.hmm = hmm; Unit base = getBaseUnit(); int type = SIMPLE_UNIT; if (base.isSilence()) { type = SILENCE_UNIT; } else if (base.isFiller()) { type = FILLER_UNIT; } else if (hmm.getPosition().isWordBeginning()) { type = WORD_BEGINNING_UNIT; } setType(type); } /** * Returns the base unit for this hmm node * * @return the base unit */ Unit getBaseUnit() { // return hmm.getUnit().getBaseUnit(); return hmm.getBaseUnit(); } /** * Returns the hmm for this node * * @return the hmm */ HMM getHMM() { return hmm; } HMMPosition getPosition() { return hmm.getPosition(); } Object getKey() { return getHMM(); } /** * Returns a string representation for this object * * @return a string representation */ public String toString() { return "HMMNode " + hmm + " p " + getUnigramProbability(); } /** * Adds a right context to the set of possible right contexts for * this node. This is typically only needed for hmms at the ends * of words. * * @param rc the right context. */ void addRC(Unit rc) { getRCSet().add(rc); } /** * Freeze this node. Convert the set into an array to reduce * memory overhead */ void freeze() { super.freeze(); if (rcSet instanceof HashSet) { Set set = (Set) rcSet; rcSet = set.toArray(new Unit[set.size()]); } } /** * Gets the rc as a set. If we've already been frozen it is an * error * * @return the set of right contexts */ private Set getRCSet() { if (rcSet == null) { rcSet = new HashSet(); } assert rcSet instanceof HashSet; return (Set) rcSet; } /** * returns the set of right contexts for this node * * @return the set of right contexts */ Unit[] getRC() { if (rcSet instanceof HashSet) { freeze(); } return (Unit[]) rcSet; }}class EndNode extends UnitNode { Unit baseUnit; Unit leftContext; Object key; /** * Creates the node, wrapping the given hmm * * @param baseUnit the base unit for this node * @param lc the left context * @param probablilty the probability for the transition to this * node */ EndNode(Unit baseUnit, Unit lc, float probablilty) { super(probablilty); this.baseUnit = baseUnit; this.leftContext = lc; key = new Integer(baseUnit.getBaseID() * 121 + leftContext.getBaseID()); } /** * Returns the base unit for this hmm node * * @return the base unit */ Unit getBaseUnit() { return baseUnit; } /** * Returns the base unit for this hmm node * * @return the base unit */ Unit getLeftContext() { return leftContext; } Object getKey() { return key; } HMMPosition getPosition() { return HMMPosition.END; } /** * Returns a string representation for this object * * @return a string representation */ public String toString() { return "EndNode base:" + baseUnit + " lc " + leftContext + " " + key; } /** * Freeze this node. Convert the set into an array to reduce * memory overhead */ void freeze() { super.freeze(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?