libgrammstein is a hybrid language model library combining N-gram models with subword embeddings. It is designed to integrate with lling-llang for WFST-based text correction and normalization.
libgrammstein provides:
- N-gram Language Model: Statistical word sequence prediction using Modified Kneser-Ney smoothing
- Subword Embeddings: FastText-style embeddings for handling out-of-vocabulary words
- Hybrid Model: Combines both approaches for robust scoring
- WFST Integration: Implements lling-llang's
LanguageModeltrait for lattice rescoring
┌─────────────────────────────────────────────────────────────────┐
│ libgrammstein │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ N-gram Model │ │ Subword │ │
│ │ │ │ Embeddings │ │
│ │ Modified KN │ │ FastText- │ │
│ │ smoothing │ │ style │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │
│ └──────────┬──────────┘ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ HybridLanguage │ │
│ │ Model │ │
│ │ │ │
│ │ Implements │ │
│ │ LanguageModel │ │
│ │ trait │ │
│ └─────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
use libgrammstein::prelude::*;
// Load a trained hybrid model
let model = HybridLanguageModel::load("model.bin")?;
// Score a token sequence
let log_prob = model.score_sequence(&["the", "quick", "brown", "fox"]);
// Score a continuation
let next_prob = model.score_continuation(&["the", "quick"], "brown");
println!("Sequence log probability: {}", log_prob);
println!("P(brown | the quick): {}", next_prob.exp());use libgrammstein::prelude::*;
use libgrammstein::corpus::PlaintextReader;
use libgrammstein::ngram::TrainerBuilder;
// Stream corpus from directory
let reader = PlaintextReader::from_directory("./corpus")?;
// Train N-gram model
let ngram = TrainerBuilder::new()
.order(5)
.train(&reader)?;
// Train subword embeddings
let embedding = EmbeddingTrainer::new()
.dimension(100)
.epochs(20)
.train(&reader)?;
// Combine into hybrid model
let hybrid = HybridLanguageModel::new(ngram, embedding);
hybrid.save("model.bin")?;- Overview - High-level design and principles
- Data Flow - How data flows through the system
- Threading Model - Concurrency and parallelism
- Overview - What N-gram models are and how they work
- Modified Kneser-Ney - The smoothing algorithm
- Trie Storage - Dictionary backend storage
- Query API - Probability computation
- Overview - Word embeddings with subword enrichment
- BPE Tokenizer - Byte-Pair Encoding
- Skip-gram - Training with negative sampling
- Similarity - Cosine similarity and nearest neighbors
- Phonetic Embeddings - Combining orthographic and phonetic similarity
- Acoustic Word Embeddings - Fixed-dimensional audio representations
- Overview - Combining N-gram and embeddings
- Interpolation - Score combination strategies
- OOV Handling - Out-of-vocabulary word handling
- Text Generation - Autoregressive text generation with sampling strategies
- Overview - Streaming corpus architecture
- Streaming - Memory-efficient processing
- Formats - Wikipedia, Gutenberg, plaintext
- Overview - Audio feature extraction module
- Feature Extraction - MFCC, filterbank, streaming extraction
- Acoustic Models - Candle-based neural acoustic models
- Overview - ModernBERT-based neural components
- Model - ModernBERT model wrapper and inference
- Embedder - Document and query embedding
- Rescorer - Neural rescoring for beam search
- Summarizer - Extractive summarization with MMR
- Cache - KV cache for efficient inference
- Overview - Document indexing and retrieval
- Document - Document structures and metadata
- Backend - Retrieval backends (Exact, HNSW)
- Index - RagIndex with topic integration
- Retriever - Query and retrieval pipeline
- Builder - Index construction workflow
- Overview - BERTopic-style topic modeling
- Clustering - Hierarchical agglomerative clustering
- c-TF-IDF - Keyword extraction algorithm
- Dendrogram - Topic hierarchy navigation
- Overview - Programming paradigm analysis
- Detection - ParadigmDetector usage and configuration
- Indicators - OOP, FP, reactive, procedural indicators
- API Patterns - PrefixSpan sequence mining
- Domain Patterns - Rholang and MeTTa pattern catalogs
- Overview - Neural code embedding models
- CodeT5+ - CodeT5+ model integration
- UniXcoder - UniXcoder model integration
- GraphCodeBERT - GraphCodeBERT with data flow
- Ensemble - Multi-model ensemble strategies
- Caching - Embedding cache management
- Overview - Frequent subtree pattern discovery
- TreeminerD - TreeminerD algorithm details
- Overview - Multi-language code correction module
- Language Trait - CodeLanguage trait and token types
- Languages - Python, Rust, JavaScript, Rholang, MeTTa support
- AST - Tree-sitter integration and parsing
- Tokenizer - Code tokenization system
- CPG - Code Property Graphs (AST + CFG + DFG)
- Correction Framework - Correction types and traits
- Correctors - Lexical, grammar, semantic correctors
- Pipeline - End-to-end correction workflow
- PCFG - Probabilistic context-free grammars
- GNN - Graph neural networks for code
- Embeddings - Code embeddings (UniXcoder, GraphCodeBERT)
- Constrained Decoding - Grammar-constrained generation
- WFST Export - PCFG to WFST approximation
- Subtree Mining - TreeminerD frequent pattern mining
- Overview - Integration architecture
- LanguageModel Trait - Implementing the trait
- Pipeline Usage - Using in correction pipelines
- PathMap Synergy - Shared infrastructure
- Overview - Dictionary backend integration
- Backend Selection - Choosing the right backend
- N-gram Training - Count collection and smoothing
- Embedding Training - Skip-gram training workflow
- Hyperparameters - Tuning guide
- NgramModel - N-gram model API
- SubwordEmbedding - Embedding API
- HybridLanguageModel - Hybrid model API
- Traits - Key traits and interfaces
- Train and Evaluate - End-to-end workflow
- Perplexity Filter - Text quality filtering
- Spell Correction - lling-llang integration
- Rust: 1.75+ (2024 edition)
- liblevenshtein-rust: Dictionary backends
- Corpus data: Wikipedia dumps, Project Gutenberg, or custom text files
[dependencies]
libgrammstein = { version = "0.1", features = ["lling-llang-integration", "serde"] }| Feature | Description |
|---|---|
default |
Core N-gram and embedding functionality |
lling-llang-integration |
Implements lling-llang's LanguageModel trait |
serde |
Model serialization/deserialization |
async |
Async corpus streaming (Tokio) |
acoustic |
Audio feature extraction (MFCC, filterbank) |
candle-model |
Candle-based neural acoustic models |
phonetic |
Phonetic-aware embeddings with Zompist rules |
neural-rescore |
ModernBERT embeddings, rescoring, and summarization |
rag |
RAG indexing with topic extraction |
code |
Core code correction (lexical corrector only) |
code-python |
Python language support |
code-rust |
Rust language support |
code-javascript |
JavaScript language support |
code-rholang |
Rholang language support |
code-metta |
MeTTa language support |
code-neural |
Neural code embeddings (UniXcoder, GraphCodeBERT) |
- lling-llang: WFST framework for text correction
- liblevenshtein-rust: Fuzzy string matching and trie dictionaries
- F1R3FLY.io: Distributed computing platform
MIT OR Apache-2.0