tiedstateacousticmodel.java

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

JAVA
689
字号
     /**      * Returns an iterator that can be used to iterate through all      * the HMMs of the acoustic model      *      * @return an iterator that can be used to iterate through all      * HMMs in the model. The iterator returns objects of type      * <code>HMM</code>.      */     public Iterator getHMMIterator() {	 return loader.getHMMManager().getIterator();      }     /**      * Returns an iterator that can be used to iterate through all      * the CI units in the acoustic model      *      * @return an iterator that can be used to iterate through all      * CI units. The iterator returns objects of type      * <code>Unit</code>      */     public Iterator getContextIndependentUnitIterator() {	 return loader.getContextIndependentUnits().values().iterator();     }     /**      * Get a composite senone sequence given the unit      * The unit should have a LeftRightContext, where one or two of      * 'left' or 'right' may be null to indicate that the match      * should succeed on any context.      *      * @param unit the unit      *      */     public SenoneSequence getCompositeSenoneSequence(Unit unit,             HMMPosition position) {	 Context context = unit.getContext();	 SenoneSequence compositeSenoneSequence = null;	 compositeSenoneSequence = (SenoneSequence)	     compositeSenoneSequenceCache.get(unit.toString());	 if (logger.isLoggable(Level.FINE)) {	    logger.fine("getCompositeSenoneSequence: " + unit.toString() 		    + ((compositeSenoneSequence != null) ? "Cached" : ""));	 }	 if (compositeSenoneSequence != null) {	     return compositeSenoneSequence;	 }	 // Iterate through all HMMs looking for	 // a) An hmm with a unit that has the proper base	 // b) matches the non-null context	 List senoneSequenceList = new ArrayList();	// collect all senone sequences that match the pattern	 for (Iterator i = getHMMIterator(); i.hasNext(); ) {	     SenoneHMM hmm = (SenoneHMM) i.next();             if (hmm.getPosition() == position) {                 Unit hmmUnit = hmm.getUnit();                 if (hmmUnit.isPartialMatch(unit.getName(), context)) {                     if (logger.isLoggable(Level.FINE)) {                        logger.fine("collected: " + hmm.getUnit().toString());                     }                     senoneSequenceList.add(hmm.getSenoneSequence());                 }            }	 }	 // couldn't find any matches, so at least include the CI unit	 if (senoneSequenceList.size() == 0) {	    Unit ciUnit = unitManager.getUnit(unit.getName(), unit.isFiller());	     SenoneHMM baseHMM = lookupHMM(ciUnit, HMMPosition.UNDEFINED);	     senoneSequenceList.add(baseHMM.getSenoneSequence());	 }	 // Add this point we have all of the senone sequences that	 // match the base/context pattern collected into the list.	 // Next we build a CompositeSenone consisting of all of the	 // senones in each position of the list.	 // First find the longest senone sequence	 int longestSequence = 0;	 for (int i = 0; i < senoneSequenceList.size(); i++) {	     SenoneSequence ss = (SenoneSequence) senoneSequenceList.get(i);	     if (ss.getSenones().length > longestSequence) {		 longestSequence = ss.getSenones().length;	     }	 }	 // now collect all of the senones at each position into	 // arrays so we can create CompositeSenones from them	 // QUESTION: is is possible to have different size senone	 // sequences. For now lets assume the worst case.	 List compositeSenones = new ArrayList();         float logWeight = 0.0f;	 for (int i = 0; i < longestSequence; i++) {	     Set compositeSenoneSet = new HashSet();	     for (int j = 0; j < senoneSequenceList.size(); j++) {		 SenoneSequence senoneSequence = 		     (SenoneSequence) senoneSequenceList.get(j);		 if (i < senoneSequence.getSenones().length) {		     Senone senone = senoneSequence.getSenones()[i];		     compositeSenoneSet.add(senone);		 }	     }	     compositeSenones.add(CompositeSenone.create(                         compositeSenoneSet, logWeight));	 }	 compositeSenoneSequence = SenoneSequence.create(compositeSenones);	 compositeSenoneSequenceCache.put(unit.toString(), 		 compositeSenoneSequence);	 if (logger.isLoggable(Level.FINE)) {	    logger.fine(unit.toString() + " consists of " +		    compositeSenones.size() + " composite senones");	    if (logger.isLoggable(Level.FINEST)) {		 compositeSenoneSequence.dump("am");	    }	 }	 return compositeSenoneSequence;     }     /**      * Returns the size of the left context for context dependent      * units      *      * @return the left context size      */     public int getLeftContextSize() {	 return loader.getLeftContextSize();     }     /**      * Returns the size of the right context for context dependent      * units      *      * @return the left context size      */     public int getRightContextSize() {	 return loader.getRightContextSize();     }    /**     * Given a unit, returns the HMM that exactly matches the given     * unit.       *     * @param unit 		the unit of interest     * @param position 	the position of the unit of interest     *     * @return 	the HMM that exactly matches, or null if no match     * 		could be found.     */    private SenoneHMM lookupHMM(Unit unit, HMMPosition position) {        return (SenoneHMM) loader.getHMMManager().get(position, unit);    }            /**     * Creates a string useful for tagging a composite senone sequence     *     * @param base the base unit     * @param context the context     *     * @return the tag associated with the composite senone sequence     */    private String makeTag(Unit base, Context context) {        StringBuffer sb = new StringBuffer();        sb.append("(");        sb.append(base.getName());        sb.append("-");        sb.append(context.toString());        sb.append(")");        return sb.toString();    }               /**     * Dumps information about this model to the logger     */    protected void logInfo() {        if (loader != null) {            loader.logInfo();        }        logger.info("CompositeSenoneSequences: " +                     compositeSenoneSequenceCache.size());    }            /**     * Searches an hmm at any position     *     * @param unit the unit to search for     *     * @return hmm the hmm or null if it was not found     */    private SenoneHMM getHMMAtAnyPosition(Unit unit) {        SenoneHMM hmm = null;        HMMManager mgr = loader.getHMMManager();        for (Iterator i = HMMPosition.iterator();              hmm == null && i.hasNext(); ) {            HMMPosition pos = (HMMPosition) i.next();            hmm = (SenoneHMM) mgr.get(pos, unit);        }        return hmm;    }        /**     * Given a unit, search for the HMM associated with this unit by     * replacing all non-silence filler contexts with the silence     * filler context     *     * @param unit the unit of interest     *     * @return the associated hmm or null     */    private SenoneHMM getHMMInSilenceContext(Unit unit, HMMPosition position) {        SenoneHMM hmm = null;        HMMManager mgr = loader.getHMMManager();        Context context = unit.getContext();                if (context instanceof LeftRightContext) {            LeftRightContext lrContext = (LeftRightContext) context;                        Unit[] lc  = lrContext.getLeftContext();            Unit[] rc  = lrContext.getRightContext();                        Unit[] nlc;            Unit[] nrc;                        if (hasNonSilenceFiller(lc)) {                nlc = replaceNonSilenceFillerWithSilence(lc);            } else {                nlc = lc;            }                        if (hasNonSilenceFiller(rc)) {                nrc = replaceNonSilenceFillerWithSilence(rc);            } else {                nrc = rc;            }                        if (nlc != lc || nrc != rc) {                Context newContext =  LeftRightContext.get(nlc, nrc);                Unit newUnit = unitManager.getUnit(unit.getName(),                        unit.isFiller(), newContext);                hmm = (SenoneHMM) mgr.get(position, newUnit);                if (hmm == null) {                    hmm = getHMMAtAnyPosition(newUnit);                }            }        }        return hmm;    }            /**     * Some debugging code that looks for illformed contexts     *      * @param msg the message associated with the check     * @param c the context to check     */    private void checkNull(String msg, Unit[] c) {        for (int i = 0; i < c.length; i++) {            if (c[i] == null) {                System.out.println("null at index " + i + " of " + msg);            }        }    }            /**     * Returns true if the array of units contains     * a non-silence filler     *     * @param units the units to check     *     * @return true if the array contains a filler that is not the     * silence filler     */    private boolean hasNonSilenceFiller(Unit[] units) {	if (units == null) {	    return false;	}        for (int i = 0; i < units.length; i++) {            if (units[i].isFiller() &&                !units[i].equals(UnitManager.SILENCE)) {                return true;            }        }        return false;    }        /**     * Returns a unit array with all non-silence filler units replaced     * with the silence filler     * a non-silence filler     *     * @param context the context to check     *     * @return true if the array contains a filler that is not the     * silence filler     */    private Unit[] replaceNonSilenceFillerWithSilence(Unit[] context) {        Unit[] replacementContext = new Unit[context.length];        for (int i = 0; i < context.length; i++) {            if (context[i].isFiller() &&                    !context[i].equals(UnitManager.SILENCE)) {                replacementContext[i] = UnitManager.SILENCE;            } else {                replacementContext[i] = context[i];            }        }        return replacementContext;    }    /**     * Returns the properties of this acoustic model.     *     * @return the properties of this acoustic model     */    public Properties getProperties() {        if (properties == null) {            properties = new Properties();            try {                properties.load                    (getClass().getResource("model.props").openStream());            } catch (IOException ioe) {                ioe.printStackTrace();            }        }        return properties;    }}

⌨️ 快捷键说明

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