-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoji-table.go
70 lines (55 loc) · 1.16 KB
/
emoji-table.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
package main
import (
"context"
"encoding/json"
"os"
"sort"
"strings"
"github.com/lithammer/fuzzysearch/fuzzy"
)
type Emoji struct {
Emoji string `json:"emoji"`
Name string `json:"name"`
Category int `json:"category"`
}
type SearchResult struct {
Symbol string
Score int // lower is better
}
var emojiTable = []Emoji{
// {
// Symbol: "😀",
// Keywords: []string{"smile", "happy", "joy", "grin"},
// },
}
var emojiNames []string
func populateEmojiTable() {
file, err := os.ReadFile("emojis.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(file, &emojiTable); err != nil {
panic(err)
}
emojiNames = make([]string, len(emojiTable))
for i, emoji := range emojiTable {
emojiNames[i] = strings.ToLower(emoji.Name)
}
}
func getEmojiSuggestions(ctx context.Context, channel chan<- []Emoji, query string) {
for {
select {
case <-ctx.Done():
return
default:
results := []Emoji{}
matches := fuzzy.RankFindFold(query, emojiNames)
sort.Sort(matches)
for _, match := range matches {
results = append(results, emojiTable[match.OriginalIndex])
}
channel <- results
// close(channel)
}
}
}