Beispiel #1
0
        /**
         * /// Replaces an old token with a new token
         *
         * /// @param oldToken the token to replace (or null in which case, replace works like add).
         * /// @param newToken the new token to be placed in the list.
         */
        //public override void replace(Token oldToken, Token newToken)
        //{
        //    ActiveList activeList = findListFor(oldToken);
        //    Debug.Assert(activeList != null);
        //    activeList.replace(oldToken, newToken);
        //}


        /**
         * /// Returns the emitting ActiveList from the manager
         *
         * /// @return the emitting ActiveList
         */
        public override ActiveList GetEmittingList()
        {
            ActiveList list = CurrentActiveLists[CurrentActiveLists.Length - 1];

            this.LogDebug("Returning a list of size :{0}", list.Size);
            return(list);
        }
Beispiel #2
0
        /**
         * /// Performs the recognition for the given number of frames.
         *
         * /// @param nFrames the number of frames to recognize
         * /// @return the current result or null if there is no Result (due to the lack of frames to recognize)
         */
        public override Result Recognize(int nFrames)
        {
            bool   done   = false;
            Result result = null;

            StreamEnd = false;

            for (int i = 0; i < nFrames && !done; i++)
            {
                done = Recognize();
            }

            // generate a new temporary result if the current token is based on a final search state
            // remark: the first check for not null is necessary in cases that the search space does not contain scoreable tokens.
            if (ActiveList.GetBestToken() != null)
            {
                // to make the current result as correct as possible we undo the last search graph expansion here
                ActiveList fixedList = UndoLastGrowStep();

                // Now create the result using the fixed active-list.
                if (!StreamEnd)
                {
                    result = new Result(fixedList, ResultList, CurrentFrameNumber, done, false);
                }
            }

            if (_showTokenCount)
            {
                ShowTokenCount();
            }

            return(result);
        }
        /// <summary>
        /// Keeps track of and reports all of the active word histories for the given active list.
        /// </summary>
        /// <param name="activeList">The active list to track.</param>
        private void MonitorWords(ActiveList activeList)
        {
            //        WordTracker tracker1 = new WordTracker(currentFrameNumber);
            //
            //        for (Token t : activeList) {
            //            tracker1.add(t);
            //        }
            //        tracker1.dump();
            //
            //        TokenTracker tracker2 = new TokenTracker();
            //
            //        for (Token t : activeList) {
            //            tracker2.add(t);
            //        }
            //        tracker2.dumpSummary();
            //        tracker2.dumpDetails();
            //
            //        TokenTypeTracker tracker3 = new TokenTypeTracker();
            //
            //        for (Token t : activeList) {
            //            tracker3.add(t);
            //        }
            //        tracker3.dump();

            //        StateHistoryTracker tracker4 = new StateHistoryTracker(currentFrameNumber);

            //        for (Token t : activeList) {
            //            tracker4.add(t);
            //        }
            //        tracker4.dump();
        }
        /// <summary>
        /// Calculate the acoustic scores for the active list. The active list should contain only emitting tokens.
        /// </summary>
        /// <returns><code>true</code> if there are more frames to score, otherwise, false</returns>
        protected bool ScoreTokens()
        {
            ScoreTimer.Start();
            var data = Scorer.CalculateScores(ActiveList.GetTokens());

            this.LogDebug("Scored Data: {0}", data);
            ScoreTimer.Stop();

            Token bestToken = null;

            if (data is Token)
            {
                bestToken = (Token)data;
            }
            else if (data == null)
            {
                StreamEnd = true;
            }

            var moreTokens = (bestToken != null);

            ActiveList.SetBestToken(bestToken);

            //monitorWords(activeList);
            MonitorStates(ActiveList);

            this.LogDebug("bestToken: {0}", bestToken);

            CurTokensScored.Value   += ActiveList.Size;
            TotalTokensScored.Value += ActiveList.Size;

            return(moreTokens);
        }
        /// <summary>
        /// Goes through the active list of tokens and expands each token,
        /// finding the set of successor tokens until all the successor tokens are emitting tokens.
        /// </summary>
        protected void GrowBranches()
        {
            GrowTimer.Start();
            var relativeBeamThreshold = ActiveList.GetBeamThreshold();

            //this.LogInfo("Frame: " + currentFrameNumber
            //            + " thresh : " + relativeBeamThreshold + " bs "
            //            + activeList.getBestScore() + " tok "
            //            + activeList.getBestToken());
            this.LogDebug("RelativeBeamThreshold: {0}", relativeBeamThreshold.ToString("R"));
            var tokenList = ActiveList;

            foreach (var token in tokenList)
            {
                if (token == null)
                {
                    break;
                }
                if (token.Score >= relativeBeamThreshold && AllowExpansion(token))
                {
                    CollectSuccessorTokens(token);
                }
            }


            //this.LogDebug(string.Format("ActiveList:{0} ",activeList.Count()));
            GrowTimer.Stop();
        }
        /// <summary>
        /// Gets the initial grammar node from the linguist and creates a GrammarNodeToken.
        /// </summary>
        protected virtual void LocalStart()
        {
            var searchGraph = Linguist.SearchGraph;

            CurrentFrameNumber    = 0;
            CurTokensScored.Value = 0;
            _numStateOrder        = searchGraph.NumStateOrder;
            _activeListManager.SetNumStateOrder(_numStateOrder);
            if (BuildWordLattice)
            {
                LoserManager = new AlternateHypothesisManager(_maxLatticeEdges);
            }

            var state = searchGraph.InitialState;

            ActiveList = _activeListManager.GetEmittingList();
            ActiveList.Add(new Token(state, CurrentFrameNumber));

            ClearCollectors();

            GrowBranches();
            GrowNonEmittingBranches();
            // tokenTracker.setEnabled(false);
            // tokenTracker.startUtterance();
        }
