Beispiel #1
0
        /** Gets the initial grammar node from the linguist and creates a GrammarNodeToken */
        protected void localStart()
        {
            ISearchGraph searchGraph = linguist.getSearchGraph();

            currentFrameNumber    = 0;
            curTokensScored.value = 0;
            numStateOrder         = searchGraph.getNumStateOrder();
            activeListManager.setNumStateOrder(numStateOrder);
            if (buildWordLattice)
            {
                loserManager = new AlternateHypothesisManager(maxLatticeEdges);
            }

            ISearchState state = searchGraph.getInitialState();

            activeList = activeListManager.getEmittingList();
            activeList.add(new Token(state, currentFrameNumber));

            clearCollectors();

            growBranches();
            growNonEmittingBranches();
            // tokenTracker.setEnabled(false);
            // tokenTracker.startUtterance();
        }
        public ISearchState Solve(ISearchState puzzle)
        {
            var frontier = Sets.All.Where(p => puzzle[p].HasValue).ToList();

            while (frontier.Any())
            {
                var newFrontier = new List <(int x, int y)>();

                foreach (var p1 in frontier)
                {
                    var val = puzzle[p1].Value;
                    foreach (var p2 in Sets.ContainingSets(p1).SelectMany(x => x).Distinct().Where(x => x != p1))
                    {
                        if (!puzzle[p2].HasValue)
                        {
                            puzzle.SetNot(p2.x, p2.y, val);
                            if (puzzle[p2].HasValue)
                            {
                                newFrontier.Add(p2);
                            }
                        }
                    }
                }

                frontier = newFrontier;
            }

            return(puzzle);
        }
Beispiel #3
0
 /// <summary>
 /// Adds item according to search type logic
 /// </summary>
 /// <param name="item"></param>
 public void Add(ISearchState item)
 {
     if (SearchType == SearchType.BFS)
     {
         Queue.Enqueue(item);
     }
     else if (SearchType == SearchType.DFS)
     {
         Stack.Push(item);
     }
     else if (SearchType == SearchType.AStar)
     {
         List.Add(item);
     }
     else
     {
         if (List.Quantity == 0)
         {
             List.Add(item);
         }
         // Substitutes item if evaluation is better
         else if (item.Evaluation < List[0].Evaluation)
         {
             List.Remove(0);
             List.Add(item);
         }
     }
 }
Beispiel #4
0
 public void Update(Token predecessor, ISearchState nextState, float logEntryScore, float insertionProbability, float languageProbability, int currentFrameNumber)
 {
     Predecessor    = predecessor;
     SearchState    = nextState;
     Score          = logEntryScore;
     InsertionScore = insertionProbability;
     LanguageScore  = languageProbability;
     FrameNumber    = currentFrameNumber;
 }
Beispiel #5
0
 public Solver(ISearchState start)
 {
     _start     = start;
     _variables = Enumerable.Repeat((VariableInteger)null, 9 * 9).ToList();
     SetVariables();
     _constraints = new List <IConstraint>();
     AddAllDiffConstraints();
     AddState();
 }
Beispiel #6
0
 /// <summary>
 /// Internal constructor for a token. Used by classes Token, CombineToken, ParallelToken
 /// </summary>
 /// <param name="predecessor">the predecessor for this token</param>
 /// <param name="state">the SentenceHMMState associated with this token</param>
 /// <param name="logTotalScore">the total entry score for this token (in LogMath log base)</param>
 /// <param name="logInsertionScore"></param>
 /// <param name="logLanguageScore">the language score associated with this token (in LogMath log base)</param>
 /// <param name="frameNumber">the frame number associated with this token</param>
 public Token(Token predecessor, ISearchState state, float logTotalScore, float logInsertionScore, float logLanguageScore, int frameNumber)
 {
     Predecessor    = predecessor;
     SearchState    = state;
     Score          = logTotalScore;
     InsertionScore = logInsertionScore;
     LanguageScore  = logLanguageScore;
     FrameNumber    = frameNumber;
     //this.location = -1;
     _curCount++;
 }
