flatlinguist.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 1,628 行 · 第 1/5 页
JAVA
1,628 行
attachState(tail, unit, logOne, logOne, logSilenceInsertionProbability); } return tail; } /** * Given a unit state, return the set of sentence hmm states associated * with the unit * * @param unitState * the unit state of intereset * * @return the hmm tree for the unit */ private HMMStateState getHMMStates(UnitState unitState) { HMMStateState hmmTree = null; HMMStateState finalState = null; Unit unit = unitState.getUnit(); HMMPosition position = unitState.getPosition(); HMM hmm = acousticModel.lookupNearestHMM(unit, position, false); HMMState initialState = hmm.getInitialState(); hmmTree = new HMMStateState(unitState, initialState); attachState(unitState, hmmTree, logOne, logOne, logOne); addStateToCache(hmmTree); finalState = expandHMMTree(unitState, hmmTree); return finalState; } /** * Expands the given hmm state tree * * @param parent * the parent of the tree * @param tree * the tree to expand * * @return the final state in the tree */ private HMMStateState expandHMMTree(UnitState parent, HMMStateState tree) { HMMStateState retState = tree; HMMStateArc[] arcs = tree.getHMMState().getSuccessors(); for (int i = 0; i < arcs.length; i++) { HMMStateState newState; if (arcs[i].getHMMState().isEmitting()) { newState = new HMMStateState(parent, arcs[i].getHMMState()); } else { newState = new NonEmittingHMMState(parent, arcs[i] .getHMMState()); } SentenceHMMState existingState = getExistingState(newState); float logProb = arcs[i].getLogProbability(); if (existingState != null) { attachState(tree, existingState, logProb, logOne, logOne); } else { attachState(tree, newState, logProb, logOne, logOne); addStateToCache(newState); retState = expandHMMTree(parent, newState); } } return retState; } /** * Connect up all of the GStates. Each state now has a table of exit * points. These exit points represent tail states for the node. Each * of these tail states is tagged with a ContextPair, that indicates * what the left context is (the exiting context) and the right context * (the entering context) for the transition. To connect up a state, * the connect does the following: 1) Iterate through all of the * grammar successors for this state 2) Get the 'entry points' for the * successor that match the exit points. 3) Hook them up. * * Note that for a task with 1000 words this will involve checking on * the order of 35,000,000 connections and making about 2,000,000 * connections * */ void connect() { GrammarArc[] arcs = getSuccessors(); // T("Connecting " + node.getWord()); for (int i = 0; i < arcs.length; i++) { GState gstate = getGState(arcs[i].getGrammarNode()); if (!gstate.getNode().isEmpty() && gstate.getNode().getWord().getSpelling().equals( Dictionary.SENTENCE_START_SPELLING)) { continue; } float probability = arcs[i].getProbability(); // adjust the language probability by the number of // pronunciations. If there are 3 ways to say the // word, then each pronunciation gets 1/3 of the total // probability. if (spreadWordProbabilitiesAcrossPronunciations && !gstate.getNode().isEmpty()) { int numPronunciations = gstate.getNode().getWord() .getPronunciations(null).length; probability -= logMath.linearToLog(numPronunciations); } float fprob = probability; for (Iterator keys = exitPoints.keySet().iterator(); keys .hasNext();) { ContextPair contextPair = (ContextPair) keys.next(); List destEntryPoints = gstate.getEntryPoints(contextPair); if (destEntryPoints != null) { List srcExitPoints = getExitPoints(contextPair); connect(srcExitPoints, destEntryPoints, fprob); } } } } /** * connect all the states in the source list to the states in the * destination list * * @param sourceList * the set of source states * @param destList * the set of destinatin states. */ private void connect(List sourceList, List destList, float logLangProb) { for (Iterator i = sourceList.iterator(); i.hasNext();) { SentenceHMMState sourceState = (SentenceHMMState) i.next(); for (Iterator j = destList.iterator(); j.hasNext();) { SentenceHMMState destState = (SentenceHMMState) j.next(); sourceState.connect(getArc(destState, logOne, logLangProb, logOne)); exitConnections++; } } } /** * Attaches one SentenceHMMState as a child to another, the transition * has the given probability * * @param prevState * the parent state * @param nextState * the child state * @param logAcousticProbability * the acoustic probability of transition in the LogMath * log domain * * @param logLanguageProbablity * the language probability of transition in the LogMath * log domain * * @param logInsertionProbablity * insertion probability of transition in the LogMath * log domain * * * @return the state that was attached */ protected void attachState(SentenceHMMState prevState, SentenceHMMState nextState, float logAcousticProbability, float logLanguageProbablity, float logInsertionProbablity) { prevState.connect(getArc(nextState, logAcousticProbability, logLanguageProbablity, logInsertionProbablity)); if (showCompilationProgress && totalStateCounter++ % 1000 == 0) { System.out.print("."); } } /** * Returns all of the states maintained by this gstate * * @return the set of all states */ public Collection getStates() { // since pstates are not placed in the cache we have to // gather those states. All other states are found in the // existingStates cache. List allStates = new ArrayList(); allStates.addAll(existingStates.values()); for (Iterator i = entryPoints.values().iterator(); i.hasNext();) { allStates.addAll((List) i.next()); } return allStates; } /** * Checks to see if a state that matches the given state already exists * * @param state * the state to check * * @return true if a state with an identical signature already exists. */ private SentenceHMMState getExistingState(SentenceHMMState state) { return (SentenceHMMState) existingStates.get(state.getSignature()); } /** * Adds the given state to the cache of states * * @param state * the state to add */ private void addStateToCache(SentenceHMMState state) { existingStates.put(state.getSignature(), state); } /** * Prints info about this GState */ void dumpInfo() { System.out.println(" ==== " + this + " ========"); System.out.print("Node: " + node); if (node.isEmpty()) { System.out.print(" (Empty)"); } else { System.out.print(" " + node.getWord()); } System.out.print(" ep: " + entryPoints.size()); System.out.print(" exit: " + exitPoints.size()); System.out.print(" cons: " + exitConnections); System.out.print(" tot: " + getStates().size()); System.out.print(" sc: " + getStartingContexts().size()); System.out.print(" rc: " + leftContexts.size()); System.out.println(" lc: " + rightContexts.size()); dumpDetails(); } /** * Dumps the details for a gstate */ void dumpDetails() { dumpCollection(" entryPoints", entryPoints.keySet()); dumpCollection(" entryPoints states", entryPoints.values()); dumpCollection(" exitPoints", exitPoints.keySet()); dumpCollection(" exitPoints states", exitPoints.values()); dumpNextNodes(); dumpExitPoints(exitPoints.values()); dumpCollection(" startingContexts", getStartingContexts()); dumpCollection(" branchingInFrom", leftContexts); dumpCollection(" branchingOutTo", rightContexts); dumpCollection(" existingStates", existingStates.keySet()); } /** * Dumps out the names of the next set of grammar nodes */ private void dumpNextNodes() { System.out.println(" Next Grammar Nodes: "); GrammarArc[] arcs = node.getSuccessors(); for (int i = 0; i < arcs.length; i++) { System.out.println(" " + arcs[i].getGrammarNode()); } } /** * Dumps the exit points and their destination states * * @param eps * the collection of exit points */ private void dumpExitPoints(Collection eps) { for (Iterator i = eps.iterator(); i.hasNext();) { List epList = (List) i.next(); for (Iterator j = epList.iterator(); j.hasNext();) { SentenceHMMState state = (SentenceHMMState) j.next(); System.out.println(" Arcs from: " + state); SearchStateArc[] arcs = state.getSuccessors(); for (int k = 0; k < arcs.length; k++) { System.out.println(" " + arcs[k].getState()); } } } } /** * Dumps the given collection * * @param name * the name of the collection * @param collection * the collection to dump */ private void dumpCollection(String name, Collection collection) { System.out.println(" " + name); for (Iterator i = collection.iterator(); i.hasNext();) { System.out.println(" " + i.next().toString()); } } /** * Dumps the given map * * @param name * the name of the map * @param map * the map to dump */ private void dumpMap(String name, Map map) { System.out.println(" " + name); for (Iterator i = map.keySet().iterator(); i.hasNext();) { Object key = i.next(); Object value = map.get(key); System.out.println(" key:" + key + " val: " + value); } } /** * Returns the string representation of the object * * @return the string representation of the object */ public String toString() { if (node.isEmpty()) { return "GState " + node + "(empty)"; } else { return "GState " + node + " word " + node.getWord(); } } } /** * Quick and dirty tracing. Traces the string if 'tracing' is true * * @param s * the string to trace. */ private void T(String s) { if (tracing) { System.out.println(s); } }}/** * A class that represents a set of units used as a context */class UnitContext { private static Map unitContextMap = new HashMap(); private Unit[] context; private int hashCode = 12; private static int foldedCount = 0; public final
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?