Registry Mirror
Skip to content

Conversation

@Narek-Khachikyan

Copy link
Copy Markdown
Contributor

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') returns 1.5 instead of 0.5; with n=2, it returns 1.5 instead of 0.25.

 n-gram padding
-  '\n' (collides with input)
+  None (input characters are Some(char))

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

  • Before: Both the Rust newline regression and Python newline regression fail with 1.5 instead of 0.5.
  • After: cargo test --workspace --locked passes 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, and git diff --check pass.
  • Independent automated review checked denominator positivity, score bounds, preserved branches, and storage size. A 21,675-case full-matrix differential test passed during investigation; the committed tests use the smaller invariance check suggested in review.
  • Validated on Python 3.13.9 and Rust 1.99.1, macOS.

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.

@luozhouyang
luozhouyang merged commit 6965273 into luozhouyang:master Nov 8, 2026
6 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants