#!/usr/bin/python3 """Word-based first-order Markov text generator. Written in a fit of pique because someone said ‘a simple generator that probabilistically generates a word based on the previous one’ was ‘less than 100 lines of simple python code’. The original version was 11 lines of simple Python code; this version is 6, although maybe it’s no longer ‘simple’ now that it's using generator expressions and walrus operators. This is significantly smaller than minimarkov.py but uses the same index structure, but over words rather than characters. """ import random, sys model, last, word = {None: []}, None, None for word in (word for line in sys.stdin for word in line.split()): model.setdefault(last, []).append(last := word) sys.stdout.writelines(str(word := random.choice(model.get(word) or words)) + " " for words in [list(model.keys())] for i in range(100_000))