Exemple #1
0
        /**
         * /// Creates a UnitState. Gets the left and right contexts from the unit itself.
         *
         * /// @param parent the parent state
         * /// @param which  the index of the given state
         * /// @param unit   the unit associated with this state
         */
        public UnitState(PronunciationState parent, int which, Unit unit)
            : base("U", parent, which)
        {
            this.Unit = unit;
            Unit[] units  = parent.Pronunciation.Units;
            int    length = units.Length;

            // If the last phone is SIL, then we should be using
            // a word-ending phone for the last phone. Decrementing
            // length will make the phone before SIL the last phone.

            if (units[length - 1] == UnitManager.Silence && length > 1)
            {
                length--;
            }

            if (length == 1)
            {
                _position = HMMPosition.Single;
            }
            else if (which == 0)
            {
                _position = HMMPosition.Begin;
            }
            else if (which == length - 1)
            {
                _position = HMMPosition.End;
            }
        }
        private HMM getCompositeHMM(Unit unit, HMMPosition position)
        {
            Unit           unit2 = this.unitManager.getUnit(unit.getName(), unit.isFiller(), Context.__EMPTY_CONTEXT);
            SenoneSequence compositeSenoneSequence = this.getCompositeSenoneSequence(unit, position);
            SenoneHMM      senoneHMM = (SenoneHMM)this.lookupNearestHMM(unit2, HMMPosition.__UNDEFINED, true);

            float[][] transitionMatrix = senoneHMM.getTransitionMatrix();
            return(new SenoneHMM(unit, compositeSenoneSequence, transitionMatrix, position));
        }
Exemple #3
0
        /// <summary>
        /// Retrieves an HMM by position and unit.
        /// </summary>
        /// <param name="position">The position of the HMM.</param>
        /// <param name="unit">The unit that this HMM represents.</param>
        /// <returns>The HMM for the unit at the given position or null if no HMM at the position could be found.</returns>
        public IHMM Get(HMMPosition position, Unit unit)
        {
            var units = _hmmsPerPosition[position];

            if (units == null || units.Count == 0 || !units.ContainsKey(unit))
            {
                return(null);
            }
            return(units[unit]);
        }
Exemple #4
0
        public override HMM lookupNearestHMM(Unit unit, HMMPosition position, bool exactMatch)
        {
            HMM result = null;

            if (!exactMatch || position == HMMPosition.__UNDEFINED)
            {
                unit   = unit.getBaseUnit();
                result = (HMM)this.hmmMap.get(unit);
            }
            return(result);
        }
 internal TrivialHMM(Unit unit, HMMPosition hmmposition)
 {
     this.unit      = unit;
     this.position  = hmmposition;
     this.hmmStates = new HMMState[4];
     this.baseUnit  = unit.getBaseUnit();
     for (int i = 0; i < this.hmmStates.Length; i++)
     {
         int num = (i == this.hmmStates.Length - 1) ? 1 : 0;
         this.hmmStates[i] = new TrivialHMMState(this, i, num != 0);
     }
 }
Exemple #6
0
            private HMMStateState getHMMStates(UnitState unitState)
            {
                Unit          unit          = unitState.getUnit();
                HMMPosition   position      = unitState.getPosition();
                HMM           hmm           = FlatLinguist.access_700(this.this_0).lookupNearestHMM(unit, position, false);
                HMMState      initialState  = hmm.getInitialState();
                HMMStateState hmmstateState = new HMMStateState(unitState, initialState);

                this.attachState(unitState, hmmstateState, 0f, 0f);
                this.addStateToCache(hmmstateState);
                return(this.expandHMMTree(unitState, hmmstateState));
            }
        public HMMManager()
        {
            this.allHMMs         = new java.util.ArrayList();
            this.hmmsPerPosition = new EnumMap(ClassLiteral <HMMPosition> .Value);
            HMMPosition[] array = HMMPosition.values();
            int           num   = array.Length;

            for (int i = 0; i < num; i++)
            {
                HMMPosition hmmposition = array[i];
                this.hmmsPerPosition.put(hmmposition, new HashMap());
            }
        }
        /**
         * /// Gets a composite HMM for the given unit and context
         *
         * /// @param unit     the unit for the hmm
         * /// @param position the position of the unit within the word
         * /// @return a composite HMM
         */
        private IHMM GetCompositeHMM(Unit unit, HMMPosition position)
        {
            var ciUnit = UnitManager.GetUnit(unit.Name, unit.IsFiller,
                                             Context.EmptyContext);

            var compositeSequence = GetCompositeSenoneSequence(unit,
                                                               position);

            var contextIndependentHMM = (SenoneHMM)LookupNearestHMM(ciUnit,
                                                                    HMMPosition.Undefined, true);
            var tmat = contextIndependentHMM.TransitionMatrix;

            return(new SenoneHMM(unit, compositeSequence, tmat, position));
        }
