#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1095
Task: Write a Python function that implements a simple Markov chain text generator from a sample paragraph
Generated: 2026-02-12T20:09:00.660948
"""

import random
from collections import defaultdict

def build_markov_chain(text, n=2):
    words = text.split()
    chain = defaultdict(list)
    for i in range(len(words) - n):
        key = tuple(words[i:i+n])
        next_word = words[i+n]
        chain[key].append(next_word)
    return chain

def generate_text(chain, start_word, length=50):
    current = tuple(start_word.split())
    result = list(current)
    for _ in range(length - len(current)):
        if current not in chain:
            break
        next_word = random.choice(chain[current])
        result.append(next_word)
        current = tuple(result[-len(current):])
    return ' '.join(result)

if __name__ == '__main__':
    sample_paragraph = "The quick brown fox jumps over the lazy dog. The dog slept through the fox's quick jump."
    markov_chain = build_markov_chain(sample_paragraph, n=2)
    generated_text = generate_text(markov_chain, "The", length=50)
    print(generated_text)