flatlinguist.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 1,628 行 · 第 1/5 页
JAVA
1,628 行
/* * 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.flat;import java.io.IOException;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import edu.cmu.sphinx.linguist.Linguist;import edu.cmu.sphinx.linguist.SearchGraph;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.SearchStateArc;import edu.cmu.sphinx.linguist.acoustic.AcousticModel;import edu.cmu.sphinx.linguist.acoustic.HMM;import edu.cmu.sphinx.linguist.acoustic.HMMPosition;import edu.cmu.sphinx.linguist.acoustic.HMMState;import edu.cmu.sphinx.linguist.acoustic.HMMStateArc;import edu.cmu.sphinx.linguist.acoustic.LeftRightContext;import edu.cmu.sphinx.linguist.acoustic.Unit;import edu.cmu.sphinx.linguist.acoustic.UnitManager;import edu.cmu.sphinx.linguist.dictionary.Dictionary;import edu.cmu.sphinx.linguist.dictionary.Pronunciation;import edu.cmu.sphinx.linguist.dictionary.Word;import edu.cmu.sphinx.linguist.language.grammar.Grammar;import edu.cmu.sphinx.linguist.language.grammar.GrammarArc;import edu.cmu.sphinx.linguist.language.grammar.GrammarNode;import edu.cmu.sphinx.util.LogMath;import edu.cmu.sphinx.util.StatisticsVariable;import edu.cmu.sphinx.util.Timer;import edu.cmu.sphinx.util.props.Configurable;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;/** * A simple form of the linguist. * * The flat linguist takes a Grammar graph (as returned by the * underlying, configurable grammar), and generates a search graph for * this grammar. * * It makes the following simplifying * assumptions: * * <ul> * <li>Zero or one word per grammar node * <li> No fan-in allowed ever * <li> No composites (yet) * <li> Only Unit, HMMState, and pronunciation states (and * the initial/final grammar state are in the graph (no word, alternative or * grammar states attached). * <li> Only valid tranisitions (matching contexts) are * allowed * <li> No tree organization of units * <li> Branching grammar states are allowed * </ul> * * <p> * Note that all probabilties are maintained in the log math domain */public class FlatLinguist implements Linguist, Configurable { /** * A sphinx property used to define the grammar to use when building the * search graph */ public final static String PROP_GRAMMAR = "grammar"; /** * A sphinx property used to define the unit manager to use * when building the search graph */ public final static String PROP_UNIT_MANAGER = "unitManager"; /** * A sphinx property used to define the acoustic model to use when building * the search graph */ public final static String PROP_ACOUSTIC_MODEL = "acousticModel"; /** * Sphinx property that defines the name of the logmath to be used by this * search manager. */ public final static String PROP_LOG_MATH = "logMath"; /** * Sphinx property used to determine whether or not the gstates are dumped. */ public final static String PROP_DUMP_GSTATES = "dumpGstates"; /** * The default value for the PROP_DUMP_GSTATES property */ public final static boolean PROP_DUMP_GSTATES_DEFAULT = false; /** * Sphinx property that specifies whether to add a branch for detecting * out-of-grammar utterances. */ public final static String PROP_ADD_OUT_OF_GRAMMAR_BRANCH = "addOutOfGrammarBranch"; /** * Default value of PROP_ADD_OUT_OF_GRAMMAR_BRANCH. */ public final static boolean PROP_ADD_OUT_OF_GRAMMAR_BRANCH_DEFAULT = false; /** * Sphinx property for the probability of entering the out-of-grammar * branch. */ public final static String PROP_OUT_OF_GRAMMAR_PROBABILITY = "outOfGrammarProbability"; /** * Sphinx property for the acoustic model used for the CI phone loop. */ public static final String PROP_PHONE_LOOP_ACOUSTIC_MODEL = "phoneLoopAcousticModel"; /** * Sphinx property for the probability of inserting a CI phone in * the out-of-grammar ci phone loop */ public static final String PROP_PHONE_INSERTION_PROBABILITY = "phoneInsertionProbability"; /** * Default value for PROP_PHONE_INSERTION_PROBABILITY */ public static final double PROP_PHONE_INSERTION_PROBABILITY_DEFAULT = 1.0; /** * The default value for PROP_OUT_OF_GRAMMAR_PROBABILITY. */ public final static double PROP_OUT_OF_GRAMMAR_PROBABILITY_DEFAULT = 1.0; private final static float logOne = LogMath.getLogOne(); // ---------------------------------- // Subcomponents that are configured // by the property sheet // ----------------------------------- private Grammar grammar; private AcousticModel acousticModel; private AcousticModel phoneLoopAcousticModel; private LogMath logMath; private UnitManager unitManager; // ------------------------------------ // Data that is configured by the // property sheet // ------------------------------------ private float logWordInsertionProbability; private float logSilenceInsertionProbability; private float logUnitInsertionProbability; private float logOutOfGrammarBranchProbability; private float logPhoneInsertionProbability; private boolean showSentenceHMM; private boolean showCompilationProgress = true; private boolean spreadWordProbabilitiesAcrossPronunciations; private boolean dumpGStates; private boolean addOutOfGrammarBranch; private float languageWeight; // ----------------------------------- // Data for monitoring performance // ------------------------------------ private StatisticsVariable totalStates; private StatisticsVariable totalArcs; private StatisticsVariable actualArcs; private SearchGraph searchGraph; private transient int totalStateCounter = 0; private final static boolean tracing = false; // ------------------------------------ // Data used for building and maintaining // the search graph // ------------------------------------- private Map nodeStateMap; private Map arcPool; private transient Collection stateSet = null; private String name; private GrammarNode initialGrammarState = null; /** * Returns the search graph * * @return the search graph */ public SearchGraph getSearchGraph() { return searchGraph; } /* * (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 { this.name = name; registry.register(PROP_GRAMMAR, PropertyType.COMPONENT); registry.register(PROP_ACOUSTIC_MODEL, PropertyType.COMPONENT); registry.register(PROP_LOG_MATH, PropertyType.COMPONENT); registry.register(PROP_WORD_INSERTION_PROBABILITY, PropertyType.DOUBLE); registry.register(PROP_SILENCE_INSERTION_PROBABILITY, PropertyType.DOUBLE); registry.register(PROP_UNIT_INSERTION_PROBABILITY, PropertyType.DOUBLE); registry.register(PROP_LANGUAGE_WEIGHT, PropertyType.FLOAT); registry.register(PROP_SHOW_SEARCH_SPACE, PropertyType.BOOLEAN); registry.register(PROP_DUMP_GSTATES, PropertyType.BOOLEAN); registry.register(PROP_SHOW_COMPILATION_PROGRESS, PropertyType.BOOLEAN); registry.register(PROP_SPREAD_WORD_PROBABILITIES_ACROSS_PRONUNCIATIONS, PropertyType.BOOLEAN); registry.register(PROP_UNIT_MANAGER, PropertyType.COMPONENT); registry.register(PROP_ADD_OUT_OF_GRAMMAR_BRANCH, PropertyType.BOOLEAN); registry.register(PROP_OUT_OF_GRAMMAR_PROBABILITY, PropertyType.DOUBLE); registry.register(PROP_PHONE_INSERTION_PROBABILITY, PropertyType.DOUBLE); registry.register(PROP_PHONE_LOOP_ACOUSTIC_MODEL, 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 { // hookup to all of the components setupAcousticModel(ps); logMath = (LogMath) ps.getComponent(PROP_LOG_MATH, LogMath.class); grammar = (Grammar) ps.getComponent(PROP_GRAMMAR, Grammar.class); unitManager = (UnitManager) ps.getComponent(PROP_UNIT_MANAGER, UnitManager.class); // get the rest of the configuration data logWordInsertionProbability = logMath.linearToLog(ps.getDouble( PROP_WORD_INSERTION_PROBABILITY, PROP_WORD_INSERTION_PROBABILITY_DEFAULT)); logSilenceInsertionProbability = logMath.linearToLog(ps.getDouble( PROP_SILENCE_INSERTION_PROBABILITY, PROP_SILENCE_INSERTION_PROBABILITY_DEFAULT)); logUnitInsertionProbability = logMath.linearToLog(ps.getDouble( PROP_UNIT_INSERTION_PROBABILITY, PROP_UNIT_INSERTION_PROBABILITY_DEFAULT)); languageWeight = ps.getFloat(Linguist.PROP_LANGUAGE_WEIGHT, PROP_LANGUAGE_WEIGHT_DEFAULT); showSentenceHMM = ps.getBoolean(Linguist.PROP_SHOW_SEARCH_SPACE, PROP_SHOW_SEARCH_SPACE_DEFAULT); dumpGStates = ps.getBoolean(PROP_DUMP_GSTATES, PROP_DUMP_GSTATES_DEFAULT); showCompilationProgress = ps.getBoolean( PROP_SHOW_COMPILATION_PROGRESS, PROP_SHOW_COMPILATION_PROGRESS_DEFAULT); spreadWordProbabilitiesAcrossPronunciations = ps.getBoolean( PROP_SPREAD_WORD_PROBABILITIES_ACROSS_PRONUNCIATIONS, PROP_SPREAD_WORD_PROBABILITIES_ACROSS_PRONUNCIATIONS_DEFAULT); addOutOfGrammarBranch = ps.getBoolean (PROP_ADD_OUT_OF_GRAMMAR_BRANCH, PROP_ADD_OUT_OF_GRAMMAR_BRANCH_DEFAULT); if (addOutOfGrammarBranch) { logOutOfGrammarBranchProbability = logMath.linearToLog (ps.getDouble(PROP_OUT_OF_GRAMMAR_PROBABILITY, PROP_OUT_OF_GRAMMAR_PROBABILITY_DEFAULT)); logPhoneInsertionProbability = logMath.linearToLog (ps.getDouble(PROP_PHONE_INSERTION_PROBABILITY, PROP_PHONE_INSERTION_PROBABILITY_DEFAULT)); phoneLoopAcousticModel = (AcousticModel) ps.getComponent(PROP_PHONE_LOOP_ACOUSTIC_MODEL, AcousticModel.class); } } /** * Sets up the acoustic model. * * @param ps the PropertySheet from which to obtain the acoustic model */ protected void setupAcousticModel(PropertySheet ps) throws PropertyException { acousticModel = (AcousticModel) ps.getComponent(PROP_ACOUSTIC_MODEL, AcousticModel.class); } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#getName() */ public String getName() { return name; } /* * (non-Javadoc) * * @see edu.cmu.sphinx.linguist.Linguist#allocate() */ public void allocate() throws IOException { allocateAcousticModel(); grammar.allocate(); totalStates = StatisticsVariable.getStatisticsVariable(getName(), "totalStates"); totalArcs = StatisticsVariable.getStatisticsVariable(getName(), "totalArcs"); actualArcs = StatisticsVariable.getStatisticsVariable(getName(), "actualArcs"); stateSet = compileGrammar();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?