示例#1
0
        private Dictionary <MarkovKey, Dictionary <MarkovWord, int> > BuildWordDict(string text, Dictionary <MarkovKey, Dictionary <MarkovWord, int> > wordDict, int wordOrder)
        {
            text = text.Replace("\n", " ");
            text = text.Replace("\"", "");

            string taggedText = Stanford.NLP.CoreNLP.CSharp.TaggerDemo.TagText(text);

            List <string> words = taggedText.Split(null).ToList <string>();

            words = words.Where(x => !string.IsNullOrEmpty(x)).ToList <string>();

            List <MarkovWord> markovWords = new List <MarkovWord>();

            foreach (string word in words)
            {
                markovWords.Add(new MarkovWord(word));
            }

            for (int i = wordOrder; i < markovWords.Count(); i++)
            {
                MarkovKey currentKey = new MarkovKey(markovWords.GetRange(i - wordOrder, wordOrder));
                if (!wordDict.ContainsKey(currentKey))
                {
                    wordDict.Add(currentKey, new Dictionary <MarkovWord, int>());
                }
                if (!wordDict[currentKey].ContainsKey(markovWords[i]))
                {
                    wordDict[currentKey][markovWords[i]] = 0;
                }
                wordDict[currentKey][markovWords[i]] += 1;
            }
            return(wordDict);
        }
示例#2
0
        private void AnalyzeButton_Click(object sender, EventArgs e)
        {
            Dictionary <MarkovKey, Dictionary <MarkovWord, int> > wordDict = new Dictionary <MarkovKey, Dictionary <MarkovWord, int> >(new MarkovKeyEqualityComparer());
            int wordOrder = 2;

            wordDict = BuildWordDict(InputBox.Text, wordOrder);
            wordDict = BuildWordDict(InputBox2.Text, wordDict, wordOrder);

            int           outputLength = 1000;
            StringBuilder chain        = new StringBuilder("");

            System.Random r = new System.Random();
            // string currentWords[] = new string[]{ "In", "a" };
            Queue <MarkovWord> currentWords = new Queue <MarkovWord>();

            for (int i = 0; i < wordOrder; i++)
            {
                currentWords.Enqueue(wordDict.ElementAt(0).Key.Words.ElementAt(i));
            }

            for (int i = 0; i < outputLength; i++)
            {
                MarkovWord dequeuedItem = currentWords.Dequeue();
                chain.Append(dequeuedItem.Text + " ");
                MarkovKey newKey = new MarkovKey(new List <MarkovWord>());
                newKey.Words.Add(dequeuedItem);
                for (int j = 0; j < wordOrder - 1; j++)
                {
                    newKey.Words.Add(currentWords.ElementAt(j));
                }

                currentWords.Enqueue(RetrieveRandomWord(wordDict[newKey], r));
            }

            OutputBox.Text = chain.ToString();

            //Stanford.NLP.CoreNLP.CSharp.TaggerDemo.TagText(InputBox.Text);
        }