dcporter / didyoumean.js

A simple, optimized JS library & node.js module for matching short, human-quality input to a list of possibilities.
Other
174 stars 12 forks source link

Wrong results if more than 1 deltion is needed. #2

Open zwif opened 6 years ago

zwif commented 6 years ago

There seems to be a bug if an input string is more than one character longer then an correlating string from the list.

Code for reproduction (runkit):

var didyoumean = require("didyoumean")
didyoumean.threshold = null;

var input = 'follower';
var list = ['follow', 'justsomethingrandom'];

let result = didyoumean(input, list);
console.log(result);
// > "justsomethingrandom"

However, the distance from follower to follow is 2 and from follower to justsomethingrandom 16. This obviously should return follow instead of justsomethingrandom.

General it seems that strings, which have a distance that includes more than 1 deletion are not matched at all.

Edit:

After some debugging I think the problem is due to line 239 and 240 in didYouMean-1.2.1.js:

maxJ = lenb + 1;
if (maxJ > max + i) maxJ = max + i;

I don't see why maxJ is restricted to length + 1, to my mind maxJ should be lenb + max.

Anyway, replacing that two lines with

maxJ = lenb + max;

seems to work fine, but maybe I'm missing something.

dcporter commented 6 years ago

Thanks! I’m unfortunately not currently in a position to write code for this project. However if you submit a PR with that change I’ll take a look.