Beispiel #7
0
        //public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        //{
        //    throw new NotImplementedException();
        //}



        public void update(Token predecessor, ISearchState nextState,
                           float logEntryScore, float insertionProbability,
                           float languageProbability, int currentFrameNumber)
        {
            this.predecessor       = predecessor;
            this.searchState       = nextState;
            this.logTotalScore     = logEntryScore;
            this.logInsertionScore = insertionProbability;
            this.logLanguageScore  = languageProbability;
            this.frameNumber       = currentFrameNumber;
        }
Beispiel #8
0
        /**
         * /// Gets the best token for this state
         *
         * /// @param state the state of interest
         * /// @return the best token
         */
        protected Token GetBestToken(ISearchState state)
        {
            Token best = null;

            if (BestTokenMap.ContainsKey(state))
            {
                best = BestTokenMap[state];
                //this.LogInfo("BT " + best + " for state " + state);
            }

            return(best);
        }
Beispiel #9
0
        /**
         * /// Gets the best token for this state
         *
         * /// @param state the state of interest
         * /// @return the best token
         */
        protected Token getBestToken(ISearchState state)
        {
            Token best = null;

            if (bestTokenMap.ContainsKey(state))
            {
                best = bestTokenMap[state];
                Trace.WriteLine("BT " + best + " for state " + state);
            }

            return(best);
        }
Beispiel #10
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.getSearchGraph().getInitialState();

            newActiveList.add(new Token(state, currentFrameNumber));
            activeList = newActiveList;

            growBranches();
        }
Beispiel #11
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 #12
0
        public SearchAlgorithm(ISearchState initialState, ISearchState desiredState, SearchType searchType)
        {
            // Intializes search structures given search type
            SearchCollection = new SearchCollection(searchType);
            SearchedStates   = new SList <ISearchState>();
            SearchType       = searchType;

            // Initialize search states definitions
            InitialState = initialState;
            DesiredState = desiredState;

            // The initial state is the first one to be searched
            SearchCollection.Add(InitialState);
        }
Beispiel #13
0
 /// <summary>
 /// Returns true if this item exists in the collection.
 /// </summary>
 /// <param name="item">Item to be verified.</param>
 /// <returns>True if item exists and false if not</returns>
 public bool Exists(ISearchState item)
 {
     if (SearchType == SearchType.BFS)
     {
         return(Queue.Exists(item));
     }
     else if (SearchType == SearchType.DFS)
     {
         return(Stack.Exists(item));
     }
     else
     {
         return(List.Exists(item));
     }
 }
Beispiel #14
0
        /**
         * /// Determines whether or not we've visited the state associated with this token since the previous frame.
         *
         * /// @param t the token to check
         * /// @return true if we've visited the search state since the last frame
         */
        private static Boolean IsVisited(Token t)
        {
            ISearchState curState = t.SearchState;

            t = t.Predecessor;

            while (t != null && !t.IsEmitting)
            {
                if (curState.Equals(t.SearchState))
                {
                    return(true);
                }
                t = t.Predecessor;
            }
            return(false);
        }
Beispiel #15
0
        /**
         * /// Determines whether or not we've visited the state associated with this token since the previous frame.
         *
         * /// @param t the token to check
         * /// @return true if we've visited the search state since the last frame
         */
        private Boolean isVisited(Token t)
        {
            ISearchState curState = t.getSearchState();

            t = t.getPredecessor();

            while (t != null && !t.isEmitting())
            {
                if (curState.Equals(t.getSearchState()))
                {
                    return(true);
                }
                t = t.getPredecessor();
            }
            return(false);
        }