Exemple #9
0
            /**
             * /// Given a unit state, return the set of sentence hmm states associated with the unit
             *
             * /// @param unitState the unit state of intereset
             * /// @return the hmm tree for the unit
             */
            private HMMStateState GetHMMStates(UnitState unitState)
            {
                HMMStateState hmmTree;
                HMMStateState finalState;
                Unit          unit         = unitState.Unit;
                HMMPosition   position     = unitState.GetPosition();
                IHMM          hmm          = _parent._acousticModel.LookupNearestHMM(unit, position, false);
                IHMMState     initialState = hmm.GetInitialState();

                hmmTree = new HMMStateState(unitState, initialState);
                AttachState(unitState, hmmTree, LogOne, LogOne);
                AddStateToCache(hmmTree);
                finalState = ExpandHMMTree(unitState, hmmTree);
                return(finalState);
            }
        /**
         * /// 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;
            var       mgr     = Loader.HMMManager;
            var       context = unit.Context;

            if (context is LeftRightContext)
            {
                var lrContext = (LeftRightContext)context;

                var lc = lrContext.LeftContext;
                var rc = lrContext.RightContext;

                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);
                    var     newUnit    = UnitManager.GetUnit(unit.Name,
                                                             unit.IsFiller, newContext);
                    hmm = (SenoneHMM)mgr.Get(position, newUnit);
                    if (hmm == null)
                    {
                        hmm = GetHMMAtAnyPosition(newUnit);
                    }
                }
            }
            return(hmm);
        }
Exemple #11
0
        /**
         * /// Constructs an HMM
         *
         * /// @param unit             the unit for this HMM
         * /// @param senoneSequence   the sequence of senones for this HMM
         * /// @param transitionMatrix the state transition matrix
         * /// @param position         the position associated with this HMM
         */
        public SenoneHMM(Unit unit, SenoneSequence senoneSequence,
                         float[][] transitionMatrix, HMMPosition position)
        {
            Unit             = unit;
            SenoneSequence   = senoneSequence;
            TransitionMatrix = transitionMatrix;
            Position         = position;
            Utilities.ObjectTracker("HMM", _objectCount++);

            _hmmStates = new IHMMState[transitionMatrix.Length];
            for (var i = 0; i < _hmmStates.Length; i++)
            {
                _hmmStates[i] = new SenoneHMMState(this, i);
            }
            // baseUnit = Unit.getUnit(unit.getName());
            BaseUnit = unit.BaseUnit;
        }
        private SenoneHMM getHMMAtAnyPosition(Unit unit)
        {
            HMMManager hmmmanager = this.loader.getHMMManager();

            HMMPosition[] array = HMMPosition.values();
            int           num   = array.Length;

            for (int i = 0; i < num; i++)
            {
                HMMPosition position  = array[i];
                SenoneHMM   senoneHMM = (SenoneHMM)hmmmanager.get(position, unit);
                if (senoneHMM != null)
                {
                    return(senoneHMM);
                }
            }
            return(null);
        }
        private SenoneHMM getHMMInSilenceContext(Unit unit, HMMPosition position)
        {
            SenoneHMM  senoneHMM  = null;
            HMMManager hmmmanager = this.loader.getHMMManager();
            Context    context    = unit.getContext();

            if (context is LeftRightContext)
            {
                LeftRightContext leftRightContext = (LeftRightContext)context;
                Unit[]           leftContext      = leftRightContext.getLeftContext();
                Unit[]           rightContext     = leftRightContext.getRightContext();
                Unit[]           array;
                if (this.hasNonSilenceFiller(leftContext))
                {
                    array = this.replaceNonSilenceFillerWithSilence(leftContext);
                }
                else
                {
                    array = leftContext;
                }
                Unit[] array2;
                if (this.hasNonSilenceFiller(rightContext))
                {
                    array2 = this.replaceNonSilenceFillerWithSilence(rightContext);
                }
                else
                {
                    array2 = rightContext;
                }
                if (array != leftContext || array2 != rightContext)
                {
                    LeftRightContext context2 = LeftRightContext.get(array, array2);
                    Unit             unit2    = this.unitManager.getUnit(unit.getName(), unit.isFiller(), context2);
                    senoneHMM = (SenoneHMM)hmmmanager.get(position, unit2);
                    if (senoneHMM == null)
                    {
                        senoneHMM = this.getHMMAtAnyPosition(unit2);
                    }
                }
            }
            return(senoneHMM);
        }
