npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@miikka_makiporhola/trigram-generator

v0.1.1

Published

Build a trigram language model from text and generate deterministic or random output.

Downloads

15

Readme

trigram-generator

A small TypeScript library for building a trigram language model from input text and generating deterministic or random token sequences.

Features

  • Trigram-based generation (2-token context -> next token)
  • Unicode-aware tokenization
  • Punctuation preserved as tokens
  • Output formatting without spaces before punctuation
  • Optional deterministic seed for reproducible generation
  • Finalization step that freezes training data for faster generation reads

Installation

npm install @miikka/trigram-generator

Usage

import { TrigramGenerator } from "@miikka/trigram-generator";

const generator = new TrigramGenerator({ seed: 357 });

generator.addSource("I wish I may I wish I might.");
generator.addSource("I may follow where the trigram leads.");

const transitions = generator.getTransitionList();

generator.finalize();

const text = generator.generate({ maxTokens: 30 });
console.log(text);

API

new TrigramGenerator(options?)

Creates a new generator.

Options:

  • seed?: number
    • If provided, the start pair is selected deterministically.
    • If omitted, the start pair is selected randomly.

addSource(source: string): void

Adds training text.

Notes:

  • Can be called multiple times before finalize().
  • Throws if called after finalize().
  • Inputs with fewer than 3 tokens are ignored.

getTransitionList(): Array<{ pair: [string, string]; nextTokens: string[] }>

Returns the current trigram transition list as token strings.

Notes:

  • Includes transitions from all added sources in insertion order.
  • Keeps duplicate next tokens to preserve transition frequency.
  • Can be called before or after finalize().

finalize(): void

Freezes the internal dataset for generation.

What it does:

  • Converts mutable transition lists into compact typed arrays (Int32Array)
  • Clears mutable training structures
  • Selects the generation start pair (seeded or random)

finalize() is idempotent.

generate(options: { maxTokens: number }): string

Generates text from the finalized model.

Rules:

  • Throws if called before finalize()
  • Returns "" when there is no usable model or maxTokens <= 0
  • Uses round-robin transition selection per token pair
  • Preserves punctuation spacing (no extra space before punctuation marks)

Tokenization

The tokenizer is Unicode-aware and uses this pattern:

\p{L}[\p{L}\p{N}'-]* | \p{N}+ | [^\s\p{L}\p{N}]

Meaning:

  • Word tokens starting with a letter, optionally continuing with letters/numbers/apostrophes/hyphens
  • Number tokens
  • Standalone punctuation/symbol tokens

Examples:

  • "don't" -> one token
  • "x-12" -> one token
  • "hello, world!" -> hello, ,, world, !

Development

npm test
npm run typecheck
npm run lint
npm run build

Browser Playground (GitHub Pages)

This repository includes a static demo app in site/ that lets you:

  • Paste one or more source texts
  • Set an optional numeric seed
  • Generate output with configurable max token count

Live demo: https://miikka-makiporhola.github.io/trigram-generator/

Run locally

npm run build
rm -rf public
mkdir -p public/dist
cp -R site/. public/
cp -R dist/. public/dist/
python3 -m http.server --directory public 4173

Then open http://localhost:4173.

Test Fixtures

Large book fixtures used by tests are documented in:

  • test/fixtures/README.md

That document includes source links and fixture preprocessing policy.