Beispiel #16
0
        /**
         * /// Determines whether or not we've visited the state associated with this token since the previous frame.
         *
         * /// @param t
         * /// @return true if we've visited the search state since the last frame
         */
        private Boolean isVisited(Token t)
        {
            ISearchState curState = t.getSearchState();

            t = t.getPredecessor();

            while (t != null && !t.isEmitting())
            {
                if (curState.Equals(t.getSearchState()))
                {
                    Trace.WriteLine("CS " + curState + " match " + t.getSearchState());
                    return(true);
                }
                t = t.getPredecessor();
            }
            return(false);
        }
        /**
         * /// Returns the state key for the given state. This key is used
         * /// to store bestToken into the bestToken map. All tokens with
         * /// the same key are basically shared. This method adds flexibility in
         * /// search.
         * ///
         * /// For example this key will allow HMM states that have identical word
         * /// histories and are in the same HMM state to be treated equivalently.
         * /// When used  as the best token key, only the best scoring token with a
         * /// given word history survives per HMM.
         * /// <pre>
         * ///   Boolean equal = hmmSearchState.getLexState().equals(
         * ///          other.hmmSearchState.getLexState())
         * ///          && hmmSearchState.getWordHistory().equals(
         * ///          other.hmmSearchState.getWordHistory());
         * /// </pre>
         * ///
         * /// @param state
         * ///            the state to get the key for
         * /// @return the key for the given state
         */



        /** Checks that the given two states are in legitimate order.
         * /// @param fromState
         * /// @param toState*/

        protected void CheckStateOrder(ISearchState fromState, ISearchState toState)
        {
            if (fromState.Order == _numStateOrder - 1)
            {
                return;
            }

            if (fromState.Order > toState.Order)
            {
                throw new Exception("IllegalState order: from "
                                    + fromState.GetType().Name + ' '
                                    + fromState.ToPrettyString()
                                    + " order: " + fromState.Order
                                    + " to "
                                    + toState.GetType().Name + ' '
                                    + toState.ToPrettyString()
                                    + " order: " + toState.Order);
            }
        }
Beispiel #18
0
        /** Checks that the given two states are in legitimate order.
         * /// @param fromState
         * /// @param toState*/
        private void checkStateOrder(ISearchState fromState, ISearchState toState)
        {
            if (fromState.getOrder() == numStateOrder - 1)
            {
                return;
            }

            if (fromState.getOrder() > toState.getOrder())
            {
                throw new Exception("IllegalState order: from "
                                    + fromState.GetType().Name + ' '
                                    + fromState.toPrettyString()
                                    + " order: " + fromState.getOrder()
                                    + " to "
                                    + toState.GetType().Name + ' '
                                    + toState.toPrettyString()
                                    + " order: " + toState.getOrder());
            }
        }
Beispiel #19
0
        public ISearchState Solve(ISearchState puzzle)
        {
            var frontier = Sets.All.Where(p => puzzle[p].HasValue).ToList();

            while (frontier.Any())
            {
                var newFrontier = new List <(int x, int y)>();

                foreach (var p1 in frontier)
                {
                    foreach (var set in Sets.ContainingSets(p1))
                    {
                        foreach (var subSet in BitCounter.PowerSet(set, 1, _n - 1))
                        {
                            var intersect = puzzle.BitDomain(subSet.First());
                            foreach (var i in subSet.Skip(1))
                            {
                                intersect |= puzzle.BitDomain(i);
                            }

                            if (BitCounter.Count(intersect) <= subSet.Count)
                            {
                                foreach (var p2 in set.Where(p => !subSet.Contains(p)))
                                {
                                    if (puzzle.DomainMinus(p2, intersect))
                                    {
                                        newFrontier.Add(p2);
                                    }
                                }
                            }
                        }
                    }
                }

                frontier = newFrontier.Distinct().ToList();
            }

            return(puzzle);
        }