Exemple #14
0
        public override HMM get(HMMPosition position, Unit unit)
        {
            HMM hmm = base.get(position, unit);

            if (null != hmm)
            {
                return(hmm);
            }
            int[] array = new int[3];
            array[1] = ((Integer)this.symbolTable.get(unit.getName())).intValue();
            if (unit.isContextDependent())
            {
                LeftRightContext leftRightContext = (LeftRightContext)unit.getContext();
                Unit             unit2            = leftRightContext.getLeftContext()[0];
                Unit             unit3            = leftRightContext.getRightContext()[0];
                array[0] = ((Integer)this.symbolTable.get(unit2.getName())).intValue();
                array[2] = ((Integer)this.symbolTable.get(unit3.getName())).intValue();
            }
            else
            {
                array[0] = ((Integer)this.symbolTable.get("SIL")).intValue();
                array[2] = ((Integer)this.symbolTable.get("SIL")).intValue();
            }
            int[] array2 = new int[]
            {
                this.eventMap.map(0, array),
                this.eventMap.map(1, array),
                this.eventMap.map(2, array)
            };
            SenoneSequence senoneSequence = new SenoneSequence(new Senone[]
            {
                (Senone)this.senonePool.get(array2[0]),
                (Senone)this.senonePool.get(array2[1]),
                (Senone)this.senonePool.get(array2[2])
            });

            float[][] transitionMatrix = this.transitionModel.getTransitionMatrix(array[1], array2);
            SenoneHMM senoneHMM        = new SenoneHMM(unit, senoneSequence, transitionMatrix, position);

            this.put(senoneHMM);
            return(senoneHMM);
        }
Exemple #15
0
        /**
         * /// Retrieves an HMM for a unit in context. If there is no direct match, the
         * /// nearest match will be used. Note that we are currently only dealing with,
         * /// at most, single unit left and right contexts.
         * ///
         * /// @param base the base CI unit
         * /// @param lc  the left context
         * /// @param rc the right context
         * /// @param pos the position of the base unit within the word
         * /// @return the HMM. (This should never return null)
         */
        public IHMM GetHMM(Unit _base, Unit lc, Unit rc, HMMPosition pos)
        {
            var id  = -1;
            var bid = GetId(_base);


            var lid = GetId(lc);
            var rid = GetId(rc);

            if (!IsValidId(bid))
            {
                //Trace.TraceError("Bad HMM Unit: " + _base.getName());
                return(null);
            }
            if (!IsValidId(lid))
            {
                //Trace.TraceError("Bad HMM Unit: " + lc.getName());
                return(null);
            }
            if (!IsValidId(rid))
            {
                //Trace.TraceError("Bad HMM Unit: " + rc.getName());
                return(null);
            }
            id = BuildId(bid, lid, rid);

            if (id < 0)
            {
                //Trace.TraceError("Unable to build HMM Unit ID for " + _base.GetType().Name
                //        + " lc=" + lc.getName() + " rc=" + rc.getName());
                return(null);
            }
            var hmm = GetHMM(id, pos);

            if (hmm == null)
            {
                //Trace.TraceError("Missing HMM Unit for " + _base.GetType().Name + " lc="
                //        + lc.getName() + " rc=" + rc.getName());
            }

            return(hmm);
        }
 public UnitState(Unit unit, HMMPosition position)
 {
     this.position = HMMPosition.__INTERNAL;
     this.unit     = unit;
     this.position = position;
 }
 public SenoneHMM(Unit unit, SenoneSequence senoneSequence, float[][] transitionMatrix, HMMPosition position)
 {
     this.unit             = unit;
     this.senoneSequence   = senoneSequence;
     this.transitionMatrix = transitionMatrix;
     this.position         = position;
     Utilities.objectTracker("HMM", SenoneHMM.objectCount++);
     this.hmmStates = new HMMState[transitionMatrix.Length];
     for (int i = 0; i < this.hmmStates.Length; i++)
     {
         this.hmmStates[i] = new SenoneHMMState(this, i);
     }
     this.baseUnit = unit.getBaseUnit();
 }
