📄 dynamicflatlinguist.java
字号:
* * @return the order */ public int getOrder() { return 1; } /** * Given a set of arcs and the ID of the desired next unit, return the * set of arcs containing only those that transition to the next unit * * @param arcs the set of arcs to filter * @param nextBase the ID of the desired next unit */ GrammarArc[] filter(GrammarArc[] arcs, int nextBase) { if (nextBase != ANY) { List list = new ArrayList(); for (int i = 0; i < arcs.length; i++) { GrammarNode node = arcs[i].getGrammarNode(); if (hasEntryContext(node, nextBase)) { list.add(arcs[i]); } } arcs = (GrammarArc[]) list.toArray(new GrammarArc[list.size()]); } return arcs; } /** * Determines if the given node starts with the specified unit * * @param node the grammar node * @param unitID the id of the unit */ private boolean hasEntryContext(GrammarNode node, int unitID) { Set unitSet = (Set) nodeToUnitSetMap.get(node); return unitSet.contains(hmmPool.getUnit(unitID)); } /** * Retain only the pronunciations that start with the unit indicated * by nextBase * * @param p the set of pronunciations to filter * @param nextBase the ID of the desired initial unit */ Pronunciation[] filter(Pronunciation[] p, int nextBase) { if (true) { return p; } if (nextBase == ANY) { return p; } else { int count = 0; for (int i = 0; i < p.length; i++) { Unit[] units = p[i].getUnits(); if (units[0].getBaseID() == nextBase) { count++; } } if (count == p.length) { return p; } else { Pronunciation[] filteredP = new Pronunciation[count]; int index = 0; for (int i = 0; i < p.length; i++) { Unit[] units = p[i].getUnits(); if (units[0].getBaseID() == nextBase) { filteredP[index++] = p[i]; } } return filteredP; } } } /** * Gets the ID of the left context unit for this path * * @return the left context ID */ int getLC() { return lc; } /** * Gets the ID of the desired next unit * * @return the ID of the next unit */ int getNextBaseID() { return nextBaseID; } /** * Returns the set of IDs for all possible next units for this grammar * node * * @return the set of IDs of all possible next units */ int[] getNextUnits() { return (int[]) nodeToNextUnitArrayMap.get(node); } /** * Returns a string representation of this object * * @return a string representation */ public String toString() { return node + "[" + hmmPool.getUnit(lc) + "," + hmmPool.getUnit(nextBaseID) + "]"; } /** * Returns the grammar node associated with this grammar state * * @return the grammar node */ GrammarNode getGrammarNode() { return node; } } class InitialState extends FlatSearchState { private List nextArcs = new ArrayList(); /** * Gets the set of successors for this state * * @return the set of successors */ public SearchStateArc[] getSuccessors() { return (SearchStateArc[]) nextArcs .toArray(new SearchStateArc[nextArcs .size()]); } public void addArc(SearchStateArc arc) { nextArcs.add(arc); } /** * 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 "initialState"; } /** * Returns the order of this state type among all of the search states * * @return the order */ public int getOrder() { return 1; } /** * Returns a string representation of this object * * @return a string representation */ public String toString() { return getSignature(); } } /** * This class representations a word punctuation in the search graph */ class PronunciationState extends FlatSearchState implements WordSearchState{ private GrammarState gs; private Pronunciation pronunciation; /** * Creates a PronunciationState * * @param gs the associated grammar state * @param p the pronunciation */ PronunciationState(GrammarState gs, Pronunciation p) { this.gs = gs; this.pronunciation = p; } /** * Gets the insertion probability of entering this state * * @return the log probability */ public float getInsertionProbability() { if (pronunciation.getWord().isFiller()) { return logOne; } else { return logWordInsertionProbability; } } /** * Generate a hashcode for an object * * @return the hashcode */ public int hashCode() { return 13 * gs.hashCode() + pronunciation.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 PronunciationState) { PronunciationState other = (PronunciationState) o; return other.gs.equals(gs) && other.pronunciation.equals(pronunciation); } else { return false; } } /** * Gets the successor states for this search graph * * @return the successor states */ public SearchStateArc[] getSuccessors() { SearchStateArc[] arcs = getCachedSuccessors(); if (arcs == null) { arcs = getSuccessors(gs.getLC(), 0); cacheSuccessors(arcs); } return arcs; } /** * Gets the successor states for the unit and the given position and * left context * * @param lc the ID of the left context * @param index the position of the unit within the pronunciation * * @return the set of sucessor arcs */ SearchStateArc[] getSuccessors(int lc, int index) { SearchStateArc[] arcs = null; if (index == pronunciation.getUnits().length -1) { if (isContextIndependentUnit( pronunciation.getUnits()[index])) { arcs = new SearchStateArc[1]; arcs[0] = new FullHMMSearchState(this, index, lc, ANY); } else { int[] nextUnits = gs.getNextUnits(); arcs = new SearchStateArc[nextUnits.length]; for (int i = 0; i < arcs.length; i++) { arcs[i] = new FullHMMSearchState(this,index,lc,nextUnits[i]); } } } else { arcs = new SearchStateArc[1]; arcs[0] = new FullHMMSearchState(this, index, lc); } return arcs; } /** * Gets the pronunciation assocated with this state * * @return the pronunciation */ public Pronunciation getPronunciation() { return pronunciation; } /** * Determines if the given unit is a CI unit * * @param unit the unit to test * * @return true if the unit is a context independent unit */ private boolean isContextIndependentUnit(Unit unit) { return unit.isFiller(); } /** * 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 "PS " + gs.getSignature() + "-" + pronunciation; } /** * Returns a string representation of this object * * @return a string representation */ public String toString() { return pronunciation.getWord().getSpelling(); } /** * Returns the order of this state type among all of the search states * * @return the order */ public int getOrder() { return 2; } /** * Returns the grammar state associated with this state * * @return the grammar state */ GrammarState getGrammarState() { return gs; } /** * Returns true if this WordSearchState indicates the start of a word. * Returns false if this WordSearchState indicates the end of a word. * * @return true if this WordSearchState indicates the start of a word, * false if this WordSearchState indicates the end of a word */ public boolean isWordStart() { return true; } } /** * Represents a unit (as an HMM) in the search graph */ class FullHMMSearchState extends FlatSearchState implements UnitSearchState { private PronunciationState pState; private int index; private int lc; private int rc; private HMM hmm; private boolean isLastUnitOfWord; /** * Creates a FullHMMSearchState * * @param p the parent PronunciationState * @param which the index of the unit within the pronunciation * @param lc the ID of the left context */ FullHMMSearchState(PronunciationState p, int which, int lc) { this(p, which, lc, p.getPronunciation().getUnits()[which + 1].getBaseID()); } /** * Creates a FullHMMSearchState * * @param p the parent PronunciationState * @param which the index of the unit within the pronunciation * @param lc the ID of the left context * @param rc the ID of the right context */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -