fstgrammar.java

来自「It is the Speech recognition software. 」· Java 代码 · 共 498 行 · 第 1/2 页

JAVA
498
字号
/* * Copyright 1999-2002 Carnegie Mellon University.   * Portions Copyright 2002 Sun Microsystems, Inc.   * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * All Rights Reserved.  Use is subject to license terms. *  * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL  * WARRANTIES. * */package edu.cmu.sphinx.linguist.language.grammar;import java.io.IOException;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import edu.cmu.sphinx.linguist.dictionary.Dictionary;import edu.cmu.sphinx.linguist.dictionary.Word;import edu.cmu.sphinx.util.ExtendedStreamTokenizer;import edu.cmu.sphinx.util.LogMath;import edu.cmu.sphinx.util.props.PropertyException;import edu.cmu.sphinx.util.props.PropertySheet;import edu.cmu.sphinx.util.props.PropertyType;import edu.cmu.sphinx.util.props.Registry;/** * Loads a grammar from a file representing a finite-state transducer (FST) in * the 'ARPA' grammar format. The ARPA FST format is like so (the explanation * of the format is below): <br> *  * <pre> *  I 2 *  F 0 2.30259 *  T 0 1 &lt;unknown&gt; &lt;unknown&gt; 2.30259 *  T 0 4 wood wood 1.60951 *  T 0 5 cindy cindy 1.60951 *  T 0 6 pittsburgh pittsburgh 1.60951 *  T 0 7 jean jean 1.60951 *  F 1 2.89031 *  T 1 0 , , 0.587725 *  T 1 4 wood wood 0.58785 *  F 2 3.00808 *  T 2 0 , , 0.705491 *  T 2 1 &lt;unknown&gt; &lt;unknown&gt; 0.58785 *  F 3 2.30259 *  T 3 0 *  F 4 2.89031 *  T 4 0 , , 0.587725 *  T 4 6 pittsburgh pittsburgh 0.58785 *  F 5 2.89031 *  T 5 0 , , 0.587725 *  T 5 7 jean jean 0.58785 *  F 6 2.89031 *  T 6 0 , , 0.587725 *  T 6 5 cindy cindy 0.58785 *  F 7 1.28093 *  T 7 0 , , 0.454282 *  T 7 4 wood wood 1.28093   *   </pre> *  * <b>Key: </b> *  * <pre> *  I - initial node, so &quot;I 2&quot; means node 2 is the initial node *  F - final node, e.g., &quot;F 0 2.30259&quot; means that node 0 is a final node, *  and the probability of finishing at node 0 is 2.30259 (in -ln) *  T - transition, &quot;T 0 4 wood wood 1.60951&quot; means &quot;transitioning from *  node 0 to node 4, the output is wood and the machine is now *  in the node wood, and the probability associated with the *  transition is 1.60951 (in -ln)&quot;. &quot;T 6 0 , , 0.587725&quot; is *  a backoff transition, and the output is null (epsilon in *  the picture), and the machine is now in the null node. *   </pre> *  * <p> * Probabilities read in from the FST file are in negative natural log format * and are converted to the internal logMath log base. * <p> * As the FST file is read in, a Grammar object that is structurally equivalent * to the FST is created. The steps of converting the FST file to a Grammar * object are: * <ol> *  * <li><b>Create all the Grammar nodes </b> <br> * Go through the entire FST file and for each word transition, take the * destination node ID and create a grammar node using that ID. These nodes are * kept in a hashtable to make sure they are created once for each ID. * Therefore, we get one word per grammar node.</li> *  * <br> *  * <li><b>Create an end node for each Grammar node </b> <br> * This is end node is used for backoff transitions into the Grammar node, so * that it will not go through the word itself, but instead go directly to the * end of the word. Moreover, we also add an <b>optional </b> silence node * between the grammar node and its end node. The result of this step on each * grammar node (show in Figure 1 below as the circle with "word") is as * follows. The end node is the empty circle at the far right: <br> * <img src="doc-files/fst-end-node.jpg"> <br> * <b>Figure 1: Addition of end node and the <i>optional </i> silence. </b> * </li> *  * <br> *  * <li><b>Create the transitions </b> <br> * Read through the entire FST file, and for each line indicating a transition, * connect up the corresponding Grammar nodes. Backoff transitions and null * transitions (i.e., the ones that do not output a word) will be linked to the * end node of a grammar node.</li> *  * </ol> */public class FSTGrammar extends Grammar {    /**     * The SphinxProperty for the location of the FST n-gram file.     */    public final static String PROP_PATH = "path";    /**     * The default value for PROP_PATH.     */    public final static String PROP_PATH_DEFAULT = "default.arpa_gram";            /**     * Sphinx property that defines the logMath component.      */        public final static String PROP_LOG_MATH = "logMath";        // TODO: If this property turns out to be worthwhile, turn this    // into a full fledged sphinx property    private boolean addInitialSilenceNode = false;        // TODO: If this property turns out to be worthwhile, turn this    // into a full fledged sphinx property        // ------------------------------    // Configuration data    // -------------------------------        private boolean addOptionalSilence = false;    private boolean ignoreUnknownTransitions = true;    private String path;    private LogMath logMath;    private Map nodes = new HashMap();    private Set expandedNodes = new HashSet();    /**     * 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");    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.util.props.Configurable#register(java.lang.String,     *      edu.cmu.sphinx.util.props.Registry)     */    public void register(String name, Registry registry)            throws PropertyException {        super.register(name, registry);        registry.register(PROP_PATH, PropertyType.STRING);        registry.register(PROP_LOG_MATH, PropertyType.COMPONENT);    }    /*     * (non-Javadoc)     *      * @see edu.cmu.sphinx.util.props.Configurable#newProperties(edu.cmu.sphinx.util.props.PropertySheet)     */    public void newProperties(PropertySheet ps) throws PropertyException {        super.newProperties(ps);        path = ps.getString(PROP_PATH, PROP_PATH_DEFAULT);        logMath = (LogMath) ps.getComponent(PROP_LOG_MATH, LogMath.class);    }    /**     * Creates the grammar.     *      * @return the initial node for the grammar.     */    protected GrammarNode createGrammar()	throws IOException {	GrammarNode initialNode = null;	GrammarNode finalNode = null;	// first pass create the FST nodes	int maxNodeId = createNodes(path);	// create the final node:	finalNode = createGrammarNode(++maxNodeId, Dictionary.SILENCE_SPELLING);        finalNode.setFinalNode(true);	// replace each word node with a pair of nodes, which	// consists of the word node and a new dummy end node, which is        // for adding null or backoff transitions	maxNodeId = expandWordNodes(maxNodeId);	ExtendedStreamTokenizer tok = new ExtendedStreamTokenizer(path, true);	// Second pass, add all of the arcs	while (!tok.isEOF()) {	    String token;	    tok.skipwhite();	    token = tok.getString();	    // System.out.println(token);	    if (token == null) {		break;	    } else if (token.equals("I")) {		assert initialNode == null;		int initialID = tok.getInt("initial ID");		String nodeName = "G" + initialID;            // TODO: FlatLinguist requires the initial grammar node            // to contain a single silence. We'll do that for now,            // but once the FlatLinguist is fixed, this should be            // returned to its former method of creating an empty            // initial grammar node            //          initialNode = createGrammarNode(initialID, false);		initialNode = createGrammarNode(initialID,                        Dictionary.SILENCE_SPELLING);		nodes.put(nodeName, initialNode);		// optionally add a silence node		if (addInitialSilenceNode) {		    GrammarNode silenceNode =			createGrammarNode(++maxNodeId, 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?