Exemple #18
0
        protected internal virtual void loadHMMPool(bool useCDUnits, HTKLoader.HTKStruct htkModels, string path)
        {
            if (!this.tie1ph)
            {
                Iterator iterator = htkModels.hmmsHTK.get1phIt();
                while (iterator.hasNext())
                {
                    SingleHMM singleHMM = (SingleHMM)iterator.next();
                    if (singleHMM == null)
                    {
                        break;
                    }
                    string baseName = singleHMM.getName();
                    string text;
                    if (String.instancehelper_equals(baseName, "sil") || String.instancehelper_equals(baseName, "sp") || String.instancehelper_equals(baseName, "bb") || String.instancehelper_equals(baseName, "xx") || String.instancehelper_equals(baseName, "hh"))
                    {
                        text = "filler";
                    }
                    else
                    {
                        text = "nofiller";
                    }
                    int   trIdx   = singleHMM.trIdx;
                    int   nstates = singleHMM.getNstates();
                    int[] array   = new int[singleHMM.getNbEmittingStates()];
                    int   num     = 0;
                    for (int i = 0; i < nstates; i++)
                    {
                        if (singleHMM.isEmitting(i))
                        {
                            HTK.HMMState state = singleHMM.getState(i);
                            array[num] = htkModels.hmmsHTK.getStateIdx(state);
                            num++;
                        }
                    }
                    Unit unit = this.unitManager.getUnit(baseName, String.instancehelper_equals(text, "filler"));
                    this.contextIndependentUnits.put(unit.getName(), unit);
                    if (this.logger.isLoggable(Level.FINE))
                    {
                        this.logger.fine(new StringBuilder().append("Loaded ").append(unit).toString());
                    }
                    if (unit.isFiller() && String.instancehelper_equals(unit.getName(), "SIL"))
                    {
                        unit = UnitManager.__SILENCE;
                    }
                    float[][]      transitionMatrix = (float[][])this.matrixPool.get(trIdx);
                    SenoneSequence senoneSequence   = this.getSenoneSequence(array);
                    SenoneHMM      hmm = new SenoneHMM(unit, senoneSequence, transitionMatrix, HMMPosition.lookup("-"));
                    this.hmmManager.put(hmm);
                }
            }
            else
            {
                for (int j = 0; j < htkModels.hmmsHTK.getNhmms(); j++)
                {
                    SingleHMM singleHMM = htkModels.hmmsHTK.getHMM(j);
                    if (singleHMM == null)
                    {
                        break;
                    }
                    string baseName = singleHMM.getBaseName();
                    if (!this.contextIndependentUnits.containsKey(baseName))
                    {
                        string text;
                        if (String.instancehelper_equals(baseName, "SIL") || String.instancehelper_equals(baseName, "SP") || String.instancehelper_equals(baseName, "BB") || String.instancehelper_equals(baseName, "XX") || String.instancehelper_equals(baseName, "HH"))
                        {
                            text = "filler";
                        }
                        else
                        {
                            text = "nofiller";
                        }
                        int   trIdx   = singleHMM.trIdx;
                        int   nstates = singleHMM.getNstates();
                        int[] array   = new int[singleHMM.getNbEmittingStates()];
                        int   num     = 0;
                        for (int i = 0; i < nstates; i++)
                        {
                            if (singleHMM.isEmitting(i))
                            {
                                HTK.HMMState state = singleHMM.getState(i);
                                array[num] = htkModels.hmmsHTK.getStateIdx(state);
                                num++;
                            }
                        }
                        Unit unit = this.unitManager.getUnit(baseName, String.instancehelper_equals(text, "filler"));
                        this.contextIndependentUnits.put(unit.getName(), unit);
                        if (this.logger.isLoggable(Level.FINE))
                        {
                            this.logger.fine(new StringBuilder().append("Loaded ").append(unit).toString());
                        }
                        if (unit.isFiller() && String.instancehelper_equals(unit.getName(), "SIL"))
                        {
                            unit = UnitManager.__SILENCE;
                        }
                        float[][]      transitionMatrix = (float[][])this.matrixPool.get(trIdx);
                        SenoneSequence senoneSequence   = this.getSenoneSequence(array);
                        SenoneHMM      hmm = new SenoneHMM(unit, senoneSequence, transitionMatrix, HMMPosition.lookup("-"));
                        this.hmmManager.put(hmm);
                    }
                }
            }
            string text2 = "";
            Unit   unit2 = null;

            int[]          ssid            = null;
            SenoneSequence senoneSequence2 = null;
            ArrayList      arrayList       = new ArrayList();
            Iterator       iterator2       = htkModels.hmmsHTK.get3phIt();

            while (iterator2.hasNext())
            {
                SingleHMM singleHMM2 = (SingleHMM)iterator2.next();
                if (singleHMM2 == null)
                {
                    break;
                }
                string baseName2 = singleHMM2.getBaseName();
                string text3     = singleHMM2.getLeft();
                string text4     = singleHMM2.getRight();
                if (String.instancehelper_equals(text3, "-"))
                {
                    text3 = "SIL";
                }
                if (String.instancehelper_equals(text4, "-"))
                {
                    text4 = "SIL";
                }
                string text5 = new StringBuilder().append(text3).append(' ').append(baseName2).append(' ').append(text4).toString();
                if (!arrayList.contains(text5))
                {
                    arrayList.add(text5);
                    text5 = "i";
                    int   trIdx2  = singleHMM2.trIdx;
                    int   nstates = singleHMM2.getNstates();
                    int[] array2  = new int[singleHMM2.getNbEmittingStates()];
                    int   num2    = 0;
                    for (int k = 0; k < nstates; k++)
                    {
                        if (singleHMM2.isEmitting(k))
                        {
                            HTK.HMMState state2 = singleHMM2.getState(k);
                            array2[num2] = htkModels.hmmsHTK.getStateIdx(state2);
                            num2++;
                        }
                    }
                    if (useCDUnits)
                    {
                        string text6 = new StringBuilder().append(baseName2).append(' ').append(text3).append(' ').append(text4).toString();
                        Unit   unit3;
                        if (String.instancehelper_equals(text6, text2))
                        {
                            unit3 = unit2;
                        }
                        else
                        {
                            LeftRightContext context = LeftRightContext.get(new Unit[]
                            {
                                (Unit)this.contextIndependentUnits.get(text3)
                            }, new Unit[]
                            {
                                (Unit)this.contextIndependentUnits.get(text4)
                            });
                            unit3 = this.unitManager.getUnit(baseName2, false, context);
                        }
                        text2 = text6;
                        unit2 = unit3;
                        if (this.logger.isLoggable(Level.FINE))
                        {
                            this.logger.fine(new StringBuilder().append("Loaded ").append(unit3).toString());
                        }
                        float[][]      transitionMatrix2 = (float[][])this.matrixPool.get(trIdx2);
                        SenoneSequence senoneSequence3   = senoneSequence2;
                        if (senoneSequence3 == null || !this.sameSenoneSequence(array2, ssid))
                        {
                            senoneSequence3 = this.getSenoneSequence(array2);
                        }
                        senoneSequence2 = senoneSequence3;
                        ssid            = array2;
                        SenoneHMM hmm2 = new SenoneHMM(unit3, senoneSequence3, transitionMatrix2, HMMPosition.lookup(text5));
                        this.hmmManager.put(hmm2);
                    }
                }
            }
        }
