Skip to content

@lexiconlang/markov

Character-level Markov n-gram trainer and sampler with backoff smoothing, verbatim-rejection, and a JSON model format that's safe to ship in your bundle.

bash
pnpm add @lexiconlang/markov

Train and sample

ts
import { markov, train } from "@lexiconlang/markov";
import { createContext } from "@lexiconlang/core";

const model = train(
  ["aberffraw", "betws", "caernarfon", "dinas", "ebbw"],
  { order: 3, rejectSubstringsOfLength: 6 },
);

const townName = markov(model);
townName.generate(createContext({ seed: "wales-1" })); // "Llanrwst"

Configuration

OptionDefaultEffect
order3n-gram size
minLength3Reject samples shorter than this
maxLength16Reject samples longer than this
rejectSubstringsOfLengthundefinedRefuse samples containing N+ chars from training
meta{}Arbitrary metadata serialized into the model

JSON model format

train() returns a MarkovModel — a plain object that JSON-serializes cleanly:

ts
{
  order: 3,
  transitions: { /* ... */ },
  meta: { source: "welsh-towns" },
}

Build offline with the CLI and import as JSON:

bash
npx @lexiconlang/cli build-markov ./corpus.json --out ./model.json --order 3

See Training Markov models for the full guide.

Released under the MIT License.