# please do not write code without functions and without comments!
import random
import sys
import string
d = {}
for c in string.punctuation:
  d[ord(c)] = None
text = False
allwords = []
hist = {}
markov = {}
prefix = ()
for line in open(sys.argv[1]):
  if line[:3] == "***":
    if text:
      break
    else:
      text = True
  elif text:
    words = line.split()
    for word in words:
      word = word.strip().translate(d).lower()
      if len(prefix) < 3:
        prefix = prefix + (word,)
      else:
        if not prefix in markov:
          markov[prefix] = []
        markov[prefix].append(word)
        prefix = prefix[1:]+(word,)
      allwords.append(word)
      hist[word] = hist.get(word,0)+1
def swap(p):
  return p[1],p[0]
def project1(p):
  return p[1]
l = list(map(swap,hist.items()))
l.sort(reverse=True)
l = list(map(project1,l))
print(l[:20])
prefix = ("the","universal","manifestation")
for i in range(100):
  next = random.choice(markov[prefix])
  print(next)
  prefix = prefix[1:]+(next,)