Exemple #19
0
 public static bool IsWordBeginning(this HMMPosition source)
 {
     return(source == HMMPosition.Single || source == HMMPosition.Begin);
 }
        /**
         * /// 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)
        {
            var unitStr = unit.ToString();
            var compositeSenoneSequence = _compositeSenoneSequenceCache.Get(unitStr);


            this.LogInfo("getCompositeSenoneSequence: "
                         + unit +
                         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

            var context = unit.Context;
            List <SenoneSequence> senoneSequenceList;

            senoneSequenceList = new List <SenoneSequence>();

            // collect all senone sequences that match the pattern
            for (var i = GetHMMIterator(); i.MoveNext();)
            {
                var hmm = (SenoneHMM)i.Current;
                if (hmm.Position == position)
                {
                    var hmmUnit = hmm.Unit;
                    if (hmmUnit.IsPartialMatch(unit.Name, context))
                    {
                        this.LogInfo("collected: " + hmm.Unit);

                        senoneSequenceList.Add(hmm.SenoneSequence);
                    }
                }
            }

            // couldn't find any matches, so at least include the CI unit
            if (senoneSequenceList.Count == 0)
            {
                var ciUnit  = UnitManager.GetUnit(unit.Name, unit.IsFiller);
                var baseHMM = lookupHMM(ciUnit, HMMPosition.Undefined);
                senoneSequenceList.Add(baseHMM.SenoneSequence);
            }

            // 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

            var longestSequence = 0;

            foreach (var ss in senoneSequenceList)
            {
                if (ss.Senones.Length > longestSequence)
                {
                    longestSequence = ss.Senones.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.

            var compositeSenones = new List <CompositeSenone>();
            var logWeight        = 0.0f;

            for (var i = 0; i < longestSequence; i++)
            {
                var compositeSenoneSet = new HashSet <ISenone>();
                foreach (var senoneSequence in senoneSequenceList)
                {
                    if (i < senoneSequence.Senones.Length)
                    {
                        var senone = senoneSequence.Senones[i];
                        compositeSenoneSet.Add(senone);
                    }
                }
                compositeSenones.Add(CompositeSenone.Create(
                                         compositeSenoneSet, logWeight));
            }

            compositeSenoneSequence = SenoneSequence.Create(compositeSenones);
            _compositeSenoneSequenceCache.Put(unit.ToString(), compositeSenoneSequence);


            this.LogInfo(unit + " consists of " + compositeSenones.Count + " composite senones");

            compositeSenoneSequence.Dump("am");

            return(compositeSenoneSequence);
        }
Exemple #21
0
 /// <summary>
 /// Given a unit, returns the HMM that best matches the given unit. If exactMatch is false and an exact match is not
 /// found, then different word positions are used. If any of the contexts are non-silence filler units. a silence
 /// filler unit is tried instead.
 /// </summary>
 /// <param name="unit">The unit of interest.</param>
 /// <param name="position">The position of the unit of interest.</param>
 /// <param name="exactMatch">if true, only an exact match is acceptable..</param>
 /// <returns>The HMM that best matches, or null if no match could be found.</returns>
 public abstract IHMM LookupNearestHMM(Unit unit, HMMPosition position, Boolean exactMatch);
Exemple #22
0
 /**
  * /// Given a unit id and a position, return the HMM associated with the
  * /// unit/position.
  *
  * /// @param unitID   the id of the unit
  * /// @param position the position within the word
  * /// @return the hmm associated with the unit/position
  */
 public IHMM GetHMM(int unitId, HMMPosition position)
 {
     return(_hmmTable[position][unitId]);
 }
