All pastes #269638 Raw Edit

GrayShade

public text v1 · immutable
·published 2006-12-06 18:53 UTC
rendered paste body
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<string> l = new List<string>(cuvinte), l2 = new List<string>();
			l.RemoveAll(Empty);
			List<string> lcuvinte = new List<string>(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<string, int> hashCuv = new Dictionary<string, int>();
			for (int i = 0; i < l2.Count; i++)
				hashCuv.Add(l2[i], i);

			Dictionary<KeyValuePair<int, int>, List<int>> hashMarkov = new Dictionary<KeyValuePair<int, int>, List<int>>();
			for (int i = 2; i < lcuvinte.Count; i++)
			{
				KeyValuePair<int, int> kvp = new KeyValuePair<int, int>(hashCuv[lcuvinte[i - 2]], hashCuv[lcuvinte[i - 1]]);
				if (hashMarkov.ContainsKey(kvp) == false)
					hashMarkov[kvp] = new List<int>();

				hashMarkov[kvp].Add(hashCuv[lcuvinte[i]]);
			}

			foreach (KeyValuePair<KeyValuePair<int, int>, List<int>> kvp in hashMarkov)
				Console.WriteLine(kvp.Value.Count);
		}
	}
}