-
Notifications
You must be signed in to change notification settings - Fork 13
/
word_service.go
190 lines (169 loc) · 5.09 KB
/
word_service.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package words
import (
"context"
"os"
"path/filepath"
"sort"
"strings"
"sync"
pb "github.com/domino14/liwords/rpc/api/proto/word_service"
"github.com/domino14/macondo/alphabet"
macondoconfig "github.com/domino14/macondo/config"
"github.com/domino14/macondo/gaddag"
"github.com/rs/zerolog/log"
"github.com/twitchtv/twirp"
)
type WordService struct {
cfg *macondoconfig.Config
definitionSources map[string]*defSource
}
// NewWordService creates a Twirp WordService
func NewWordService(cfg *macondoconfig.Config) *WordService {
dawgPath := filepath.Join(cfg.LexiconPath, "dawg")
dawgDir, err := os.Open(dawgPath)
var filenames []string
if err != nil {
log.Warn().Err(err).Msgf("cannot open directory %s", dawgPath)
} else {
filenames, err = dawgDir.Readdirnames(-1)
if err != nil {
log.Warn().Err(err).Msgf("cannot readdir %s", dawgPath)
}
dawgDir.Close()
}
definitionSources := make(map[string]*defSource)
dictionaryPath := filepath.Join(cfg.LexiconPath, "words")
for _, filename := range filenames {
lexicon := strings.TrimSuffix(filename, ".dawg")
if len(lexicon) == len(filename) {
continue
}
definitionSource, err := loadDefinitionSource(filepath.Join(dictionaryPath, lexicon+".txt"))
if err != nil {
log.Warn().Err(err).Msgf("bad definition source for %s", lexicon)
} else {
definitionSources[lexicon] = definitionSource
log.Info().Msgf("found definition source for %s", lexicon)
}
}
return &WordService{cfg, definitionSources}
}
var daPool = sync.Pool{
New: func() interface{} {
return gaddag.DawgAnagrammer{}
},
}
func (ws *WordService) DefineWords(ctx context.Context, req *pb.DefineWordsRequest) (*pb.DefineWordsResponse, error) {
gd, err := gaddag.GetDawg(ws.cfg, req.Lexicon)
if err != nil {
return nil, twirp.NewError(twirp.InvalidArgument, err.Error())
}
alph := gd.GetAlphabet()
definer, hasDefiner := ws.definitionSources[req.Lexicon]
var wordsToDefine []string
results := make(map[string]*pb.DefineWordsResult)
var anagrams map[string][]string
if req.Anagrams {
anagrams = make(map[string][]string)
da := daPool.Get().(gaddag.DawgAnagrammer)
defer daPool.Put(da)
for _, query := range req.Words {
if _, found := anagrams[query]; found {
continue
}
if strings.IndexByte(query, alphabet.BlankToken) >= 0 {
return nil, twirp.NewError(twirp.InvalidArgument, "word cannot have blanks")
}
if err = da.InitForString(gd, query); err != nil {
return nil, twirp.NewError(twirp.InvalidArgument, err.Error())
}
var words []string
da.Anagram(gd, func(word alphabet.MachineWord) error {
words = append(words, word.UserVisible(alph))
return nil
})
anagrams[query] = words
if len(words) > 0 {
for _, word := range words {
if _, found := results[word]; found {
continue
}
definition := ""
if req.Definitions {
// IMPORTANT: "" will make frontend do infinite loop
definition = word // lame
if hasDefiner {
wordsToDefine = append(wordsToDefine, word)
}
}
results[word] = &pb.DefineWordsResult{D: definition, V: true}
}
}
}
} else {
for _, word := range req.Words {
machineWord, err := alphabet.ToMachineWord(word, alph)
if err != nil {
return nil, twirp.NewError(twirp.InvalidArgument, err.Error())
}
if _, found := results[word]; found {
continue
}
if gaddag.FindMachineWord(gd, machineWord) {
definition := ""
if req.Definitions {
// IMPORTANT: "" will make frontend do infinite loop
definition = word // lame
if hasDefiner {
wordsToDefine = append(wordsToDefine, word)
}
}
results[word] = &pb.DefineWordsResult{D: definition, V: true}
} else {
results[word] = &pb.DefineWordsResult{D: "", V: false}
}
}
}
if len(wordsToDefine) > 0 {
// this must be sort|uniq and matching definitions file (all-caps)
sort.Strings(wordsToDefine)
definitions, err := definer.bulkDefine(wordsToDefine)
if err != nil {
log.Warn().Err(err).Msgf("cannot read %s definition", req.Lexicon)
} else {
for _, word := range wordsToDefine {
if definition, ok := definitions[word]; ok && definition != "" {
results[word].D = definition
}
}
}
}
if req.Anagrams {
originalResults := results
results = make(map[string]*pb.DefineWordsResult)
for _, query := range req.Words {
if _, found := results[query]; found {
continue
}
if words, found := anagrams[query]; found && len(words) > 0 {
definitions := ""
if req.Definitions {
var definitionBytes []byte
for _, word := range words {
if len(definitionBytes) > 0 {
definitionBytes = append(definitionBytes, '\n')
}
definitionBytes = append(definitionBytes, word...)
definitionBytes = append(definitionBytes, " - "...)
definitionBytes = append(definitionBytes, originalResults[word].D...)
}
definitions = string(definitionBytes)
}
results[query] = &pb.DefineWordsResult{D: definitions, V: true}
} else {
results[query] = &pb.DefineWordsResult{D: "", V: false}
}
}
}
return &pb.DefineWordsResponse{Results: results}, nil
}