Evaluation Methods
TheNgramLanguageModel class provides multiple methods for evaluating probabilities and model quality.
score
Compute the probability of a word given its context.word(string) - The target word to scorecontext(string[], optional) - Previous words in the sequence (default:[])
- Input is automatically lowercased
- Context is trimmed to model order - 1
- Returns smoothed probabilities based on model type
logScore
Compute the log probability (base 2) of a word given its context.word(string) - The target word to scorecontext(string[], optional) - Previous words in the sequence (default:[])
- Numerical stability for very small probabilities
- Computing cross-entropy and perplexity
- Comparing models on same data
perplexity
Compute the perplexity of a token sequence. Lower perplexity indicates better model fit.tokens(string[]) - Sequence of tokens to evaluate
- Lower perplexity = better model fit
- Perplexity of 1 = perfect prediction
- Higher perplexity = more uncertain predictions
- Perplexity roughly represents “average branching factor”
evaluateBatch
Evaluate multiple word-context pairs and compute perplexity in a single optimized call.probes(LmProbe[]) - Array of word-context pairs to scoreperplexityTokens(string[]) - Token sequence for perplexity computation
scores(number[]) - Probability scores for each probeperplexity(number) - Perplexity on the token sequence
- For order ≤ 3, uses optimized native implementation
- Processes all probes in a single pass
- More efficient than calling
score()andperplexity()separately