-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscoring.go
84 lines (70 loc) · 1.99 KB
/
scoring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package scoring
import (
"strings"
"github.com/devnoname120/turtle"
)
func IsScoredHigher(query string, emojiLeft *turtle.Emoji, emojiRight *turtle.Emoji) bool {
scoreLeft := Score(query, emojiLeft)
scoreRight := Score(query, emojiRight)
return scoreLeft > scoreRight
}
func Score(query string, emoji *turtle.Emoji) int {
emojiNicknamePriorities := map[string][]string{
"rai": {"🙌", "✋", "🤚", "🖐", "🤨"},
"han": {"✋", "🤚", "🖐", "🤝"},
"smil": {"🙂", "😊"},
"sli": {"🙂", "🙁"},
"sw": {"😅", "😰", "😓", "😥", "💦"},
"sq": {"😆", "😝"},
"blo": {"😘"},
"dea": {"💀"},
"ey": {"👀"},
"jo": {"😂", "😹", "🕹️"},
"joy": {"😂", "😹", "🕹️"},
"ok": {"👌", "🆗"},
"che": {"✅", "✔️", "☑️", "🏁", "🏨"},
// This doesn't work because this is a keyword, and right now this function only uses names
"lo": {"😆", "🤣", "🍭"},
"spa": {"✨"},
"pra": {"🙏"},
"cry": {"😢", "😭"},
"thu": {"👍", "👎"},
}
for emojiNickname, emojiPriorities := range emojiNicknamePriorities {
if strings.HasPrefix(query, emojiNickname) {
return positionToScore(emoji.Char, emojiPriorities)
}
}
if emoji.Name == query {
return 2
}
if strings.HasPrefix(emoji.Name, query) {
return 1
}
return 0
}
func normalizeEmoji(e string) string {
return strings.ReplaceAll(e, "\uFE0F", "")
}
func positionToScore(emojiChar string, emojiChars []string) int {
norm := normalizeEmoji(emojiChar)
for i, curEmojiChar := range emojiChars {
if norm == normalizeEmoji(curEmojiChar) {
return 2 + (len(emojiChars) - i)
}
}
return 0
}
type SortedByScoreDsc struct {
Query string
Emojis *[]*turtle.Emoji
}
func (s SortedByScoreDsc) Len() int {
return len(*s.Emojis)
}
func (s SortedByScoreDsc) Less(i, j int) bool {
return IsScoredHigher(s.Query, (*s.Emojis)[i], (*s.Emojis)[j])
}
func (s SortedByScoreDsc) Swap(i, j int) {
(*s.Emojis)[i], (*s.Emojis)[j] = (*s.Emojis)[j], (*s.Emojis)[i]
}