fstgrammar.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 498 行 · 第 1/2 页
JAVA
498 行
Dictionary.SILENCE_SPELLING); initialNode.add(silenceNode, LogMath.getLogOne()); silenceNode.add(initialNode, LogMath.getLogOne()); } } else if (token.equals("T")) { int thisID = tok.getInt("this id"); int nextID = tok.getInt("next id"); GrammarNode thisNode = get(thisID); GrammarNode nextNode = get(nextID); // if the source node is an FSTGrammarNode, we want // to join the endNode to the destination node if (hasEndNode(thisNode)) { thisNode = getEndNode(thisNode); } float lnProb = 0f; // negative natural log String output = tok.getString(); if (output == null || output.equals(",")) { // these are epsilon (meaning backoff) transitions if (output != null && output.equals(",")) { tok.getString(); // skip the word lnProb = tok.getFloat("probability"); } // if the destination node has been expanded // we actually want to add the backoff transition // to the endNode if (hasEndNode(nextNode)) { nextNode = getEndNode(nextNode); } } else { String word = tok.getString(); // skip words lnProb = tok.getFloat("probability"); if (ignoreUnknownTransitions && word.equals("<unknown>")) { continue; } /* * System.out.println(nextNode.toString() + ": " + output); */ assert hasWord(nextNode); } thisNode.add(nextNode, convertProbability(lnProb)); } else if (token.equals("F")) { int thisID = tok.getInt("this id"); float lnProb = tok.getFloat("probability"); GrammarNode thisNode = get(thisID); GrammarNode nextNode = finalNode; if (hasEndNode(thisNode)) { thisNode = getEndNode(thisNode); } thisNode.add(nextNode, convertProbability(lnProb)); } } tok.close(); assert initialNode != null; return initialNode; } /** * Reads the FST file in the given path, and creates the nodes in the FST * file. * * @param path * the path of the FST file to read * * @return the highest ID of all nodes */ private int createNodes(String path) throws IOException { ExtendedStreamTokenizer tok = new ExtendedStreamTokenizer(path, true); int maxNodeId = 0; while (!tok.isEOF()) { tok.skipwhite(); String token = tok.getString(); if (token == null) { break; } else if (token.equals("T")) { tok.getInt("src id"); // toss source node int id = tok.getInt("dest id"); // dest node numb if (id > maxNodeId) { maxNodeId = id; } String word1 = tok.getString(); // get word if (word1 == null) { continue; } String word2 = tok.getString(); // get word tok.getString(); // toss probability String nodeName = "G" + id; GrammarNode node = (GrammarNode) nodes.get(nodeName); if (node == null) { if (word2.equals(",")) { node = createGrammarNode(id, false); } else { node = createGrammarNode(id, word2.toLowerCase()); } nodes.put(nodeName, node); } else { if (!word2.equals(",")) { /* * if (!word2.toLowerCase().equals(getWord(node))) { * System.out.println(node.toString() + ": " + word2 + " " + * getWord(node)); } */ assert(word2.toLowerCase().equals(getWord(node))); } } } } tok.close(); return maxNodeId; } /** * Expand each of the word nodes into a pair of nodes, as well as adding an * optional silence node between the grammar node and its end node. * * @param maxNodeID * the node ID to start with for the new nodes * * @return the last (or maximum) node ID */ private int expandWordNodes(int maxNodeID) { Collection allNodes = nodes.values(); String[][] silence = { {Dictionary.SILENCE_SPELLING}}; for (Iterator i = allNodes.iterator(); i.hasNext();) { GrammarNode node = (GrammarNode) i.next(); // if it has at least one word, then expand the node if (node.getNumAlternatives() > 0) { GrammarNode endNode = createGrammarNode(++maxNodeID, false); node.add(endNode, LogMath.getLogOne()); // add an optional silence if (addOptionalSilence) { GrammarNode silenceNode = createGrammarNode(++maxNodeID, silence); node.add(silenceNode, LogMath.getLogOne()); silenceNode.add(endNode, LogMath.getLogOne()); } expandedNodes.add(node); } } return maxNodeID; } /** * Converts the probability from -ln to logmath * * @param lnProb * the probability to convert. Probabilities in the arpa * format in negative natural log format. We convert them to * logmath. * * @return the converted probability in logMath log base */ private float convertProbability(float lnProb) { return logMath.lnToLog(-lnProb); } /** * Given an id returns the associated grammar node * * @param id * the id of interest * * @return the grammar node or null if none could be found with the proper * id */ private GrammarNode get(int id) { String name = "G" + id; GrammarNode grammarNode = (GrammarNode) nodes.get(name); if (grammarNode == null) { grammarNode = createGrammarNode(id, false); nodes.put(name, grammarNode); } return grammarNode; } /** * Determines if the node has a word * * @param node * the grammar node of interest * * @return true if the node has a word * */ private boolean hasWord(GrammarNode node) { return (node.getNumAlternatives() > 0); } /** * Gets the word from the given grammar ndoe * * @param node * the node of interest * * @return the word (or null if the node has no word) */ private String getWord(GrammarNode node) { String word = null; if (node.getNumAlternatives() > 0) { Word[][] alternatives = node.getAlternatives(); word = alternatives[0][0].getSpelling(); } return word; } /** * Determines if the given node has an end node associated with it. * * @param node * the node of interest * * @return <code>true</code> if the given node has an end node. */ private boolean hasEndNode(GrammarNode node) { return (expandedNodes.contains(node)); } /** * Retrieves the end node associated with the given node * * @param node * the node of interest * * @return the ending node or null if no end node is available */ private GrammarNode getEndNode(GrammarNode node) { GrammarArc[] arcs = node.getSuccessors(); assert arcs != null && arcs.length > 0; return arcs[0].getGrammarNode(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?