flatlinguist.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 1,628 行 · 第 1/5 页
JAVA
1,628 行
int actualSize = Math.min(units.length, maxSize); Unit[] context = new Unit[actualSize]; for (int j = 0; j < context.length; j++) { context[j] = units[j]; } return UnitContext.get(context); } /** * Retrieves the set of trailing contexts for this node. the trailing * contexts are the set of Unit[] with a size equal to the maximum left * context size that align with the end of the node * */ Collection getEndingContexts() { Collection endingContexts = new HashSet(); if (!node.isEmpty()) { int maxSize = getLeftContextSize(); Word word = node.getWord(); Pronunciation[] prons = word.getPronunciations(null); for (int i = 0; i < prons.length; i++) { Unit[] units = prons[i].getUnits(); int actualSize = Math.min(units.length, maxSize); Unit[] context = new Unit[actualSize]; int unitIndex = units.length - actualSize; for (int j = 0; j < context.length; j++) { context[j] = units[unitIndex++]; } endingContexts.add(UnitContext.get(context)); } // add a silence to the ending context since we want to // include an optional transition to a silence unit at // the end of all words endingContexts.add(UnitContext.SILENCE); } return endingContexts; } /** * Visit all of the successor states, and gather their starting * contexts into this gstates right context */ private void pullRightContexts() { GrammarArc[] arcs = getSuccessors(); for (int i = 0; i < arcs.length; i++) { GState gstate = getGState((GrammarNode) arcs[i] .getGrammarNode()); rightContexts.addAll(gstate.getStartingContexts()); } } /** * Returns the set of succesor arcs for this grammar node. If a * successor grammar node has no words we'll substitute the successors * for that node (avoiding loops of course) * * @return an array of successors for this GState */ private GrammarArc[] getSuccessors() { return node.getSuccessors(); } /** * Visit all of the successor states, and push our ending context into * the successors left context */ void pushLeftContexts() { Collection endingContext = getEndingContexts(); Set visitedSet = new HashSet(); pushLeftContexts(visitedSet, endingContext); } /** * Pushes the given left context into the successor states. If a * successor state is empty, continue to push into this empty states * successors * * @param leftContext * the context to push */ void pushLeftContexts(Set visitedSet, Collection leftContext) { if (visitedSet.contains(getNode())) { return; } else { visitedSet.add(getNode()); } GrammarArc[] arcs = getSuccessors(); for (int i = 0; i < arcs.length; i++) { GState gstate = getGState((GrammarNode) arcs[i] .getGrammarNode()); gstate.addLeftContext(leftContext); // if our successor state is empty, also push our // ending context into the empty nodes successors if (gstate.getNode().isEmpty()) { gstate.pushLeftContexts(visitedSet, leftContext); } } } /** * Add the given left contexts to the set of left contexts for this * state * * @param context * the set of contexts to add */ private void addLeftContext(Collection context) { leftContexts.addAll(context); } /** * Adds the given context to the set of left contexts for this state * * @param context * the context to add */ private void addLeftContext(UnitContext context) { leftContexts.add(context); } /** * Returns the entry points for a given context pair */ private List getEntryPoints(ContextPair contextPair) { return (List) entryPoints.get(contextPair); } /** * Gets the context-free entry point to this state * * @return the entry point to the state */ // TODO: ideally we'll look for entry points with no left // context, but those don't exist yet so we just take // the first entry point with an SILENCE left context // note that this assumes that the first node in a grammar has a // word and that word is a SIL. Not always a valid assumption. public SentenceHMMState getEntryPoint() { ContextPair cp = ContextPair.get(UnitContext.SILENCE, UnitContext.SILENCE); List list = getEntryPoints(cp); if (list != null && list.size() > 0) { return (SentenceHMMState) list.get(0); } else { return null; } } /** * Returns the exit points for a given context pair * * @param contextPair * the context pair of interest * * @return the list of exit points */ private List getExitPoints(ContextPair contextPair) { return (List) exitPoints.get(contextPair); } /** * Add the items on the newContexts list to the dest list. Duplicate * items are not added. * * @param dest * where the contexts are added * @param newContexts * the list of new contexts */ private void addWithNoDuplicates(List dest, List newContexts) { // this could potentially be a bottleneck, but the contexts // lists should be fairly small (<100) items, so this approach // should be fast enough. for (Iterator i = newContexts.iterator(); i.hasNext();) { Unit[] context = (Unit[]) i.next(); if (!listContains(dest, context)) { dest.add(context); } } } /** * Deterimes if the give list contains the given context * * @param list * the list of contexts * * @param context * the context to check */ private boolean listContains(List list, Unit[] context) { for (Iterator i = list.iterator(); i.hasNext();) { Unit[] item = (Unit[]) i.next(); if (Unit.isContextMatch(item, context)) { return true; } } return false; } /** * Collects the right contexts for this node and pushes this nodes * ending context into the next next set of nodes. */ void collectContexts() { pullRightContexts(); pushLeftContexts(); } /** * Expands each GState into the sentence HMM States */ void expand() { // for each left context/starting context pair create a list // of starting states. for (Iterator i = leftContexts.iterator(); i.hasNext();) { UnitContext leftContext = (UnitContext) i.next(); for (Iterator j = getStartingContexts().iterator(); j.hasNext();) { UnitContext startingContext = (UnitContext) j.next(); ContextPair contextPair = ContextPair.get(leftContext, startingContext); entryPoints.put(contextPair, new ArrayList()); } } // if this is a final node don't expand it, just create a // state and add it to all entry points if (node.isFinalNode()) { GrammarState gs = new GrammarState(node); for (Iterator i = entryPoints.values().iterator(); i.hasNext();) { List epList = (List) i.next(); epList.add(gs); } } else if (!node.isEmpty()) { // its a full fledged node with a word // so expand it. Nodes without words don't need // to be expanded. for (Iterator i = leftContexts.iterator(); i.hasNext();) { UnitContext leftContext = (UnitContext) i.next(); expandWord(leftContext); } } else { //if the node is empty, populate the set of entry and exit //points with a branch state. The branch state // branches to the succesor entry points for this // state // the exit point should consist of the set of // incoming left contexts and outgoing right contexts // the 'entryPoint' table already consists of such // pairs so we can use that for (Iterator i = entryPoints.keySet().iterator(); i.hasNext();) { ContextPair cp = (ContextPair) i.next(); List epList = (List) entryPoints.get(cp); SentenceHMMState bs = new BranchState(cp.getLeftContext() .toString(), cp.getRightContext().toString(), node .getID()); epList.add(bs); addExitPoint(cp, bs); } } addEmptyEntryPoints(); } /** * Adds the set of empty entry points. The list of entry points are * tagged with a context pair. The context pair represent the left * context for the state and the starting contesxt for the state, this * allows states to be hooked up properly. However, we may be * transitioning from states that have no right hand context (CI units * such as SIL fall into this category). In this case we'd normally * have no place to transition to since we add entry points for each * starting context. To make sure that there are entry points for empty * contexts if necesary, we go through the list of entry points and * find all left contexts that have a right hand context size of zero. * These entry points will need an entry point with an empty starting * context. These entries are synthesized and added to the the list of * entry points. */ private void addEmptyEntryPoints() { Map emptyEntryPoints = new HashMap(); for (Iterator i = entryPoints.keySet().iterator(); i.hasNext();) { ContextPair cp = (ContextPair) i.next(); if (needsEmptyVersion(cp)) { ContextPair emptyContextPair = ContextPair.get(cp .getLeftContext(), UnitContext.EMPTY); List epList = (List) emptyEntryPoints.get(emptyContextPair); if (epList == null) { epList = new ArrayList(); emptyEntryPoints.put(emptyContextPair, epList); } epList.addAll((List) entryPoints.get(cp)); } } entryPoints.putAll(emptyEntryPoints); } /** * Determines if the context pair needs an empty version. A context * pair needs an empty version if the left context has a max size of * zero. * * @param cp * the contex pair to check * * @return <code>true</code> if the pair needs an empt version */ private boolean needsEmptyVersion(ContextPair cp) { UnitContext left = cp.getLeftContext(); Unit[] units = left.getUnits(); if (units.length > 0) { return (getRightContextSize(units[0]) < getRightContextSize()); } return false; } /** * Returns the grammar node of the gstate * * @return the grammar node */ private final GrammarNode getNode() { return node; } /** * Expand the the word given the left context * * @param leftContext * the left context */ private void expandWord(UnitContext leftContext) { Word word = node.getWord(); T(" Expanding word " + word + " for lc " + leftContext); Pronunciation[] pronunciations = word.getPronunciations(null); for (int i = 0; i < pronunciations.length; i++) { expandPronunciation(leftContext, pronunciations[i], i); } } /** * Expand the pronunciation given the left context * * @param leftContext * the left context * @param pronunciation * the pronunciation to expand
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?