Repository navigation
fix: JaroWinklerDistance of single character strings that differ in case - #786
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
For strings of length 1 the match window floor(1 / 2) - 1 is negative, so
no character matched and the Jaro distance was 0. The prefix loop also ran
past the end of the strings, so JaroWinklerDistance('A', 'a', { ignoreCase:
true }) returned 0.4 instead of 1. Clamp the match window to 0 and bound
the prefix loop by the lengths of the strings.
This branch has not been deployed
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.
Fixes # (no existing issue; the problem is described below)
What was wrong
JaroWinklerDistancereturns a wrong value for strings of length 1 that are equal after lowercasing:distance()the match window isfloor(max(len1, len2) / 2) - 1, which is-1when both strings have length 1. No character can match, so the Jaro distance is 0.JaroWinklerDistancethe prefix loopwhile (s1[l] === s2[l] && l < 4)comparesundefined === undefinedpast the end of the strings, so it counts a prefix of 4 characters for strings that are shorter. With Jaro distance 0 this gives0 + 4 * 0.1 * (1 - 0).Real output on current
master:JaroWinklerDistance('A', 'a', { ignoreCase: true })0.41JaroWinklerDistance('a', 'A', { ignoreCase: true })0.41JaroWinklerDistance('I', 'i', { ignoreCase: true })0.41Unchanged before and after:
('A', 'A')is1,('a', 'b')is0,('A', 'b', { ignoreCase: true })is0,('a', 'ab')is0.8500000000000001,('ab', 'AB', { ignoreCase: true })is1,('MARTHA', 'MARHTA')is0.9611111111111111,('DIXON', 'DICKSONX')is0.8133333333333332,('DWAYNE', 'DUANE')is0.8400000000000001.Proposed Changes
Math.max(0, ...). This is the change that fixes the example above (the window only goes negative when both strings have length 1).0.1); it only matters when a precomputeddjoption is passed for strings shorter than 4 characters.spec/jaro-winkler_spec.tsfor strings of length 1 (with and withoutignoreCase) and for the prefix bound with thedjoption.Verification
lib/natural/distance/jaro-winkler_distance.jsreverted (confirmed withgit diff --statand grep), the 2 new specs fail; with the change restored all 10 specs injaro-winkler_specpass.npm test: 860 specs, 0 failures.eslint . --ext .ts(thelintscript): no errors.npx standard lib/natural/distance/jaro-winkler_distance.js: no errors.npm run test_iowas not run (it needs external services).Checklist before merging
index.d.tsfiles (not applicable: no API or type change,index.d.tsis untouched)