static public string BuildString(TDict t, int len, bool exact) { string last; List <string> ucStr = new List <string>(); StringBuilder sb = new StringBuilder(); foreach (string word in t.Keys.Skip(1)) { if (char.IsUpper(word.First())) { ucStr.Add(word); } } if (ucStr.Count > 0) { sb.Append(ucStr.ElementAt(Program.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[""]; } 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()); }
static void Main(string[] args) { r = new Random(); int limit, size; bool exact = false; if (args.Length < 4) { printUsage(); return; } string inFile = args[0]; string outFile = args[1]; if (!int.TryParse(args[2], out limit)) { printUsage(); return; } if (!int.TryParse(args[3], out size)) { printUsage(); return; } if (args.Length == 5) { exact = true; } if (!File.Exists(inFile)) { Console.WriteLine("Input file doesn't exist"); return; } string s = Regex.Replace(File.ReadAllText(inFile), @"\s+", " ").TrimEnd(' '); TDict t = MarkovHelper.BuildTDict(s, size); File.WriteAllText(outFile, MarkovHelper.BuildString(t, limit, exact).TrimEnd(' ')); }