Esempio n. 1
0
        public static void printTransitionMatrix(Dictionary <StateTransition, double> model)
        {
            StringBuilder output = new StringBuilder();

            WordType[]      types = (WordType[])Enum.GetValues(typeof(WordType));
            StateTransition trans = new StateTransition(WordType.Undefined, WordType.Undefined);

            //Header
            output.Append("...,");
            for (int n = 0; n < types.Length; n++)
            {
                output.Append(types[n].ToString() + ",");
            }
            output.AppendLine();



            for (int row = 0; row < types.Length; row++)
            {
                trans.from = types[row];
                output.Append(trans.from.ToString() + ",");

                for (int col = 0; col < types.Length; col++)
                {
                    trans.to = types[col];
                    if (!model.ContainsKey(trans))
                    {
                        output.Append("NA" + ",");
                    }
                    else
                    {
                        output.Append(model[trans].ToString() + ",");
                    }
                }

                output.AppendLine();
            }

            System.IO.File.WriteAllText("transition.csv", output.ToString());
        }
Esempio n. 2
0
        static Dictionary <StateTransition, double> createTransitionModel(List <Word> words)
        {
            Dictionary <StateTransition, int>    counter     = new Dictionary <StateTransition, int>();    //counts instances of the state transition
            Dictionary <WordType, int>           fromCounter = new Dictionary <WordType, int>();           //counts times we see the WordType from (needed for percentage)
            Dictionary <StateTransition, double> model       = new Dictionary <StateTransition, double>(); //Holds answer

            for (int i = 0; i < words.Count - 1; i++)
            {
                if (fromCounter.ContainsKey(words[i].PartOfSpeech))
                {
                    fromCounter[words[i].PartOfSpeech]++;
                }
                else
                {
                    fromCounter.Add(words[i].PartOfSpeech, 1);
                }
                StateTransition transition = new StateTransition(words[i].PartOfSpeech, words[i + 1].PartOfSpeech);
                if (counter.ContainsKey(transition))
                {
                    counter[transition]++;
                }
                else
                {
                    counter.Add(transition, 1);
                }
            }

            foreach (KeyValuePair <StateTransition, int> pair in counter)
            {
                model.Add(pair.Key, (double)pair.Value / (double)fromCounter[(WordType)pair.Key.from]);
            }



            return(model);
        }