using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Markov { class Program { static bool Empty(string s) { return s == ""; } static void Main(string[] args) { string text = File.ReadAllText("cioran.txt").Replace('\n', ' ').Replace('\r', ' '); string[] cuvinte = text.Split(' '); List l = new List(cuvinte), l2 = new List(); l.RemoveAll(Empty); List lcuvinte = new List(l); l.Sort(); l2.Add(l[0]); for (int i = 1; i < l.Count; i++) if (l[i] != l[i - 1]) l2.Add(l[i]); Dictionary hashCuv = new Dictionary(); for (int i = 0; i < l2.Count; i++) hashCuv.Add(l2[i], i); Dictionary, List> hashMarkov = new Dictionary, List>(); KeyValuePair kvp; for (int i = 2; i < lcuvinte.Count; i++) { kvp = new KeyValuePair(hashCuv[lcuvinte[i - 2]], hashCuv[lcuvinte[i - 1]]); if (hashMarkov.ContainsKey(kvp) == false) hashMarkov[kvp] = new List(); hashMarkov[kvp].Add(hashCuv[lcuvinte[i]]); } Random r = new Random(); int a = r.Next(lcuvinte.Count - 2); int b = a + 1; Console.Write("{0} {1} ", lcuvinte[a], lcuvinte[b]); a = hashCuv[lcuvinte[a]]; b = hashCuv[lcuvinte[b]]; kvp = new KeyValuePair(a, b); do { if (hashMarkov.ContainsKey(kvp) == false) { Console.WriteLine(); break; } List list = hashMarkov[kvp]; a = b; b = list[r.Next(list.Count)]; Console.Write("{0} ", l2[b]); kvp = new KeyValuePair(a, b); } while (true); } } }