r/PythonLearning • • 7d ago

Learning Project: Word-level Markov chain text generator

I wrote a small Python program that generates text using a word-level Markov chain. It reads a source.txt file, builds a dictionary of word sequences and their possible followers, and then generates new text by randomly selecting subsequent words. I'm sharing it here because I'd appreciate feedback on the implementation, particularly code structure, efficiency, and any improvements you might suggest.

"""Word-level Markov chain text generator.
Learns which words follow each ORDER-word group in source.txt,
then generates new text by repeatedly picking a random follower.
"""

from collections import defaultdict
import random

ORDER = 3
STEPS = 50

def build_dict(data):
    big_dict = defaultdict(list)
    words = data.split()

    if len(words) <= ORDER:
        raise ValueError(f"Source needs more than {ORDER} words")

    length = len(words) - ORDER

    for i in range(length):
        previous = tuple(words[i : i + ORDER])
        following = words[i + ORDER]
        big_dict[previous].append(following)

    return dict(big_dict)

def generate_text(big_dict):
    output_words = list()
    keys = list(big_dict.keys())
    start = random.choice(keys)
    output_words.extend(start)
    step = 0

    while step < STEPS:
        try:
            choice = random.choice(big_dict[start])
            output_words.append(choice)
            start = tuple(output_words[-ORDER:])
        except KeyError:
            start = random.choice(keys)
            output_words.extend(start)
        finally:
            step += 1

    return output_words

if __name__ == "__main__":
    with open("source.txt", "r", encoding="utf-8") as file:
        data = file.read()

    big_dict = build_dict(data)
    output_words = generate_text(big_dict)
    print(" ".join(output_words))```
4 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/Dracunculous 7d ago

That’s really cool! It makes me wonder, how do other Python programs typically handle state? Is this kind of approach common, or are there other ways of managing it?

1

u/Sea-Ad7805 7d ago

The Python runtime environment handles the state, I'm just visualizing it so students can learn how to think about it more easily.

1

u/Dracunculous 6d ago

Is there a way for us to intentionally influence how Python manages state to improve efficiency?

2

u/Sea-Ad7805 6d ago

Yes, choosing the right data structure list, set, dict, ... for the implementation of logic is one way of doing that. Do a Data Structure and Algorithms (DSA) course after doing a general Python introduction course to learn more.