From 0119478d8561da4a84cae66287f5946de5d7413d Mon Sep 17 00:00:00 2001 From: automated-signal <37887102+automated-signal@users.noreply.github.com> Date: Mon, 6 May 2024 19:28:44 -0500 Subject: [PATCH] Improve localized emoji ranking Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> --- ts/components/emoji/lib.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ts/components/emoji/lib.ts b/ts/components/emoji/lib.ts index b376a7ed99..085e339d89 100644 --- a/ts/components/emoji/lib.ts +++ b/ts/components/emoji/lib.ts @@ -254,8 +254,13 @@ export function createSearch(localeEmoji: SearchEmojiListType): SearchFnType { } } + let maxShortNameLength = 0; + for (const { shortName } of knownEmoji) { + maxShortNameLength = Math.max(maxShortNameLength, shortName.length); + } + const fuse = new Fuse(knownEmoji, { - shouldSort: true, + shouldSort: false, threshold: 0.2, minMatchCharLength: 1, keys: ['shortName', 'tags'], @@ -263,7 +268,8 @@ export function createSearch(localeEmoji: SearchEmojiListType): SearchFnType { }); const fuseExactPrefix = new Fuse(knownEmoji, { - shouldSort: true, + // We re-rank and sort manually below + shouldSort: false, threshold: 0, // effectively a prefix search minMatchCharLength: 2, keys: ['shortName', 'tags'], @@ -280,8 +286,17 @@ export function createSearch(localeEmoji: SearchEmojiListType): SearchFnType { const rankedResults = rawResults.map(entry => { const rank = entry.item.rank || 1e9; + // Rank exact prefix matches in [0,1] range + if (entry.item.shortName.startsWith(query)) { + return { + score: entry.item.shortName.length / maxShortNameLength, + item: entry.item, + }; + } + + // Other matches in [1,], ordered by score and rank return { - score: (entry.score ?? 0) + rank / knownEmoji.length, + score: 1 + (entry.score ?? 0) + rank / knownEmoji.length, item: entry.item, }; });