flatlinguist.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 1,628 行 · 第 1/5 页
JAVA
1,628 行
* @param which * unique ID for this pronunciation */ // Each GState maintains a list of entry points. This list of // entry points is used when connecting up the end states of // one GState to the beginning states in another GState. The // entry points are tagged by a ContextPair which represents // the left context upon entering the state (the left context // of the initial units of the state), and the right context // of the previous states (corresponding to the starting // contexts for this state). // // When expanding a proununciation, the following steps are // taken: // 1) Get the starting context for the pronunciation. // This is the set of units that correspond to the start // of the pronunciation. // // 2) Create a new PronunciationState for the // pronunciation. // // 3) Add the PronunciationState to the entry point table // (a hash table keyed by the ContextPair(LeftContext, // StartingContext). // // 4) Generate the set of context dependent units, using // the left and right context of the GState as necessary. // Note that there will be fan out at the end of the // pronunciation to allow for units with all of the // various right contexts. The point where the fan-out // occurs is the (length of the pronunciation - the max // right context size). // // 5) Attach each cd unit to the tree // // 6) Expand each cd unit into the set of HMM states // // 7) Attach the optional and looping back silence cd // unit // // 8) Collect the leaf states of the tree and add them to // the exitStates list. private void expandPronunciation(UnitContext leftContext, Pronunciation pronunciation, int which) { UnitContext startingContext = getStartingContext(pronunciation); // Add the pronunciation state to the entry point list // (based upon its left and right context) String pname = "P(" + pronunciation.getWord() + "[" + leftContext + "," + startingContext + "])-G" + getNode().getID(); PronunciationState ps = new PronunciationState(pname, pronunciation, which); T(" Expanding " + ps.getPronunciation() + " for lc " + leftContext); ContextPair cp = ContextPair.get(leftContext, startingContext); List epList = (List) entryPoints.get(cp); if (epList == null) { throw new Error("No EP list for context pair " + cp); } else { epList.add(ps); } Unit[] units = pronunciation.getUnits(); int fanOutPoint = units.length - getRightContextSize(); if (fanOutPoint < 0) { fanOutPoint = 0; } SentenceHMMState tail = ps; for (int i = 0; tail != null && i < fanOutPoint; i++) { tail = attachUnit(ps, tail, units, i, leftContext, UnitContext.EMPTY); } SentenceHMMState branchTail = tail; for (Iterator iter = rightContexts.iterator(); iter.hasNext();) { UnitContext finalRightContext = (UnitContext) iter.next(); tail = branchTail; for (int i = fanOutPoint; tail != null && i < units.length; i++) { tail = attachUnit(ps, tail, units, i, leftContext, finalRightContext); } } } /** * Attaches the given unit to the given tail, expanding the unit if * necessary. If an identical unit is already attached, then this path * is folded into the existing path. * * @param parent * the parent state * @param tail * the place to attach the unit to * @param units * the set of units * @param which * the index into the set of units * @param leftContext * the left context for the unit * @param rightContext * the right context for the unit * * @return the tail of the added unit (or null if the path was folded * onto an already expanded path. */ private SentenceHMMState attachUnit(PronunciationState parent, SentenceHMMState tail, Unit[] units, int which, UnitContext leftContext, UnitContext rightContext) { Unit[] lc = getLC(leftContext, units, which); Unit[] rc = getRC(units, which, rightContext); UnitContext actualRightContext = UnitContext.get(rc); LeftRightContext context = LeftRightContext.get(lc, rc); Unit cdUnit = unitManager.getUnit(units[which].getName(), units[which] .isFiller(), context); UnitState unitState = new ExtendedUnitState(parent, which, cdUnit); float logInsertionProbability; if (unitState.getUnit().isFiller()) { logInsertionProbability = logSilenceInsertionProbability; } else if (unitState.getWhich() == 0) { logInsertionProbability = logWordInsertionProbability; } else { logInsertionProbability = logUnitInsertionProbability; } // check to see if this state already exists, if so // branch to it and we are done, otherwise, branch to // the new state and expand it. SentenceHMMState existingState = getExistingState(unitState); if (existingState != null) { attachState(tail, existingState, logOne, logOne, logInsertionProbability); // T(" Folding " + existingState); return null; } else { attachState(tail, unitState, logOne, logOne, logInsertionProbability); addStateToCache(unitState); // T(" Attaching " + unitState); tail = expandUnit(unitState); // if we are attaching the last state of a word, then // we add it to the exitPoints table. the exit points // table is indexed by a ContextPair, consisting of // the exiting left context and the right context. if (unitState.isLast()) { UnitContext nextLeftContext = generateNextLeftContext( leftContext, units[which]); ContextPair cp = ContextPair.get(nextLeftContext, actualRightContext); // T(" Adding to exitPoints " + cp); addExitPoint(cp, tail); // if we have encountered a last unit with a right // context of silence, then we add a silence unit // to this unit. the silence unit has a self // loopback. if (actualRightContext == UnitContext.SILENCE) { SentenceHMMState silTail; UnitState silUnit = new ExtendedUnitState(parent, which + 1, UnitManager.SILENCE); SentenceHMMState silExistingState = getExistingState(silUnit); if (silExistingState != null) { attachState(tail, silExistingState, logOne, logOne, logSilenceInsertionProbability); } else { attachState(tail, silUnit, logOne, logOne, logSilenceInsertionProbability); addStateToCache(silUnit); silTail = expandUnit(silUnit); ContextPair silCP = ContextPair.get( UnitContext.SILENCE, UnitContext.EMPTY); addExitPoint(silCP, silTail); } } } return tail; } } /** * Adds an exit point to this gstate * * @param cp * the context tag for the state * @param state * the state associated with the tag */ private void addExitPoint(ContextPair cp, SentenceHMMState state) { List list = (List) exitPoints.get(cp); if (list == null) { list = new ArrayList(); exitPoints.put(cp, list); } list.add(state); } /** * Get the left context for a unit based upon the left context size, * the entry left context and the current unit. * * @param left * the entry left context * @param units * the set of units * @param index * the index of the current unit */ private Unit[] getLC(UnitContext left, Unit[] units, int index) { Unit[] leftUnits = left.getUnits(); int maxSize = getLeftContextSize(units[index]); int curSize = index + leftUnits.length; int actSize = Math.min(curSize, maxSize); Unit[] lc = new Unit[actSize]; for (int i = 0; i < lc.length; i++) { int lcIndex = (index - lc.length) + i; if (lcIndex < 0) { lc[i] = leftUnits[leftUnits.length + lcIndex]; } else { lc[i] = units[lcIndex]; } } return lc; } /** * Get the right context for a unit based upon the right context size, * the exit right context and the current unit. * * @param units * the set of units * @param index * the index of the current unit * @param right * the exiting right context */ private Unit[] getRC(Unit[] units, int index, UnitContext right) { Unit[] rightUnits = right.getUnits(); int maxSize = getRightContextSize(units[index]); int curSize = (units.length - (index + 1)) + rightUnits.length; int actSize = Math.min(curSize, maxSize); Unit[] rc = new Unit[actSize]; for (int i = 0; i < rc.length; i++) { int rcIndex = index + i + 1; if (rcIndex >= units.length) { rc[i] = rightUnits[rcIndex - units.length]; } else { rc[i] = units[rcIndex]; } } return rc; } /** * Gets the maximum context size for the given unit * * @param unit * the unit of interest * * @return the maximum left context size for the unit */ private int getLeftContextSize(Unit unit) { if (true && unit.isFiller()) { return 0; } else { return getLeftContextSize(); } } /** * Gets the maximum context size for the given unit * * @param unit * the unit of interest * * @return the maximum right context size for the unit */ private int getRightContextSize(Unit unit) { if (true && unit.isFiller()) { return 0; } else { return getRightContextSize(); } } /** * Returns the size of the left context. * * @return the size of the left context */ protected int getLeftContextSize() { return acousticModel.getLeftContextSize(); } /** * Returns the size of the right context. * * @return the size of the right context */ protected int getRightContextSize() { return acousticModel.getRightContextSize(); } /** * Generates the next left context based upon a previous context and a * unit * * @param prevLeftContext * the previous left context * @param unit * the current unit */ UnitContext generateNextLeftContext(UnitContext prevLeftContext, Unit unit) { Unit[] prevUnits = prevLeftContext.getUnits(); int maxSize = getLeftContextSize(); int curSize = prevUnits.length; int actSize = Math.min(maxSize, curSize); Unit[] leftUnits = new Unit[actSize]; for (int i = 0; i < leftUnits.length - 1; i++) { leftUnits[i] = prevUnits[i + 1]; } if (leftUnits.length > 0) { leftUnits[leftUnits.length - 1] = unit; } return UnitContext.get(leftUnits); } /** * Expands the unit into a set of HMMStates. If the unit is a silence * unit add an optional loopback to the tail. * * @param unit * the unit to expand * * @return the head of the hmm tree */ protected SentenceHMMState expandUnit(UnitState unit) { SentenceHMMState tail = getHMMStates(unit); // if the unit is a silence unit add a loop back from the // tail silence unit if (unit.getUnit().isSilence()) { // add the loopback, but don't expand it // anymore
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?