Beispiel #20
0
        /// <summary>
        /// Returns the string of words and units for this token, with embedded silences.
        /// </summary>
        /// <returns>the string of words and units</returns>public IWord getWord()
        public String getWordUnitPath()
        {
            StringBuilder sb    = new StringBuilder();
            Token         token = this;

            while (token != null)
            {
                ISearchState searchState = token.getSearchState();
                if (searchState is IWordSearchState)
                {
                    IWordSearchState wordState = (IWordSearchState)searchState;
                    IWord            word      = wordState.getPronunciation().getWord();
                    sb.Insert(0, ' ' + word.getSpelling());
                }
                else if (searchState is IUnitSearchState)
                {
                    IUnitSearchState unitState = (IUnitSearchState)searchState;
                    IUnit            unit      = unitState.getUnit();
                    sb.Insert(0, ' ' + unit.getName());
                }
                token = token.getPredecessor();
            }
            return(sb.ToString().Trim());
        }
Beispiel #21
0
 protected Token GetFastMatchBestToken(ISearchState state)
 {
     return(FastMatchBestTokenMap.Get(state));
 }
Beispiel #22
0
 public SearchWorker(ISearchState state)
 {
     _state = state;
     _lock  = new object();
 }
Beispiel #23
0
 protected void SetFastMatchBestToken(Token token, ISearchState state)
 {
     FastMatchBestTokenMap.Put(state, token);
 }
Beispiel #24
0
        /**
         * /// Collects the next set of emitting tokens from a token and accumulates them in the active or result lists
         *
         * /// @param token the token to collect successors from be immediately expanded are placed. Null if we should always
         * ///              expand all nodes.
         */
        protected 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;
            }

            ISearchState state = token.getSearchState();

            ISearchStateArc[] arcs        = state.getSuccessors();
            Token             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.

            foreach (ISearchStateArc arc in arcs)
            {
                ISearchState nextState = arc.getState();

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

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

                Token bestToken = getBestToken(nextState);

                //


                if (bestToken == null)
                {
                    Token newBestToken = new Token(predecessor, nextState, logEntryScore, arc.getInsertionProbability(),
                                                   arc.getLanguageProbability(), currentFrameNumber);
                    tokensCreated.value++;
                    setBestToken(newBestToken, nextState);
                    activeListAdd(newBestToken);
                }
                else if (bestToken.getScore() < logEntryScore)
                {
                    // System.out.println("Updating " + bestToken + " with " +
                    // newBestToken);
                    Token oldPredecessor = bestToken.getPredecessor();
                    bestToken.update(predecessor, nextState, logEntryScore, arc.getInsertionProbability(),
                                     arc.getLanguageProbability(), currentFrameNumber);
                    if (buildWordLattice && nextState is IWordSearchState)
                    {
                        loserManager.addAlternatePredecessor(bestToken, oldPredecessor);
                    }
                }
                else if (buildWordLattice && nextState is IWordSearchState)
                {
                    if (predecessor != null)
                    {
                        loserManager.addAlternatePredecessor(bestToken, predecessor);
                    }
                }
            }
        }
Beispiel #25
0
        /**
         * /// Sets the best token for a given state
         *
         * /// @param token the best token
         * /// @param state the state
         */
        protected void setBestToken(Token token, ISearchState state)
        {
            Object key = getStateKey(state);

            bestTokenMap.Add(key, token);
        }
Beispiel #26
0
 /**
  * /// Returns the state key for the given state. This key is used
  * /// to store bestToken into the bestToken map. All tokens with
  * /// the same key are basically shared. This method adds flexibility in
  * /// search.
  * ///
  * /// For example this key will allow HMM states that have identical word
  * /// histories and are in the same HMM state to be treated equivalently.
  * /// When used  as the best token key, only the best scoring token with a
  * /// given word history survives per HMM.
  * /// <pre>
  * ///   Boolean equal = hmmSearchState.getLexState().equals(
  * ///          other.hmmSearchState.getLexState())
  * ///          && hmmSearchState.getWordHistory().equals(
  * ///          other.hmmSearchState.getWordHistory());
  * /// </pre>
  * ///
  * /// @param state
  * ///            the state to get the key for
  * /// @return the key for the given state
  */
 protected Object getStateKey(ISearchState state)
 {
     return(state);
 }
