📄 dynamicflatlinguist.java
字号:
/* * 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.dflat;import java.io.IOException;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.logging.Logger;import java.util.Set;import edu.cmu.sphinx.linguist.Linguist;import edu.cmu.sphinx.linguist.SearchGraph;import edu.cmu.sphinx.linguist.WordSequence;import edu.cmu.sphinx.linguist.SearchState;import edu.cmu.sphinx.linguist.UnitSearchState;import edu.cmu.sphinx.linguist.WordSearchState;import edu.cmu.sphinx.linguist.HMMSearchState;import edu.cmu.sphinx.linguist.SearchStateArc;import edu.cmu.sphinx.linguist.util.HMMPool;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. It makes the following simplifying * assumptions: 1) Zero or one word per grammar node 2) No fan-in allowed ever 3) * No composites (yet) 4) Only Unit, HMMState, and pronunciation states (and * the initial/final grammar state are in the graph (no word, alternative or * grammar states attached). 5) Only valid tranisitions (matching contexts) are * allowed 6) No tree organization of units 7) Branching grammar states are * allowed * * This is a dynamic version of the flat linguist that is more efficient in * terms of startup time and overall footprint * * Note that all probabilties are maintained in the log math domain */public class DynamicFlatLinguist 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 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"; /** * The default value for PROP_OUT_OF_GRAMMAR_PROBABILITY. */ public final static double PROP_OUT_OF_GRAMMAR_PROBABILITY_DEFAULT = 1.0; /** * 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; /** * Sphinx property for the acoustic model to use to build the phone loop * that detects out of grammar utterances. */ public final static String PROP_PHONE_LOOP_ACOUSTIC_MODEL = "phoneLoopAcousticModel"; /** * Sphinx property that defines the name of the logmath to be used by this * search manager. */ public final static String PROP_LOG_MATH = "logMath"; 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 logFillerInsertionProbability; private float languageWeight; private float logOutOfGrammarBranchProbability; private float logPhoneInsertionProbability; private boolean addOutOfGrammarBranch; // ------------------------------------ // Data used for building and maintaining // the search graph // ------------------------------------- private SearchGraph searchGraph; private String name; private Logger logger; private HMMPool hmmPool; SearchStateArc outOfGrammarGraph; // this map is used to manage the set of follow on units for a // particular grammar node. It is used to select the set of // possible right contexts as we leave a node private Map nodeToNextUnitArrayMap; // this map is used to manage the set of possible entry units for // a grammar node. It is used to filter paths so that we only // branch to grammar nodes that match the current right context. private Map nodeToUnitSetMap; // an empty arc (just waiting for Noah, I guess) private final SearchStateArc[] EMPTY_ARCS = new SearchStateArc[0]; /* * (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_UNIT_MANAGER, PropertyType.COMPONENT); registry.register(PROP_FILLER_INSERTION_PROBABILITY, PropertyType.DOUBLE); 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 logger = ps.getLogger(); 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)); logFillerInsertionProbability = logMath.linearToLog(ps.getDouble( PROP_FILLER_INSERTION_PROBABILITY, PROP_FILLER_INSERTION_PROBABILITY_DEFAULT)); languageWeight = ps.getFloat(Linguist.PROP_LANGUAGE_WEIGHT, PROP_LANGUAGE_WEIGHT_DEFAULT); addOutOfGrammarBranch = ps.getBoolean (PROP_ADD_OUT_OF_GRAMMAR_BRANCH, PROP_ADD_OUT_OF_GRAMMAR_BRANCH_DEFAULT); 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)); if (addOutOfGrammarBranch) { phoneLoopAcousticModel = (AcousticModel) ps.getComponent(PROP_PHONE_LOOP_ACOUSTIC_MODEL, AcousticModel.class); } } /** * Returns the search graph * * @return the search graph */ public SearchGraph getSearchGraph() { return searchGraph; } /** * 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 { logger.info("Allocating DFLAT"); allocateAcousticModel(); grammar.allocate(); hmmPool = new HMMPool(acousticModel, logger, unitManager); nodeToNextUnitArrayMap = new HashMap(); nodeToUnitSetMap = new HashMap(); Timer timer = Timer.getTimer("compileGrammar"); timer.start(); compileGrammar(); timer.stop(); logger.info("Done allocating DFLAT"); } /** * Allocates the acoustic model. */ protected void allocateAcousticModel() throws IOException { acousticModel.allocate(); if (addOutOfGrammarBranch) { phoneLoopAcousticModel.allocate(); } } /* * (non-Javadoc) * * @see edu.cmu.sphinx.linguist.Linguist#deallocate() */ public void deallocate() { if (acousticModel != null) { acousticModel.deallocate(); } grammar.deallocate(); } /** * * Called before a recognition */ public void startRecognition() { } /** * Called after a recognition */ public void stopRecognition() { } /** * Returns the LogMath used. * * @return the logMath used */ public LogMath getLogMath() { return logMath; } /** * Returns the log silence insertion probability. * * @return the log silence insertion probability. */ public float getLogSilenceInsertionProbability() { return logSilenceInsertionProbability; } /** * Compiles the grammar */ private void compileGrammar() { // iterate through the grammar nodes Set nodeSet = grammar.getGrammarNodes(); for (Iterator i = nodeSet.iterator(); i.hasNext(); ) { GrammarNode node = (GrammarNode) i.next(); initUnitMaps(node); } searchGraph = new DynamicFlatSearchGraph(); } /** * Initializes the unit maps for this linguist. There are two unit maps: * (a) nodeToNextUnitArrayMap contains an array of unit ids for all possible * units that immediately follow the given grammar node. This is used to * determine the set of exit contexts for words within a grammar node. * (b) nodeToUnitSetMap contains the set of possible entry units for a * given grammar node. This is typically used to determine if a path with * a given right context should branch into a particular grammar node * * @param node the units maps will be created for this node. */ private void initUnitMaps(GrammarNode node) { // collect the set of next units for this node if (nodeToNextUnitArrayMap.get(node) == null) { Set vistedNodes = new HashSet(); Set unitSet = new HashSet(); GrammarArc[] arcs = node.getSuccessors();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -