-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Improve emoji and mention matching #24255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f6123bb
When auto-completing emojis prioritize matches that start with the gi…
yardenshoham 9afd78b
Extract matcher functions and make them more intelligent
silverwind 523c752
fix lint
silverwind 47d2548
add test for alias
silverwind 2616183
fix calculation, remove early exit
silverwind e19b64c
revert modules/emoji/emoji_data.go
silverwind c789aa2
restore file to correct branch
silverwind a751544
fix underscore matching
silverwind f05132f
add sortAndReduce helper function
silverwind fae4629
rename variable
silverwind 48d8cbe
Merge branch 'main' into emoji-matching
silverwind f109a8a
factor in alias index into weight
silverwind 2f771f6
Merge branch 'main' into emoji-matching
yardenshoham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import emojis from '../../../assets/emoji.json'; | ||
|
||
const maxMatches = 6; | ||
|
||
function sortAndReduce(map) { | ||
const sortedMap = new Map([...map.entries()].sort((a, b) => a[1] - b[1])); | ||
return Array.from(sortedMap.keys()).slice(0, maxMatches); | ||
} | ||
|
||
export function matchEmoji(queryText) { | ||
const query = queryText.toLowerCase().replaceAll('_', ' '); | ||
if (!query) return emojis.slice(0, maxMatches).map((e) => e.aliases[0]); | ||
|
||
// results is a map of weights, lower is better | ||
const results = new Map(); | ||
for (const {aliases} of emojis) { | ||
const mainAlias = aliases[0]; | ||
for (const [aliasIndex, alias] of aliases.entries()) { | ||
const index = alias.replaceAll('_', ' ').indexOf(query); | ||
if (index === -1) continue; | ||
const existing = results.get(mainAlias); | ||
const rankedIndex = index + aliasIndex; | ||
results.set(mainAlias, existing ? existing - rankedIndex : rankedIndex); | ||
} | ||
} | ||
|
||
return sortAndReduce(results); | ||
} | ||
|
||
export function matchMention(queryText) { | ||
const query = queryText.toLowerCase(); | ||
|
||
// results is a map of weights, lower is better | ||
const results = new Map(); | ||
for (const obj of window.config.tributeValues) { | ||
const index = obj.key.toLowerCase().indexOf(query); | ||
if (index === -1) continue; | ||
const existing = results.get(obj); | ||
results.set(obj, existing ? existing - index : index); | ||
} | ||
|
||
return sortAndReduce(results); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {test, expect} from 'vitest'; | ||
import {matchEmoji, matchMention} from './match.js'; | ||
|
||
test('matchEmoji', () => { | ||
expect(matchEmoji('')).toEqual([ | ||
'+1', | ||
'-1', | ||
'100', | ||
'1234', | ||
'1st_place_medal', | ||
'2nd_place_medal', | ||
]); | ||
|
||
expect(matchEmoji('hea')).toEqual([ | ||
'headphones', | ||
'headstone', | ||
'health_worker', | ||
'hear_no_evil', | ||
'heard_mcdonald_islands', | ||
'heart', | ||
]); | ||
|
||
expect(matchEmoji('hear')).toEqual([ | ||
'hear_no_evil', | ||
'heard_mcdonald_islands', | ||
'heart', | ||
'heart_decoration', | ||
'heart_eyes', | ||
'heart_eyes_cat', | ||
]); | ||
|
||
expect(matchEmoji('poo')).toEqual([ | ||
'poodle', | ||
'hankey', | ||
'spoon', | ||
'bowl_with_spoon', | ||
]); | ||
|
||
expect(matchEmoji('1st_')).toEqual([ | ||
'1st_place_medal', | ||
]); | ||
}); | ||
|
||
test('matchMention', () => { | ||
expect(matchMention('')).toEqual(window.config.tributeValues.slice(0, 6)); | ||
expect(matchMention('user4')).toEqual([window.config.tributeValues[3]]); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.