Conversation
luozhouyang
added a commit
that referenced
this pull request
Nov 8, 2026
- Fix NGram treating newline input as padding, which produced distances above 1 for some inputs (#41). Thanks to @Narek-Khachikyan.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NGram treats literal input newlines as its internal padding marker. Matching newline characters can shrink the substitution denominator to zero, leading to incorrect edit costs and normalized distances above 1. For example,
NGram(1).distance('\na', '\nb')returns1.5instead of0.5; withn=2, it returns1.5instead of0.25.The padding marker is now distinct from every valid input character. Equal, empty, short-string, and Unicode character-count behavior are preserved. No dependency or API changes.
Evidence
1.5instead of0.5.cargo test --workspace --lockedpasses all 65 Rust tests. Python tests pass: 74 tests and 52 subtests. Regressions include exact expected scores plus relabeling invariance for leading/interior/trailing/repeated newlines, NUL, Unicode, and n=1..4.cargo fmt --all -- --check,cargo clippy --workspace --all-targets --locked -- -D warnings, Black, Ruff's configured export-order check, andgit diff --checkpass.Merge Danger
Door: two-way. Local algorithm correction, easily reverted.
Blast Radius: NGram. Changes inputs containing the former newline padding character; existing normal-text behavior is preserved.
Created with GPT-6.1 Sol (high reasoning, priority) in T3 Code through the Codex harness, including separate Sol agents for implementation and independent review.