示例#1
0
        protected List <string> MakeChain(int Length)
        {
            List <string> text = new List <string>(10);

            Structs.RootWord w = (Structs.RootWord)Words[((string)this.StartIndex[this.Random.Next(this.StartIndex.Count)]).ToLower()];
            text.Add(w.Word + " ");

            Structs.Child c = new Structs.Child();
            ArrayList     a = new ArrayList();
            int           pos = 0;
            int           rnd = 0; int min = 0; int max = 0;

            do
            {
                rnd = this.Random.Next(w.ChildCount /* + 1*/);
                pos = 0;
                foreach (object x in w.Childs)
                {
                    c    = (Structs.Child)w.Childs[((DictionaryEntry)x).Key];
                    min  = pos;
                    pos += c.Occurrence; //bigger slice for more occurrences
                    max  = pos;
                    if (min <= rnd & max >= rnd)
                    {
                        text.Add(c.Word);
                        w = (Structs.RootWord)Words[c.Word.ToLower()];
                        break;
                    }
                }

                if (Length < text.Count)
                {
                    break;
                }
            } while (!w.End);

            return(text);
        }
示例#2
0
        protected void AnalyzeContent()
        {
            Structs.RootWord w = new Structs.RootWord();
            Structs.Child    c = new Structs.Child();

            string[] fragments = this.Content.Replace("\r", " ").Replace("\n", " ").Replace("\t", " ").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            bool nextIsStart = false;

            for (int i = 0; i < fragments.Length; i++)
            {
                try
                {
                    string item = fragments[i].ToLower();
                    w = new Structs.RootWord();
                    c = new Structs.Child();

                    if (this.Words.ContainsKey(item))
                    {
                        // Already Exists, add new child word or update count of existing child word
                        if (i < fragments.Length - 1)
                        {
                            w = (Structs.RootWord) this.Words[item];
                            if (nextIsStart)
                            {
                                w.Start     = true;
                                nextIsStart = false;
                                this.StartIndex.Add(item);
                            }

                            if (w.Childs.ContainsKey(fragments[i + 1].ToLower()))
                            {
                                // Exists, just update count
                                c = (Structs.Child)w.Childs[fragments[i + 1].ToLower()];
                                c.Occurrence++;
                                w.Childs.Remove(fragments[i + 1].ToLower());
                            }
                            else
                            {
                                // Doesn't Exist, add new word
                                c.Word       = fragments[i + 1];
                                c.Occurrence = 1;
                            }

                            w.ChildCount++;
                            w.Childs.Add(fragments[i + 1].ToLower(), c);

                            this.Words.Remove(item);
                            this.Words.Add(item, w);
                        }
                    }
                    else
                    {
                        // New Word
                        w        = new Structs.RootWord();
                        w.Childs = new Hashtable();
                        if (i == 0)
                        {
                            w.Start = true;
                            this.StartIndex.Add(item);
                        }

                        w.Word = fragments[i];

                        if (i < fragments.Length - 1)
                        {
                            c.Word       = fragments[i + 1];
                            c.Occurrence = 1;
                            w.Childs.Add(fragments[i + 1].ToLower(), c);
                            w.ChildCount = 1;
                        }
                        else
                        {
                            w.End = true;
                        }

                        if (item.Substring(item.Length - 1, 1) == ".")
                        {
                            w.End       = true;
                            nextIsStart = true;
                        }
                        else if (nextIsStart)
                        {
                            w.Start     = true;
                            nextIsStart = false;
                            this.StartIndex.Add(item);
                        }

                        this.Words.Add(item, w);
                    }
                }
                catch (Exception)
                {
                }
            }
        }