An HMM English sentence generator

×

Overview

module url N/A
git repository https://bitbucket.org/arrizza-public/ai-hmm-sentence-gen
git command git clone git@bitbucket.org:arrizza-public/ai-hmm-sentence-gen.git
verification report https://arrizza.com/web-ver/ai-hmm-sentence-gen-report.html
version info
OS Language #Runs Last Run Cov%
Ubuntu 24.04 noble Python 3.12 76 2025-08-26 2.9%

Summary

This project uses a Hidden Markov Model (HMM) to generate random English sentences.

Currently, the model is hard coded. The states the HMM can be in, the words that can be chosen in that state and the probabilities in those are pre-selected. Eventually, the HMM should be Viterbi trained using some existing word corpus. The sentences can then be generated from there.

Build

Run these:

# follow the common setup steps, see the link above 
./do_gen    # generate the Makefile
./doit      # generates default 10 sentences
./doit 20   # generates 20 sentences

HMM

See src/app.cpp for current model.

  • A sentence can start with "A" and be followed by a singular noun.
  • A sentence can start with "The" and be followed by an adjective, singular noun, or a plural noun.
  • An adjective can be followed by a singular or plural noun.
  • A singular noun or a plural noun can be followed by a verb.
  • A verb can be followed by "the" or an indirect object (e.g. "it")
  • "the" can be followed by an object
  • An object can be followed by an adverb
  • An indirect object can be followed by an adverb
  • An object is a final state and displays punctuation

The source code for examples ("symbols") of all these.

Sample output:

 --  1]  A dog ate the book.
 --  2]  The snakes smashed the sock.
 --  3]  Mommy nibbled the desk crazily.
 --  4]  They created it.
 --  5]  The dogs gobbled the car crazily.
 --  6]  Daddy created the beach.
 --  7]  Trevor changed everything crazily!
 --  8]  Grandma created the shirt.
 --  9]  Grandma chewed the hat with relish.
 -- 10]  She ate the dice!

Since the HMM probabilities and the states do not associate word meanings, some of the sentences are nonsensical but a few do make sense.

- John Arrizza