Improve localized emoji ranking
This commit is contained in:
parent
1280afe619
commit
99022e7e6b
1 changed files with 18 additions and 3 deletions
|
@ -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, {
|
const fuse = new Fuse(knownEmoji, {
|
||||||
shouldSort: true,
|
shouldSort: false,
|
||||||
threshold: 0.2,
|
threshold: 0.2,
|
||||||
minMatchCharLength: 1,
|
minMatchCharLength: 1,
|
||||||
keys: ['shortName', 'tags'],
|
keys: ['shortName', 'tags'],
|
||||||
|
@ -263,7 +268,8 @@ export function createSearch(localeEmoji: SearchEmojiListType): SearchFnType {
|
||||||
});
|
});
|
||||||
|
|
||||||
const fuseExactPrefix = new Fuse(knownEmoji, {
|
const fuseExactPrefix = new Fuse(knownEmoji, {
|
||||||
shouldSort: true,
|
// We re-rank and sort manually below
|
||||||
|
shouldSort: false,
|
||||||
threshold: 0, // effectively a prefix search
|
threshold: 0, // effectively a prefix search
|
||||||
minMatchCharLength: 2,
|
minMatchCharLength: 2,
|
||||||
keys: ['shortName', 'tags'],
|
keys: ['shortName', 'tags'],
|
||||||
|
@ -280,8 +286,17 @@ export function createSearch(localeEmoji: SearchEmojiListType): SearchFnType {
|
||||||
const rankedResults = rawResults.map(entry => {
|
const rankedResults = rawResults.map(entry => {
|
||||||
const rank = entry.item.rank || 1e9;
|
const rank = entry.item.rank || 1e9;
|
||||||
|
|
||||||
|
// Rank exact prefix matches in [0,1] range
|
||||||
|
if (entry.item.shortName.startsWith(query)) {
|
||||||
return {
|
return {
|
||||||
score: (entry.score ?? 0) + rank / knownEmoji.length,
|
score: entry.item.shortName.length / maxShortNameLength,
|
||||||
|
item: entry.item,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Other matches in [1,], ordered by score and rank
|
||||||
|
return {
|
||||||
|
score: 1 + (entry.score ?? 0) + rank / knownEmoji.length,
|
||||||
item: entry.item,
|
item: entry.item,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue