Esempio n. 1
0
        public static double GetWordLikelihood(Model model, Queue <string> evidence, string word)
        {
            List <double> weights     = getWeights();
            List <double> likelihoods = new List <double>();

            nWeights = new List <double>();
            while (evidence.Count >= 0)
            {
                Gram   g     = model.getGramFromChain(evidence);
                double temp  = 0;
                int    total = g.getCount();
                if (total > 0)
                {
                    int occurences = g[word].getCount();
                    temp = occurences / (double)total;
                }
                likelihoods.Add(temp);
                nWeights.Add(weights[evidence.Count]);
                if (evidence.Count == 0)
                {
                    break;
                }
                evidence.Dequeue();
            }
            double likelihood = 0;

            for (int i = 0; i < likelihoods.Count; i++)
            {
                likelihood += likelihoods[i] * nWeights[i];
            }
            return(likelihood * 100);
        }
Esempio n. 2
0
        private static Dictionary <string, double> LaplacianSmoothing(Gram predicateState, List <string> dictionary) //this list dictionary may contain words not in the official dictionary such as a misspelling of currentword
        {
            Dictionary <string, double> probabilityDistribution = new Dictionary <string, double>();
            int predicateFrequency = predicateState.getCount();

            predicateFrequency += dictionary.Count;     // the plus one smoothing
            foreach (string word in dictionary)
            {
                int count = 1;                          // the plus one smoothing
                count += predicateState.NextWordCount(word);
                probabilityDistribution.Add(word, count / (double)predicateFrequency);
            }
            return(probabilityDistribution);
        }