NGRAM
a very simple demo for ngram algorithm
- Active · last change 6-10-2026
- ★ 1 on GitHub
README
Tiny NGRAM (LLM) Evaluation (Delphi / FMX)
A small study project that implements an N-gram language model (character- or word-based) with a FireMonkey (FMX) GUI to train on text files and generate continuations from a prompt. The UI is titled “NGRAM Model Evaluation” and is meant for quick experimentation with model order, smoothing, and sampling settings [1][2].
What this code does
- Creates an N-gram model with configurable:
- Order (N) (e.g., 3–6 are typical)
- Gram type:
wordorchar[2][4] - Smoothing:
Laplace,WittenBell,WittenBellFast[2][4] - Optional preserve case flag [2][4]
- Loads training text (from selected files/folders in the GUI), then trains the model by collecting counts of “context → next token” transitions (classic N-gram) [4].
- Generates text from a user seed/prompt by repeatedly sampling the next token using weighted probabilities [4].
- Includes Top‑P (nucleus) filtering to restrict sampling to the smallest set of tokens whose cumulative probability mass reaches the threshold (e.g., 0.9) [3].
Project layout (key units)
GUI.FMX.TestLLMs.fmx/GUI.FMX.TestLLMs.pas
FMX application + controls (gram type, smoothing selection, status/output memo) and model creation configuration [1][2].Unit_Math.AI.Ngram.pas
CoreTNGramModelimplementation (ngChar/ngWord, smoothing options, generation/sampling logic) [4].Unit_Math.AI.Ngram.helpers.pas
Helper utilities, includingApplyTopP(...)for nucleus sampling [3].
How to use (GUI)
- Open the project in Delphi (FMX).
- Run the app. You should see the “NGRAM Model Evaluation” window [1].
- Choose:
- Order (N) (e.g., 3)
- Gram type:
wordorchar[2] - Smoothing: Laplace / WittenBell / WittenBellFast [2]
- Optional: Preserve Case [2]
- Click Create Model to instantiate
TNGramModelwith your settings [2]. - Load training text (via folder/file selection in the UI), then Train Model.
- Type a seed prompt, set generation parameters (length / TopP etc. as exposed by your UI), and click Execute Model to generate output.
Notes on sampling
- The generator samples next tokens from weighted candidates (probability-like weights) [4].
- Top‑P filtering keeps only the highest-probability tokens until the cumulative mass reaches
TopP[3]. - Smoothing affects how probabilities are assigned when contexts are sparse/unseen (
smLaplace,smWittenBell,smWittenBellFast) [4].
How to extend
1) Add a new smoothing method
- Extend the
TSmoothingenum (currently includessmLaplace,smWittenBell,smWittenBellFast) [4]. - Implement the corresponding probability/backoff logic inside the model where next-token weights are computed.
- Add the new option to the GUI combo box initialization (currently adds Laplace/WittenBell/WittenBellFast) [2].
2) Add new sampling controls (Top‑K, temperature, etc.)
- Top‑P already exists as a helper (
ApplyTopP) [3]. - You can add:
- Top‑K: sort candidates by weight, keep K
- Temperature: transform weights with
p_i^(1/T)(or logit scaling if you convert to logits)
- Then call these filters/transforms in the token selection step used during generation [4].
3) Improve tokenization
- For
ngWord, you can refine splitting rules (punctuation handling, normalization, Unicode categories). - For
ngChar, you can optionally support multi-byte grapheme clusters instead of code units.
4) Persistence / interoperability
- There are GUI buttons for saving/loading; you can extend the model with:
- Versioned serialization
- Export to JSON for inspection/debugging
- Compression for large count tables
Limitations (expected for a study project)
- N-grams capture local context only; coherence degrades with longer text.
- Training data quality strongly affects output.
- Large orders and word-based models can grow memory quickly.
Delphi 11 Compiler

