Beispiel #1
0
        /// <summary>
        /// Predict the next item most likey from the given one.
        /// We assume arrays of sequence items has NValue -1 elements in it
        /// </summary>
        /// <param name="sequence">a sequence of NValue-1 items</param>
        /// <returns></returns>
        public string PredictNext(string[] sequence)
        {
            if (sequence.Length != NValue - 1)
            {
                throw new Exception("Bad set of sequence items. The member count is not the same as the NValue-1!");
            }

            string key = GetStringName(sequence);

            if (!data.ContainsKey(key))
            {
                PredictedProbabilty = 0;
                return("Unknown");
            }

            KeyDataRecord keyData = data[key];

            //find the highest probability
            int    highestValue = 0;
            string bestItem     = "Unknown";

            //Get the list of items in the store
            ICollection <string> possibleItems = keyData.counts.Keys;

            //Go through each
            foreach (string ds in possibleItems)
            {
                //Check for the highest value
                if (keyData.counts[ds] > highestValue)
                {
                    //Store the item
                    highestValue = keyData.counts[ds];
                    bestItem     = ds;
                }
            }

            //calculate probability
            if (total > 0)
            {
                PredictedProbabilty = ((double)highestValue / (double)keyData.total) * 100.0;
            }

            return(bestItem);
        }
Beispiel #2
0
        /// <summary>
        /// Register a set of input sequnces with predictor,
        /// updating its data. We assume the sequence has exactly NValue in it
        /// </summary>
        /// <param name="sequence">a sequence of NValue item</param>
        public void RegisterSequence(string[] sequences)
        {
            if (sequences.Length != NValue)
            {
                throw new Exception("Bad set of sequences. The member count is not the same as the N value!");
            }

            total++;

            string[] previousSeq = new string[NValue - 1];
            for (int i = 0; i < NValue - 1; i++)
            {
                previousSeq[i] = sequences[i];
            }

            string currentSeq = sequences[NValue - 1];

            string key = GetStringName(previousSeq);

            if (!data.ContainsKey(key))
            {
                data[key] = new KeyDataRecord();
            }

            KeyDataRecord keyData = data[key];

            if (keyData.counts == null)
            {
                keyData.counts = new Dictionary <string, int>();
            }

            if (!keyData.counts.ContainsKey(currentSeq))
            {
                keyData.counts[currentSeq] = 0;
            }

            keyData.counts[currentSeq] = keyData.counts[currentSeq] + 1;
            keyData.total += 1;

            data[key] = keyData;
        }