Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

LuabeginneraiAI generated

Markov Chain Text Generator

Build a Lua script that learns from a text, such as a book from Project Gutenberg, and writes new random sentences that sound a bit like it. It works with a Markov chain: it records which words follow which in the original text, then builds sentences by repeatedly picking a likely next word. Looking at the last two words instead of one makes the sentences more sensible, and you can choose this setting. The result is often funny, sometimes surprisingly good. It's a fun first Lua project: you'll learn reading files, splitting text with string patterns, tables used as dictionaries and lists, random choices, and reading command-line arguments.

Estimate
~7h
Steps
6
Completed by
0
Proposed by
codeseed.app

Lua standard library

Project roadmap

  1. 01

    Read and split the text

    ~1h

    Download a public-domain book as a plain text file from Project Gutenberg. Write a function that reads the file and splits it into words with string.gmatch, keeping punctuation such as full stops attached to words so the generator knows where sentences end. Print the number of words and the first 20. Done when: the script prints the word count and a sample of words.

  2. 02

    Build the chain

    ~1.5h

    Build a table where each key is a word (or, for order 2, two words joined with a space) and each value is a list of the words that came after it in the text, including repeats, so common followers are picked more often. Also collect the words that start sentences (words after a full stop, question mark or exclamation mark). Done when: printing the followers of a common word, such as the, shows a sensible list.

  3. 03

    Generate sentences

    ~1.5h

    Start from a random sentence-starting word, then repeatedly pick a random follower with math.random, until a word ends with a full stop, question mark or exclamation mark, or a maximum length is reached. Stop cleanly when a word has no followers. Seed the random generator with os.time(). Done when: the script prints readable, random sentences each time it runs.

  4. 04

    Add command-line options

    ~1h

    Read options from the arg table: the text file, how many sentences (default 5), the order (1 or 2, default 2) and an optional --seed for repeatable output. Check each value and print usage for invalid ones or a missing file. Done when: lua markov.lua book.txt --count 3 --order 1 works, and bad options print the usage.

  5. 05

    Tidy the output

    ~1h

    Capitalize the first letter of each sentence, remove stray quotes and brackets that aren't closed, and skip sentences that are exact copies of a sentence in the original text. Print each sentence on its own line. Done when: the output looks like clean, new sentences.

  6. 06

    Test it

    ~1h

    Write a small test file (with plain assert, or with busted) using a tiny text where you know every follower: check the chain built for orders 1 and 2, that a fixed seed gives the same sentences every time, and that an empty file gives a clear error. Done when: the tests pass.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7h · 6 steps

Tech stack

Lua standard library