Exemple #1
0
 public virtual string[] AllTags()
 {
     string[] tags = new string[mPosModel.OutcomeCount];
     for (int currentTag = 0; currentTag < mPosModel.OutcomeCount; currentTag++)
     {
         tags[currentTag] = mPosModel.GetOutcomeName(currentTag);
     }
     return(tags);
 }
Exemple #2
0
        /// <summary>
        /// Returns the best sequence of outcomes based on model for this object.</summary>
        /// <param name="numSequences">
        /// The maximum number of sequences to be returned.
        /// </param>
        /// <param name="sequence">
        /// The input sequence.
        /// </param>
        /// <param name="additionalContext">
        /// An object[] of additional context.  This is passed to the context generator blindly with the assumption that the context are appropiate.
        /// </param>
        /// <param name="minSequenceScore">
        /// A lower bound on the score of a returned sequence.</param>
        /// <returns>
        /// An array of the top ranked sequences of outcomes.
        /// </returns>
        public virtual Sequence[] BestSequences(int numSequences, object[] sequence, object[] additionalContext, double minSequenceScore)
        {
            int sequenceCount = sequence.Length;
            ListHeap <Sequence> previousHeap = new ListHeap <Sequence>(Size);
            ListHeap <Sequence> nextHeap     = new ListHeap <Sequence>(Size);
            ListHeap <Sequence> tempHeap;

            previousHeap.Add(new Sequence());
            if (additionalContext == null)
            {
                additionalContext = mEmptyAdditionalContext;
            }
            for (int currentSequence = 0; currentSequence < sequenceCount; currentSequence++)
            {
                int sz = System.Math.Min(Size, previousHeap.Size);
                int sc = 0;
                for (; previousHeap.Size > 0 && sc < sz; sc++)
                {
                    Sequence topSequence = previousHeap.Extract();
                    String[] outcomes    = topSequence.Outcomes.ToArray();
                    String[] contexts    = ContextGenerator.GetContext(currentSequence, sequence, outcomes, additionalContext);
                    double[] scores;
                    if (mContextsCache != null)
                    {
                        scores = (double[])mContextsCache[contexts];
                        if (scores == null)
                        {
                            scores = Model.Evaluate(contexts, mProbabilities);
                            mContextsCache[contexts] = scores;
                        }
                    }
                    else
                    {
                        scores = Model.Evaluate(contexts, mProbabilities);
                    }

                    double[] tempScores = new double[scores.Length];
                    Array.Copy(scores, tempScores, scores.Length);

                    Array.Sort(tempScores);
                    double minimum = tempScores[System.Math.Max(0, scores.Length - Size)];

                    for (int currentScore = 0; currentScore < scores.Length; currentScore++)
                    {
                        if (scores[currentScore] < minimum)
                        {
                            continue; //only advance first "size" outcomes
                        }

                        string outcomeName = Model.GetOutcomeName(currentScore);
                        if (ValidSequence(currentSequence, sequence, outcomes, outcomeName))
                        {
                            Sequence newSequence = new Sequence(topSequence, outcomeName, scores[currentScore]);
                            if (newSequence.Score > minSequenceScore)
                            {
                                nextHeap.Add(newSequence);
                            }
                        }
                    }
                    if (nextHeap.Size == 0)
                    {//if no advanced sequences, advance all valid
                        for (int currentScore = 0; currentScore < scores.Length; currentScore++)
                        {
                            string outcomeName = Model.GetOutcomeName(currentScore);
                            if (ValidSequence(currentSequence, sequence, outcomes, outcomeName))
                            {
                                Sequence newSequence = new Sequence(topSequence, outcomeName, scores[currentScore]);
                                if (newSequence.Score > minSequenceScore)
                                {
                                    nextHeap.Add(newSequence);
                                }
                            }
                        }
                    }
                    //nextHeap.Sort();
                }
                //    make prev = next; and re-init next (we reuse existing prev set once we clear it)
                previousHeap.Clear();
                tempHeap     = previousHeap;
                previousHeap = nextHeap;
                nextHeap     = tempHeap;
            }
            int topSequenceCount = System.Math.Min(numSequences, previousHeap.Size);

            Sequence[] topSequences  = new Sequence[topSequenceCount];
            int        sequenceIndex = 0;

            for (; sequenceIndex < topSequenceCount; sequenceIndex++)
            {
                topSequences[sequenceIndex] = (Sequence)previousHeap.Extract();
            }
            return(topSequences);
        }