import random

with open('en.txt') as f: rawText = f.read()  # Read in sample text.
WORD = True  # Word or character mode.
if WORD: rawText = rawText.split(' ')

data = {}  # Grow the database on the training text.
prev = None
for c in rawText:
  if prev != None:
    if not data.has_key(prev):
      data[prev] = {'\0': 0}  # Initialize with zero total.
    if not data[prev].has_key(c):
      data[prev][c] = 0
    data[prev][c] += 1  # Increment single entry.
    data[prev]['\0'] += 1  # Increment running total.
  prev = c

text = ''  # Start generating text.
c = None
for i in range(1000):
  if not data.has_key(c):  # First iteration, or if the last entry is unique.
    c = random.choice(data.keys())
  n = random.randint(0, data[c]['\0'])
  for (k, v) in data[c].items():  # Choose a weighted random entry.
    if k == '\0': continue  # Skip the running total field.
    n = n - v;
    if n <= 0:
      text += k + (' ' if WORD else '')
      c = k
      break

print(text)