Beispiel #27
0
        /**
         * /// Gets the best token for this state
         *
         * /// @param state the state of interest
         * /// @return the best token
         */
        protected Token getBestToken(ISearchState state)
        {
            Object key = getStateKey(state);

            return(bestTokenMap[key]);
        }
Beispiel #28
0
 public SearchWorker(ISearchState state)
 {
     _state = state;
     _lock = new object();
 }
Beispiel #29
0
 /// <summary>
 /// Creates the initial token with the given word history depth
 /// </summary>
 /// <param name="state">the SearchState associated with this token</param>
 /// <param name="frameNumber">the frame number for this token</param>
 public Token(ISearchState state, int frameNumber)
     : this(null, state, 0.0f, 0.0f, 0.0f, frameNumber)
 {
 }
Beispiel #30
0
        /**
         * /// Collects the next set of emitting tokens from a token and accumulates them in the active or result lists
         *
         * /// @param token the token to collect successors from
         */
        protected void CollectSuccessorTokens(Token token)
        {
            ISearchState state = token.SearchState;

            // If this is a final state, add it to the final list
            if (token.IsFinal)
            {
                ResultList.Add(token);
            }
            if (token.Score < _threshold)
            {
                return;
            }
            if (state is IWordSearchState &&
                token.Score < _wordThreshold)
            {
                return;
            }
            ISearchStateArc[] arcs = state.GetSuccessors();
            //this.LogDebug("Arcs Count: {0}", arcs.Length);
            // 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.
            foreach (ISearchStateArc arc in arcs)
            {
                ISearchState nextState = arc.State;
                // this.LogDebug("Next State: {0}",nextState);
                // We're actually multiplying the variables, but since
                // these come in log(), multiply gets converted to add
                float logEntryScore = token.Score + arc.GetProbability();
                //this.LogDebug("LogEntryScore: {0}", logEntryScore);
                if (_wantEntryPruning)
                { // false by default
                    if (logEntryScore < _threshold)
                    {
                        continue;
                    }
                    if (nextState is IWordSearchState &&
                        logEntryScore < _wordThreshold)
                    {
                        continue;
                    }
                }
                Token predecessor = GetResultListPredecessor(token);
                // this.LogDebug("Predecessor: {0}", predecessor);
                // if not emitting, check to see if we've already visited
                // this state during this frame. Expand the token only if we
                // haven't visited it already. This prevents the search
                // from getting stuck in a loop of states with no
                // intervening emitting nodes. This can happen with nasty
                // jsgf grammars such as ((foo*)*)*
                if (!nextState.IsEmitting)
                {
                    Token newToken = new Token(predecessor, nextState, logEntryScore,
                                               arc.InsertionProbability,
                                               arc.LanguageProbability,
                                               CurrentFrameNumber);
                    TokensCreated.Value++;
                    if (!IsVisited(newToken))
                    {
                        CollectSuccessorTokens(newToken);
                    }
                    continue;
                }

                Token bestToken = GetBestToken(nextState);
                // this.LogDebug("BestToken: {0}", bestToken);
                if (bestToken == null)
                {
                    Token newToken = new Token(predecessor, nextState, logEntryScore,
                                               arc.InsertionProbability,
                                               arc.LanguageProbability,
                                               CurrentFrameNumber);
                    TokensCreated.Value++;
                    SetBestToken(newToken, nextState);
                    //this.LogDebug("Adding Token: {0}", newToken);
                    ActiveList.Add(newToken);
                }
                else
                {
                    if (bestToken.Score <= logEntryScore)
                    {
                        bestToken.Update(predecessor, nextState, logEntryScore,
                                         arc.InsertionProbability,
                                         arc.LanguageProbability,
                                         CurrentFrameNumber);
                        _viterbiPruned.Value++;
                    }
                    else
                    {
                        _viterbiPruned.Value++;
                    }
                }
            }
        }
Beispiel #31
0
 protected Token SetBestToken(Token token, ISearchState state)
 {
     return(BestTokenMap.Put(state, token));
 }