thecodingtrain.com icon indicating copy to clipboard operation
thecodingtrain.com copied to clipboard

bug/feat: exact match should be first result in search dropdown

Open dipamsen opened this issue 1 year ago • 1 comments

image

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".

dipamsen avatar Oct 30 '23 08:10 dipamsen

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.

shiffman avatar Oct 30 '23 21:10 shiffman

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.

CleanShot 2024-07-10 at 10 24 25@2x

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.

CleanShot 2024-07-10 at 10 27 14@2x

// 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.

fturmel avatar Jul 10 '24 14:07 fturmel