Exemple #23
0
        private void loadPhoneList(PropertySheet propertySheet, bool flag, InputStream inputStream, string text)
        {
            int num  = 0;
            int num2 = 1;
            int num3 = 1;
            ExtendedStreamTokenizer extendedStreamTokenizer = new ExtendedStreamTokenizer(inputStream, 35, false);

            this.meansPool    = new Pool("means");
            this.variancePool = new Pool("variances");
            this.matrixPool   = new Pool("transitionmatrices");
            this.senonePool   = new Pool("senones");
            float @float = propertySheet.getFloat("MixtureComponentScoreFloor");
            float float2 = propertySheet.getFloat("mixtureWeightFloor");
            float num4   = 0f;
            float float3 = propertySheet.getFloat("varianceFloor");

            this.logger.info("Loading phone list file from: ");
            this.logger.info(text);
            string expecting = "0.1";

            extendedStreamTokenizer.expectString("version");
            extendedStreamTokenizer.expectString(expecting);
            extendedStreamTokenizer.expectString("same_sized_models");
            int num5 = String.instancehelper_equals(extendedStreamTokenizer.getString(), "yes") ? 1 : 0;

            if (num5 != 0)
            {
                extendedStreamTokenizer.expectString("n_state");
                num = extendedStreamTokenizer.getInt("numBase");
            }
            extendedStreamTokenizer.expectString("tmat_skip");
            int num6 = String.instancehelper_equals(extendedStreamTokenizer.getString(), "yes") ? 1 : 0;
            int num7 = 0;
            int num8 = 0;

            for (;;)
            {
                string @string = extendedStreamTokenizer.getString();
                if (extendedStreamTokenizer.isEOF())
                {
                    break;
                }
                int num9 = num;
                if (num5 == 0)
                {
                    num9 = extendedStreamTokenizer.getInt("ModelSize");
                }
                this.phoneList.put(@string, Integer.valueOf(num9));
                this.logger.fine(new StringBuilder().append("Phone: ").append(@string).append(" size: ").append(num9).toString());
                int[]  array = new int[num9];
                string rep   = "-";
                int    i     = 0;
                while (i < num9)
                {
                    array[i] = num7;
                    i++;
                    num7++;
                }
                Unit unit = this.unitManager.getUnit(@string, String.instancehelper_equals(@string, "SIL"));
                this.contextIndependentUnits.put(unit.getName(), unit);
                if (this.logger.isLoggable(Level.FINE))
                {
                    this.logger.fine(new StringBuilder().append("Loaded ").append(unit).append(" with ").append(num9).append(" states").toString());
                }
                this.addModelToDensityPool(this.meansPool, array, num2, num3);
                this.addModelToDensityPool(this.variancePool, array, num2, num3);
                this.addModelToTransitionMatrixPool(this.matrixPool, num8, array.Length, num4, num6 != 0);
                this.addModelToSenonePool(this.senonePool, array, @float, float3);
                float[][]      transitionMatrix = (float[][])this.matrixPool.get(num8);
                SenoneSequence senoneSequence   = this.getSenoneSequence(array);
                SenoneHMM      hmm = new SenoneHMM(unit, senoneSequence, transitionMatrix, HMMPosition.lookup(rep));
                this.hmmManager.put(hmm);
                num8++;
            }
            this.mixtureWeights = this.initMixtureWeights(num7, num2, num3, float2);
            extendedStreamTokenizer.close();
        }
Exemple #24
0
 /**
  * /// Creates a UnitState with the given unit and HMM position.
  *
  * /// @param unit     the unit associated with this state
  * /// @param position the HMM position of this unit
  */
 public UnitState(Unit unit, HMMPosition position)
 {
     this.Unit      = unit;
     this._position = position;
 }
