static public TDict BuildTDict(string s, int size, TDict t) { string prev = ""; foreach (string word in Chunk(s, size)) { if (t.ContainsKey(prev)) { WDict w = t[prev]; if (w.ContainsKey(word)) { w[word] += 1; } else { w.Add(word, 1); } } else { t.Add(prev, new WDict() { { word, 1 } }); } prev = word; } return(t); }
static private string Choose(WDict w) { long total = w.Sum(t => t.Value); while (true) { int i = r.Next(0, w.Count); double c = r.NextDouble(); System.Collections.Generic.KeyValuePair <string, uint> k = w.ElementAt(i); if (c < (double)k.Value / total) { return(k.Key); } } }
static public string BuildString(TDict t, int len, bool exact, string seed = "") { string last; List <string> ucStr = new List <string>(); StringBuilder sb = new StringBuilder(); if (seed == "") { foreach (string word in t.Keys.Skip(1)) { if (char.IsUpper(word.First())) { ucStr.Add(word); } } } else { ucStr.Add(seed); } if (ucStr.Count > 0) { sb.Append(ucStr.ElementAt(r.Next(0, ucStr.Count))); } last = sb.ToString(); sb.Append(" "); WDict w = new WDict(); for (uint i = 0; i < len; ++i) { if (t.ContainsKey(last)) { w = t[last]; } else { w = t[""]; //sb.Append(" "); } last = MarkovHelper.Choose(w); sb.Append(last.Split(' ').Last()).Append(" "); } if (!exact) { while (last.Last() != '.') { if (t.ContainsKey(last)) { w = t[last]; } else { w = t[""]; } last = MarkovHelper.Choose(w); sb.Append(last.Split(' ').Last()).Append(" "); } } return(sb.ToString()); }