grammar.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 612 行 · 第 1/2 页
JAVA
612 行
* successor nodes * * @param node * the node * @return a random successor node. */ private GrammarNode selectRandomSuccessor(GrammarNode node) { GrammarArc[] arcs = node.getSuccessors(); int index = randomizer.nextInt(arcs.length); return arcs[index].getGrammarNode(); } /** * Dumps the grammar */ public void dumpGrammar(String name) { getInitialNode().dumpGDL(name); } /** * returns the number of nodes in this grammar * * @return the number of nodes */ public int getNumNodes() { return grammarNodes.size(); } /** * returns the set of of nodes in this grammar * * @return the set of nodes */ public Set getGrammarNodes() { return grammarNodes; } /** * Prepare to create a new grammar */ protected void newGrammar() { maxIdentity = 0; grammarNodes = new HashSet(); initialNode = null; postProcessed = false; } /** * Creates a grammar. Subclasses of grammar should implement this method. * * @return the initial node for the grammar * * @throws java.io.IOException * if the grammar could not be loaded * @throws java.lang.NoSuchMethodException * if called with inappropriate subclass. */ protected abstract GrammarNode createGrammar() throws IOException; /** * Create class from reference text (not implemented). * * @param bogusText * dummy variable * * @throws NoSuchMethogException * if called with reference sentence */ protected GrammarNode createGrammar(String bogusText) throws NoSuchMethodException { throw new NoSuchMethodException("Does not create " + "grammar with reference text"); } /** * Gets the dictionary for this grammar * * @return the dictionary */ protected Dictionary getDictionary() { return dictionary; } /** * Returns a new GrammarNode with the given set of alternatives. * * @param identity * the id for this node * @param alts * the set of alternative word lists for this GrammarNode */ protected GrammarNode createGrammarNode(int identity, String[][] alts) { GrammarNode node; Word[][] alternatives = new Word[alts.length][]; for (int i = 0; i < alternatives.length; i++) { alternatives[i] = new Word[alts[i].length]; for (int j = 0; j < alts[i].length; j++) { Word word = getDictionary().getWord(alts[i][j]); // Pronunciation[] pronunciation = // word.getPronunciations(null); if (word == null) { alternatives = EMPTY_ALTERNATIVE; break; } else { alternatives[i][j] = word; } } } node = new GrammarNode(identity, alternatives); add(node); return node; } /** * Returns a new GrammarNode with the given single word. If the word is not * in the dictionary, an empty node is created. The grammar id is automatically * assigned * * @param word * the word for this grammar node */ protected GrammarNode createGrammarNode(String word) { GrammarNode node = createGrammarNode(maxIdentity + 1, word); return node; } /** * Creates an empty grammar node in this grammar. The gramar ID is * automatically assigned. * * @param isFinal * if true, this is a final node * * @return the grammar node */ protected GrammarNode createGrammarNode(boolean isFinal) { return createGrammarNode(maxIdentity + 1, isFinal); } /** * Returns a new GrammarNode with the given single word. If the word is not * in the dictionary, an empty node is created * * @param identity * the id for this node * @param word * the word for this grammar node */ protected GrammarNode createGrammarNode(int identity, String word) { GrammarNode node = null; Word[][] alternatives = EMPTY_ALTERNATIVE; Word wordObject = getDictionary().getWord(word); // Pronunciation[] pronunciation = wordObject.getPronunciations(null); if (wordObject != null) { alternatives = new Word[1][]; alternatives[0] = new Word[1]; alternatives[0][0] = wordObject; node = new GrammarNode(identity, alternatives); add(node); } else { node = createGrammarNode(identity, false); logger.warning("Can't find pronunciation for " + word); } return node; } /** * Creates a grammar node in this grammar with the given identity * * @param identity * the identity of the node * @param isFinal * if true, this is a final node * * @return the grammar node */ protected GrammarNode createGrammarNode(int identity, boolean isFinal) { GrammarNode node; node = new GrammarNode(identity, isFinal); add(node); return node; } /** * Adds the given grammar node to the set of nodes for this grammar * * @param node * the grammar node */ private void add(GrammarNode node) { if (node.getID() > maxIdentity) { maxIdentity = node.getID(); } // check to see if there is already a node with the given ID. if (idCheck) { for (Iterator i = grammarNodes.iterator(); i.hasNext();) { GrammarNode gn = (GrammarNode) i.next(); if (gn.getID() == node.getID()) { throw new Error("DUP ID " + gn + " and " + node); } } } grammarNodes.add(node); } /** * Eliminate unnecessary nodes from the grammar. This method goes through * the grammar and looks for branches to nodes that have no words and have * only a single exit and bypasses these nodes. * */ private void optimizeGrammar() { Set nodes = getGrammarNodes(); for (Iterator i = nodes.iterator(); i.hasNext();) { GrammarNode g = (GrammarNode) i.next(); g.optimize(); } } /** * Adds an optional silence word after every non-filler word in the * grammar */ private void addSilenceWords() { Set nodes = new HashSet(getGrammarNodes()); for (Iterator i = nodes.iterator(); i.hasNext();) { GrammarNode g = (GrammarNode) i.next(); if (!g.isEmpty() && !g.getWord().isFiller()) { GrammarNode silNode = createGrammarNode(maxIdentity + 1, dictionary.getSilenceWord().getSpelling()); GrammarNode branchNode = g.splitNode(maxIdentity + 1); add(branchNode); g.add(silNode, 0.00f); silNode.add(branchNode, 0.0f); silNode.add(silNode, 0.0f); } } } /** * Adds an optional filler word loop after every * non-filler word in the * grammar */ private void addFillerWords() { Set nodes = new HashSet(getGrammarNodes()); Word[] fillers = getInterWordFillers(); if (fillers.length == 0) { return; } for (Iterator i = nodes.iterator(); i.hasNext();) { GrammarNode wordNode = (GrammarNode) i.next(); if (!wordNode.isEmpty() && !wordNode.getWord().isFiller()) { GrammarNode wordExitNode = wordNode.splitNode(maxIdentity + 1); add(wordExitNode); GrammarNode fillerStart = createGrammarNode(false); GrammarNode fillerEnd = createGrammarNode(false); fillerEnd.add(fillerStart, 0.0f); fillerEnd.add(wordExitNode, 0.0f); wordNode.add(fillerStart, 0.0f); for (int j = 0; j < fillers.length; j++) { GrammarNode fnode = createGrammarNode(maxIdentity + 1, fillers[j].getSpelling()); fillerStart.add(fnode, 0.0f); fnode.add(fillerEnd, 0.0f); } } } } /** * Gets the set of fillers after filtering out fillers that don't go * between words. * * @return the set of inter-word fillers */ private Word[] getInterWordFillers() { List fillerList = new ArrayList(); Word[] fillers = dictionary.getFillerWords(); for (int i = 0; i < fillers.length; i++) { if (fillers[i] == dictionary.getSentenceStartWord()) { continue; } else if (fillers[i] == dictionary.getSentenceEndWord()) { continue; } else { fillerList.add(fillers[i]); } } return (Word[]) fillerList.toArray(new Word[fillerList.size()]); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?