Skip to content

Return 0 instead of NaN if there are no shingles - #72

Open
bardrop wants to merge 1 commit into
tdebatty:masterfrom
bardrop-oss:fix-nan-without-shingles
Open

bardrop wants to merge 1 commit into
tdebatty:masterfrom
bardrop-oss:fix-nan-without-shingles

Conversation

@bardrop

@bardrop bardrop commented Nov 25, 2026

Copy link
Copy Markdown

Jaccard and SorensenDice return NaN if both strings are smaller than k:

new Jaccard(3).similarity("AB", "CD");      // NaN
new SorensenDice(3).distance("AB", "CD");   // NaN
new Cosine(3).similarity("A   ", "ABCE");   // NaN

There are no shingles, so they divide 0 by 0. Cosine checks the length of the strings, but not after the spaces are collapsed (or for precomputed profiles).

These are 0 now (distance 1), as Cosine already does for small strings. Equal strings still have similarity 1. The three new tests fail without the change.

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.

1 participant