markov5_use.py

from __future__ import print_function

#
# Generate a non-sensical sentence using bigram
#
def compose(m1, m2, count=10):
    from markov5_gram import random_prefix, random_suffix, shift
    first_prefix = random_prefix(m1)
    first_word = first_prefix[0]
    second_word = random_suffix(m1, first_prefix)
    prefix = (first_word, second_word)
    print(first_word, second_word, end=' ')
    for n in range(2, count):
        next_word = random_suffix(m2, prefix)
        print(next_word, end=' ')
        prefix = shift(prefix, next_word)
    print()

def compose_from(path, count=10):
    from markov5_gram import read_grams
    m1, m2 = read_grams(path)
    compose(m1, m2, count)

try:
    compose_from('bee.txt')
except IOError as e:
    print("compose error: missing file: %s" % e)
except KeyError as e:
    print("compose error: illegal prefix: %s" % e)