class TransitionTests(Chai):

	def setUp(self):
		
		super(TransitionTests, self).setUp()
		self.mc = MarkhovChain(random_source=SequentialRandom())

	def _make_dice(self):

		for i in range(1, 7):
			for j in range(1, 7):
				self.mc.add_transition(i, j)
		self.mc.set_random_state()

	def _dice_test(self, trials, expected):

		state_counts = Counter()

		for _ in range(trials):
			state_counts[self.mc.next_state()] += 1

		# Test transitions
		print()
		print("[ State distribution ]\n")

		for i in range(1, 7):
			print("   {} : {}".format(i, state_counts[i]))

		print("\nExpecting {}".format(expected))

		for k, c in state_counts.items():
			assertEqual(state_counts[k], expected[k])

	def test_fair_dice(self):

		self._make_dice()
		self._dice_test(1200, dict((x, 200) for x in range(1, 7)))

	def test_6_weighted_dice(self):

		self._make_dice()

		for i in range(1, 7):
			self.mc.add_transition(i, 6)

		expected = dict((x, 200) for x in range(1, 7))
		expected[6] = 400

		self._dice_test(1400, expected)

	def test_6_and_4_weighted_dice(self):

		self._make_dice()

		for i in range(1, 7):
			self.mc.add_transition(i, 6)
			self.mc.add_transition(i, 4)

		expected = dict((x, 200) for x in range(1, 7))
		expected[4] = 400
		expected[6] = 400

		self._dice_test(1600, expected)
# -*- coding: utf-8 -*-

from markhov_chain import MarkhovChain
import string
import re

some_file = './some_text.txt'
text = open(some_file, 'r', encoding='utf-8').read()

TERMINATORS = "?!."
THROW_AWAYS = string.punctuation.translate(dict((ord(x), None) for x in TERMINATORS))
text = text.translate(dict((ord(x), None) for x in THROW_AWAYS if x not in ",;"))
text = re.sub("([" + string.punctuation + "])", " \\1", text)
text = text.split()
mc = MarkhovChain()

last = ""
for i in range(len(text)):
    mc.add_transition(last, text[i])
    last = text[i]

if not mc.has_state(text[-1]) or not mc.has_next_state(text[-1]):
	mc.add_transition(text[-1], "")

mc.set_state("")

print(re.sub(" ([" + "\\".join(string.punctuation) + "])", \
	"\\1", " ".join(mc.next_state() for _ in range(35))))