private List <string> Output(int Length) { List <string> output = new List <string>(10); Structs.RootWord w = (Structs.RootWord)Words[((string)startindex[MainRandom.Next(startindex.Count)]).ToLower()]; output.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 = MainRandom.Next(w.ChildCount /* + 1*/); pos = 0; foreach (object x in w.Childs) { c = (Structs.Child)w.Childs[((System.Collections.DictionaryEntry)x).Key]; min = pos; pos += c.Occurrence; //bigger slice for more occurrences max = pos; if (min <= rnd & max >= rnd) { output.Add(c.Word); w = (Structs.RootWord)Words[c.Word.ToLower()]; break; } } //Выходит ли текст за указанное значение if (output.Count > Length) { break; } } while (!w.End); return(output); }
public override void Load(string Input) { if (Input == string.Empty) { throw new Exception("Input text == string.Empty!"); } startindex = new ArrayList(); Words = new Hashtable(10240, .1f); Structs.RootWord w = new Structs.RootWord(); Structs.Child c = new Structs.Child(); Input = Input.ToLower(); if (this.settings.TextGenrationCGPunctuationMarksConsideration == 1) { Input = Input.Replace(".", " ").Replace(",", " ").Replace("!", " ").Replace("?", " ").Replace(":", " ") .Replace(";", " ").Replace("-", " ").Replace("(", " ").Replace(")", " ").Replace("[", " ").Replace("]", " ") .Replace("{", " ").Replace("}", " "); } string[] s = Input.Replace("\r", " ").Replace("\n", " ").Replace("\t", " ").Replace(" ", " ").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string s1 = string.Empty; bool NextisStart = false; for (int i = 0; i < s.Length; i++) { try { s1 = s[i].ToLower(); w = new Structs.RootWord(); c = new Structs.Child(); if (Words.ContainsKey(s1)) {//Already Exists, add new child word or update count of existing child word if (i < s.Length - 1) { w = (Structs.RootWord)Words[s1]; if (NextisStart) { w.Start = true; NextisStart = false; startindex.Add(s1); } if (w.Childs.ContainsKey(s[i + 1].ToLower())) //Exists, just update count { c = (Structs.Child)w.Childs[s[i + 1].ToLower()]; c.Occurrence++; w.Childs.Remove(s[i + 1].ToLower()); } else //Doesn't Exist, add new word { c.Word = s[i + 1]; c.Occurrence = 1; } w.ChildCount++; w.Childs.Add(s[i + 1].ToLower(), c); Words.Remove(s1); Words.Add(s1, w); } } else {//New Word w = new Structs.RootWord(); w.Childs = new Hashtable(); if (i == 0) { w.Start = true; startindex.Add(s1); } w.Word = s[i]; if (i < s.Length - 1) { c.Word = s[i + 1]; c.Occurrence = 1; w.Childs.Add(s[i + 1].ToLower(), c); w.ChildCount = 1; } else { w.End = true; } if (s1.Substring(s1.Length - 1, 1) == ".") { w.End = true; NextisStart = true; } else if (NextisStart) { w.Start = true; NextisStart = false; startindex.Add(s1); } Words.Add(s1, w); } } catch (Exception) { } } }