Beispiel #7
0
        /// <summary>
        /// Removes unpromising branches from the active list.
        /// </summary>
        protected void PruneBranches()
        {
            int startSize = ActiveList.Size;

            _pruneTimer.Start();
            ActiveList         = _pruner.Prune(ActiveList);
            _beamPruned.Value += startSize - ActiveList.Size;
            _pruneTimer.Stop();
        }
        /// <summary>
        /// Keeps track of and reports statistics about the number of active states.
        /// </summary>
        /// <param name="activeList">The active list of states.</param>
        protected void MonitorStates(ActiveList activeList)
        {
            _tokenSum += activeList.Size;
            _tokenCount++;

            if ((_tokenCount % 1000) == 0)
            {
                this.LogInfo("Average Tokens/State: " + (_tokenSum / _tokenCount));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Gets the initial grammar node from the linguist and creates a GrammarNodeToken.
        /// </summary>
        protected void LocalStart()
        {
            CurrentFrameNumber     = 0;
            _curTokensScored.Value = 0;
            ActiveList   newActiveList = ActiveListFactory.NewInstance();
            ISearchState state         = Linguist.SearchGraph.InitialState;

            newActiveList.Add(new Token(state, CurrentFrameNumber));
            ActiveList = newActiveList;

            GrowBranches();
        }
Beispiel #10
0
        /**
         * /// Adds the given token to the list
         *
         * /// @param token the token to add
         */
        public override void Add(Token token)
        {
            ActiveList activeList = FindListFor(token);

            if (activeList == null)
            {
                throw new Exception("Cannot find ActiveList for "
                                    + token.SearchState.GetType().Name);
            }
            //this.LogDebug("Token '{0}' to activeList of Size {1}", token, activeList.size());
            activeList.Add(token);
        }
Beispiel #11
0
        /// <summary>
        /// Gets the initial grammar node from the linguist and creates a GrammarNodeToken
        /// </summary>
        protected override void LocalStart()
        {
            _currentFastMatchFrameNumber = 0;
            if (_loader is Sphinx3Loader && ((Sphinx3Loader)_loader).HasTiedMixtures())
            {
                ((Sphinx3Loader)_loader).ClearGauScores();
            }
            // prepare fast match active list
            FastmatchActiveList = _fastmatchActiveListFactory.NewInstance();
            var fmInitState = _fastmatchLinguist.SearchGraph.InitialState;

            FastmatchActiveList.Add(new Token(fmInitState, _currentFastMatchFrameNumber));
            CreateFastMatchBestTokenMap();
            GrowFastmatchBranches();
            _fastmatchStreamEnd = false;
            for (var i = 0; (i < _lookaheadWindow - 1) && !_fastmatchStreamEnd; i++)
            {
                FastMatchRecognize();
            }
            base.LocalStart();
        }
Beispiel #12
0
        /// <summary>
        /// Goes through the fast match active list of tokens and expands each token,
        /// finding the set of successor tokens until all the successor tokens are emitting tokens.
        /// </summary>
        protected void GrowFastmatchBranches()
        {
            GrowTimer.Start();
            var oldActiveList = FastmatchActiveList;

            FastmatchActiveList = _fastmatchActiveListFactory.NewInstance();
            var fastmathThreshold = oldActiveList.GetBeamThreshold();
            // TODO more precise range of baseIds, remove magic number
            var frameCiScores = new float[100];

            Arrays.Fill(frameCiScores, -Float.MAX_VALUE);
            var frameMaxCiScore = -Float.MAX_VALUE;

            foreach (var token in oldActiveList)
            {
                var tokenScore = token.Score;
                if (tokenScore < fastmathThreshold)
                {
                    continue;
                }

                // filling max ci scores array that will be used in general search
                // token score composing
                if (token.SearchState is PhoneHmmSearchState)
                {
                    var baseId = ((PhoneHmmSearchState)token.SearchState).GetBaseId();
                    if (frameCiScores[baseId] < tokenScore)
                    {
                        frameCiScores[baseId] = tokenScore;
                    }
                    if (frameMaxCiScore < tokenScore)
                    {
                        frameMaxCiScore = tokenScore;
                    }
                }
                CollectFastMatchSuccessorTokens(token);
            }
            _ciScores.Add(new FrameCiScores(frameCiScores, frameMaxCiScore));
            GrowTimer.Stop();
        }
Beispiel #13
0
        /**
         * /// Goes through the active list of tokens and expands each token, finding the set of successor tokens until all the
         * /// successor tokens are emitting tokens.
         */
        protected void GrowBranches()
        {
            int mapSize = ActiveList.Size * 10;

            if (mapSize == 0)
            {
                mapSize = 1;
            }
            GrowTimer.Start();
            BestTokenMap = new HashMap <ISearchState, Token>(mapSize);
            ActiveList oldActiveList = ActiveList;

            ResultList     = new List <Token>();
            ActiveList     = ActiveListFactory.NewInstance();
            _threshold     = oldActiveList.GetBeamThreshold();
            _wordThreshold = oldActiveList.GetBestScore() + _logRelativeWordBeamWidth;

            foreach (Token token in oldActiveList)
            {
                CollectSuccessorTokens(token);
            }
            GrowTimer.Stop();
        }
Beispiel #14
0
        /// <summary>
        /// Collects the next set of emitting tokens from a token and accumulates them in the active or result lists
        /// </summary>
        /// <param name="token">The token to collect successors from be immediately expanded are placed. Null if we should always expand all nodes.</param>
        protected override void CollectSuccessorTokens(Token token)
        {
            // tokenTracker.add(token);
            // tokenTypeTracker.add(token);

            // If this is a final state, add it to the final list

            if (token.IsFinal)
            {
                ResultList.Add(GetResultListPredecessor(token));
                return;
            }

            // if this is a non-emitting token and we've already
            // visited the same state during this frame, then we
            // are in a grammar loop, so we don't continue to expand.
            // This check only works properly if we have kept all of the
            // tokens (instead of skipping the non-word tokens).
            // Note that certain linguists will never generate grammar loops
            // (lextree linguist for example). For these cases, it is perfectly
            // fine to disable this check by setting keepAllTokens to false

            if (!token.IsEmitting && (KeepAllTokens && IsVisited(token)))
            {
                return;
            }

            var state       = token.SearchState;
            var arcs        = state.GetSuccessors();
            var predecessor = GetResultListPredecessor(token);

            // For each successor
            // calculate the entry score for the token based upon the
            // predecessor token score and the transition probabilities
            // if the score is better than the best score encountered for
            // the SearchState and frame then create a new token, add
            // it to the lattice and the SearchState.
            // If the token is an emitting token add it to the list,
            // otherwise recursively collect the new tokens successors.

            var tokenScore             = token.Score;
            var beamThreshold          = ActiveList.GetBeamThreshold();
            var stateProducesPhoneHmms = state is LexTreeNonEmittingHMMState || state is LexTreeWordState ||
                                         state is LexTreeEndUnitState;

            foreach (var arc in arcs)
            {
                var nextState = arc.State;

                // prune states using lookahead heuristics
                if (stateProducesPhoneHmms)
                {
                    if (nextState is LexTreeHmmState)
                    {
                        Float penalty;
                        var   baseId = ((LexTreeHmmState)nextState).HmmState.HMM.BaseUnit.BaseID;
                        if ((penalty = _penalties.Get(baseId)) == null)
                        {
                            penalty = UpdateLookaheadPenalty(baseId);
                        }
                        if ((tokenScore + _lookaheadWeight * penalty) < beamThreshold)
                        {
                            continue;
                        }
                    }
                }

                if (_checkStateOrder)
                {
                    CheckStateOrder(state, nextState);
                }

                // We're actually multiplying the variables, but since
                // these come in log(), multiply gets converted to add
                var logEntryScore = tokenScore + arc.GetProbability();

                var bestToken = GetBestToken(nextState);

                if (bestToken == null)
                {
                    var newBestToken = new Token(predecessor, nextState, logEntryScore, arc.InsertionProbability, arc.LanguageProbability, CurrentFrameNumber);
                    TokensCreated.Value++;
                    SetBestToken(newBestToken, nextState);
                    ActiveListAdd(newBestToken);
                }
                else if (bestToken.Score < logEntryScore)
                {
                    // System.out.println("Updating " + bestToken + " with " +
                    // newBestToken);
                    var oldPredecessor = bestToken.Predecessor;
                    bestToken.Update(predecessor, nextState, logEntryScore, arc.InsertionProbability, arc.LanguageProbability, CurrentFrameNumber);
                    if (BuildWordLattice && nextState is IWordSearchState)
                    {
                        LoserManager.AddAlternatePredecessor(bestToken, oldPredecessor);
                    }
                }
                else if (BuildWordLattice && nextState is IWordSearchState)
                {
                    if (predecessor != null)
                    {
                        LoserManager.AddAlternatePredecessor(bestToken, predecessor);
                    }
                }
            }
        }
Beispiel #15
0
 /**
  * /// Dumps out debugging info for the given active list
  *
  * /// @param al the active list to dump
  */
 private void DumpList(ActiveList al)
 {
     this.LogInfo("Size: " + al.Size + " Best token: " + al.GetBestToken());
 }
Beispiel #16
0
        /**
         * /// Clears emitting list in manager
         */
        public override void ClearEmittingList()
        {
            ActiveList list = CurrentActiveLists[CurrentActiveLists.Length - 1];

            CurrentActiveLists[CurrentActiveLists.Length - 1] = list.NewInstance();
        }
Beispiel #17
0
 /// <summary>
 /// Removes unpromising branches from the fast match active list.
 /// </summary>
 protected void PruneFastMatchBranches()
 {
     PruneTimer.Start();
     FastmatchActiveList = Pruner.Prune(FastmatchActiveList);
     PruneTimer.Stop();
 }