thecodingtrain.com
thecodingtrain.com copied to clipboard
bug/feat: exact match should be first result in search dropdown
Here, preferably "data" should be listed as the first search result, or it leads to a weird interaction where typing "data" and Enter opens up the search results for "COCO dataset".
ah! looks like it is maybe doing it in alphabetical order? We could implement a Levenshtein distance (I actually had this on my list to do a quick coding challenge at one point heh) or similar algorithm maybe? Or perhaps there's a more standard way of sorting results that can be applied here.
Looking at this issue this morning. Let me know what you think and I can finish the PR.
Levenshtein distance doesn't perform great here because it will rank shorter strings higher. It bubbles up the best match properly, but the rest is not very coherent.
The following approach ranks full word matches highest, then ranks partial matches based on the closeness to the beginning of the string. Seems to work well and it's much cheaper to compute.
// values are normalized before being passed to this function
const rank = (value, input) => {
// exact match: highest priority
if (value === input) return 0;
// complete word match: higher priority based on word position
const words = value.split(' ');
for (let i = 0; i < words.length; i++) {
if (words[i] === input) return i + 1;
}
// partial match: lower priority based on character position
const index = value.indexOf(input);
return index === -1 ? Number.MAX_SAFE_INTEGER : 1000 + index;
};
EDIT: made it so that partial matches can never rank higher than full word match by adding 1000 instead of using word count.