Exemple #25
0
 public static bool IsWordEnd(this HMMPosition source)
 {
     return(source == HMMPosition.Single || source == HMMPosition.End);
 }
 /**
  * /// 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.HMMManager.Get(position, unit));
 }
Exemple #27
0
 public static string ToString(this HMMPosition source)
 {
     return(((char)source).ToString(CultureInfo.InvariantCulture));
 }
Exemple #28
0
        protected internal virtual void loadHMMPool(bool useCDUnits, InputStream inputStream)
        {
            ExtendedStreamTokenizer extendedStreamTokenizer = new ExtendedStreamTokenizer(inputStream, 35, false);

            this.logger.fine(new StringBuilder().append("Loading HMM file from: ").append(this.location).toString());
            extendedStreamTokenizer.expectString("0.3");
            int @int = extendedStreamTokenizer.getInt("numBase");

            extendedStreamTokenizer.expectString("n_base");
            int int2 = extendedStreamTokenizer.getInt("numTri");

            extendedStreamTokenizer.expectString("n_tri");
            int int3 = extendedStreamTokenizer.getInt("numStateMap");

            extendedStreamTokenizer.expectString("n_state_map");
            int int4 = extendedStreamTokenizer.getInt("numTiedState");

            extendedStreamTokenizer.expectString("n_tied_state");
            int int5 = extendedStreamTokenizer.getInt("numContextIndependentTiedState");

            extendedStreamTokenizer.expectString("n_tied_ci_state");
            int int6 = extendedStreamTokenizer.getInt("numTiedTransitionMatrices");

            extendedStreamTokenizer.expectString("n_tied_tmat");
            int num  = int3;
            int num2 = int2 + @int;
            int num3 = (num2 != -1) ? (num / num2) : (-num);

            if (!Sphinx3Loader.assertionsDisabled && int4 != this.mixtureWeights.getStatesNum())
            {
                throw new AssertionError();
            }
            if (!Sphinx3Loader.assertionsDisabled && int6 != this.transitionsPool.size())
            {
                throw new AssertionError();
            }
            for (int i = 0; i < @int; i++)
            {
                string @string = extendedStreamTokenizer.getString();
                string string2 = extendedStreamTokenizer.getString();
                string string3 = extendedStreamTokenizer.getString();
                string string4 = extendedStreamTokenizer.getString();
                string string5 = extendedStreamTokenizer.getString();
                int    int7    = extendedStreamTokenizer.getInt("tmat");
                int[]  array   = new int[num3 - 1];
                for (int j = 0; j < num3 - 1; j++)
                {
                    array[j] = extendedStreamTokenizer.getInt("j");
                    if (!Sphinx3Loader.assertionsDisabled && (array[j] < 0 || array[j] >= int5))
                    {
                        throw new AssertionError();
                    }
                }
                extendedStreamTokenizer.expectString("N");
                if (!Sphinx3Loader.assertionsDisabled && !String.instancehelper_equals(string2, "-"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && !String.instancehelper_equals(string3, "-"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && !String.instancehelper_equals(string4, "-"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && int7 >= int6)
                {
                    throw new AssertionError();
                }
                Unit unit = this.unitManager.getUnit(@string, String.instancehelper_equals(string5, "filler"));
                this.contextIndependentUnits.put(unit.getName(), unit);
                if (this.logger.isLoggable(Level.FINE))
                {
                    this.logger.fine(new StringBuilder().append("Loaded ").append(unit).toString());
                }
                if (unit.isFiller() && String.instancehelper_equals(unit.getName(), "SIL"))
                {
                    unit = UnitManager.__SILENCE;
                }
                float[][]      transitionMatrix = (float[][])this.transitionsPool.get(int7);
                SenoneSequence senoneSequence   = this.getSenoneSequence(array);
                SenoneHMM      hmm = new SenoneHMM(unit, senoneSequence, transitionMatrix, HMMPosition.lookup(string4));
                this.hmmManager.put(hmm);
            }
            if (this.hmmManager.get(HMMPosition.__UNDEFINED, UnitManager.__SILENCE) == null)
            {
                string text = "Could not find SIL unit in acoustic model";

                throw new IOException(text);
            }
            string text2 = "";
            Unit   unit2 = null;

            int[]          ssid            = null;
            SenoneSequence senoneSequence2 = null;

            for (int k = 0; k < int2; k++)
            {
                string string5 = extendedStreamTokenizer.getString();
                string string6 = extendedStreamTokenizer.getString();
                string string7 = extendedStreamTokenizer.getString();
                string string8 = extendedStreamTokenizer.getString();
                string string9 = extendedStreamTokenizer.getString();
                int    int8    = extendedStreamTokenizer.getInt("tmat");
                int[]  array2  = new int[num3 - 1];
                for (int l = 0; l < num3 - 1; l++)
                {
                    array2[l] = extendedStreamTokenizer.getInt("j");
                    if (!Sphinx3Loader.assertionsDisabled && (array2[l] < int5 || array2[l] >= int4))
                    {
                        throw new AssertionError();
                    }
                }
                extendedStreamTokenizer.expectString("N");
                if (!Sphinx3Loader.assertionsDisabled && String.instancehelper_equals(string6, "-"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && String.instancehelper_equals(string7, "-"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && String.instancehelper_equals(string8, "-"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && !String.instancehelper_equals(string9, "n/a"))
                {
                    throw new AssertionError();
                }
                if (!Sphinx3Loader.assertionsDisabled && int8 >= int6)
                {
                    throw new AssertionError();
                }
                if (useCDUnits)
                {
                    string text3 = new StringBuilder().append(string5).append(' ').append(string6).append(' ').append(string7).toString();
                    Unit   unit3;
                    if (String.instancehelper_equals(text3, text2))
                    {
                        unit3 = unit2;
                    }
                    else
                    {
                        LeftRightContext context = LeftRightContext.get(new Unit[]
                        {
                            (Unit)this.contextIndependentUnits.get(string6)
                        }, new Unit[]
                        {
                            (Unit)this.contextIndependentUnits.get(string7)
                        });
                        unit3 = this.unitManager.getUnit(string5, false, context);
                    }
                    text2 = text3;
                    unit2 = unit3;
                    if (this.logger.isLoggable(Level.FINE))
                    {
                        this.logger.fine(new StringBuilder().append("Loaded ").append(unit3).toString());
                    }
                    float[][]      transitionMatrix2 = (float[][])this.transitionsPool.get(int8);
                    SenoneSequence senoneSequence3   = senoneSequence2;
                    if (senoneSequence3 == null || !this.sameSenoneSequence(array2, ssid))
                    {
                        senoneSequence3 = this.getSenoneSequence(array2);
                    }
                    senoneSequence2 = senoneSequence3;
                    ssid            = array2;
                    SenoneHMM hmm2 = new SenoneHMM(unit3, senoneSequence3, transitionMatrix2, HMMPosition.lookup(string8));
                    this.hmmManager.put(hmm2);
                }
            }
            extendedStreamTokenizer.close();
        }
 public virtual HMM get(HMMPosition position, Unit unit)
 {
     return((HMM)((Map)this.hmmsPerPosition.get(position)).get(unit));
 }
        /**
         * /// Given a unit, returns the HMM that best matches the given unit. If exactMatch is false and an exact match is not
         * /// found, then different word positions are used. If any of the contexts are non-silence filler units. a silence
         * /// filler unit is tried instead.
         *
         * /// @param unit       the unit of interest
         * /// @param position   the position of the unit of interest
         * /// @param exactMatch if true, only an exact match is acceptable.
         * /// @return the HMM that best matches, or null if no match could be found.
         */
        public override IHMM LookupNearestHMM(Unit unit, HMMPosition position, Boolean exactMatch)
        {
            if (exactMatch)
            {
                return(lookupHMM(unit, position));
            }

            var mgr = Loader.HMMManager;
            var hmm = mgr.Get(position, unit);

            if (hmm != null)
            {
                return(hmm);
            }
            // no match, try a composite

            if (_useComposites && hmm == null)
            {
                if (IsComposite(unit))
                {
                    hmm = GetCompositeHMM(unit, position);
                    if (hmm != null)
                    {
                        mgr.Put(hmm);
                    }
                }
            }
            // no match, try at other positions
            if (hmm == null)
            {
                hmm = GetHMMAtAnyPosition(unit);
            }
            // still no match, try different filler
            if (hmm == null)
            {
                hmm = GetHMMInSilenceContext(unit, position);
            }

            // still no match, backoff to base phone
            if (hmm == null)
            {
                var ciUnit = lookupUnit(unit.Name);

                Debug.Assert(unit.IsContextDependent());
                if (ciUnit == null)
                {
                    this.LogInfo("Can't find HMM for " + unit.Name);
                }
                Debug.Assert(ciUnit != null);
                Debug.Assert(!ciUnit.IsContextDependent());

                hmm = mgr.Get(HMMPosition.Undefined, ciUnit);
            }

            Debug.Assert(hmm != null);

            // System.out.println("PROX match for "
            //  + unit + " at " + position + ":" + hmm);

            return(hmm);
        }