Skip to content

Fix SimilarityEncoder n-gram counts past the end of the string - #2311

Merged
rcap107 merged 3 commits into
skrub-data:mainfrom
SashaMIT:fix/similarity-ngram-count
Oct 9, 2026
Merged

rcap107 merged 3 commits into
skrub-data:mainfrom
SashaMIT:fix/similarity-ngram-count

Conversation

@SashaMIT

@SashaMIT SashaMIT commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

SimilarityEncoder(ngram_range=(2, 5)) fit on "a" returned self-similarity 3. get_ngram_count adds len(string) - n + 1 for every order, including windows longer than the string, so the padded string " a " (length 3) was counted as 2 n-grams instead of 3. The fast path, which is the default, then divided the overlap by that short total. The slow path uses the vectorizer totals and returned 1. Orders that do not fit now contribute 0. The default ngram_range=(2, 4) on "a" is unchanged, because the order-4 window count was already 0.

test_similarity_encoder_self_similarity_when_ngram_exceeds_string failed on main (3) and passes after the change. test_similarity_encoder.py passed (13 passed, 9 xpassed). The rest of the package pytest suite was not run.

AI Disclosure

  • This PR contains AI-generated code
  • I have tested the code generated in my PR
  • I have read and understood every line that has been generated by the AI agent
  • I can explain what the AI-generated code does

SimilarityEncoder(ngram_range=(2, 5)) on "a" returned self-similarity 3. Windows longer than the string were counted as negative, so the fast path divided by the wrong total.
@rcap107

rcap107 commented Sep 30, 2026

Copy link
Copy Markdown
Member

Hi @SashaMIT, out of curiosity, did you fix this because you encountered the issue while using the SimilarityEncoder?

@SashaMIT

Copy link
Copy Markdown
Contributor Author

Hi, I did not hit this while using SimilarityEncoder. I was reading the n-gram counts, and a window longer than the string came out negative, so the fast path divided by 2 instead of 3. The slow path on the same input was 1.

@rcap107 rcap107 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks @SashaMIT

Comment thread CHANGES.rst Outdated
@rcap107
rcap107 merged commit 35effab into skrub-data:main Oct 9, 2026
29 checks passed
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