@@ -140,26 +140,26 @@ func Load(filename string) (*Spell, error) {
140
140
return s , nil
141
141
}
142
142
143
- type dictParams struct {
143
+ type dictOptions struct {
144
144
name string
145
145
}
146
146
147
147
// DictionaryOption is a function that controls the dictionary being used.
148
148
// An error will be returned if a dictionary option is invalid
149
- type DictionaryOption func (* dictParams ) error
149
+ type DictionaryOption func (* dictOptions ) error
150
150
151
- func (s * Spell ) defaultDictParams () * dictParams {
152
- return & dictParams {
151
+ func (s * Spell ) defaultDictOptions () * dictOptions {
152
+ return & dictOptions {
153
153
name : defaultDict ,
154
154
}
155
155
}
156
156
157
- // Name defines the name of the dictionary that should be used when storing,
158
- // deleting, looking up words, etc. If not set, the default dictionary will be
159
- // used
160
- func Name (name string ) DictionaryOption {
161
- return func (params * dictParams ) error {
162
- params .name = name
157
+ // DictionaryName defines the name of the dictionary that should be used when
158
+ // storing, deleting, looking up words, etc. If not set, the default dictionary
159
+ // will be used
160
+ func DictionaryName (name string ) DictionaryOption {
161
+ return func (opts * dictOptions ) error {
162
+ opts .name = name
163
163
return nil
164
164
}
165
165
}
@@ -168,10 +168,10 @@ func Name(name string) DictionaryOption {
168
168
// will be overwritten. Returns true if a new word was added, false otherwise.
169
169
// Will return an error if there was a problem adding a word
170
170
func (s * Spell ) AddEntry (de Entry , opts ... DictionaryOption ) (bool , error ) {
171
- dictParams := s .defaultDictParams ()
171
+ dictOptions := s .defaultDictOptions ()
172
172
173
173
for _ , opt := range opts {
174
- if err := opt (dictParams ); err != nil {
174
+ if err := opt (dictOptions ); err != nil {
175
175
return false , err
176
176
}
177
177
}
@@ -182,13 +182,13 @@ func (s *Spell) AddEntry(de Entry, opts ...DictionaryOption) (bool, error) {
182
182
183
183
// If the word already exists, just update its result - we don't need to
184
184
// recalculate the deletes as these should never change
185
- if _ , exists := s .library .load (dictParams .name , word ); exists {
185
+ if _ , exists := s .library .load (dictOptions .name , word ); exists {
186
186
atomic .AddUint64 (& s .cumulativeFreq , ^ (de .Frequency - 1 ))
187
- s .library .store (dictParams .name , word , de )
187
+ s .library .store (dictOptions .name , word , de )
188
188
return false , nil
189
189
}
190
190
191
- s .library .store (dictParams .name , word , de )
191
+ s .library .store (dictOptions .name , word , de )
192
192
193
193
// Keep track of the longest word in the dictionary
194
194
wordLength := uint32 (len ([]rune (word )))
@@ -208,7 +208,7 @@ func (s *Spell) AddEntry(de Entry, opts ...DictionaryOption) (bool, error) {
208
208
str : word ,
209
209
}
210
210
for deleteHash := range deletes {
211
- s .dictionaryDeletes .add (dictParams .name , deleteHash , & de )
211
+ s .dictionaryDeletes .add (dictOptions .name , deleteHash , & de )
212
212
}
213
213
}
214
214
@@ -218,15 +218,15 @@ func (s *Spell) AddEntry(de Entry, opts ...DictionaryOption) (bool, error) {
218
218
// GetEntry returns the Entry for word. If a word does not exist, nil will
219
219
// be returned
220
220
func (s * Spell ) GetEntry (word string , opts ... DictionaryOption ) (* Entry , error ) {
221
- dictParams := s .defaultDictParams ()
221
+ dictOpts := s .defaultDictOptions ()
222
222
223
223
for _ , opt := range opts {
224
- if err := opt (dictParams ); err != nil {
224
+ if err := opt (dictOpts ); err != nil {
225
225
return nil , err
226
226
}
227
227
}
228
228
229
- if entry , exists := s .library .load (dictParams .name , word ); exists {
229
+ if entry , exists := s .library .load (dictOpts .name , word ); exists {
230
230
return & entry , nil
231
231
}
232
232
return nil , nil
@@ -240,15 +240,15 @@ func (s *Spell) GetLongestWord() uint32 {
240
240
// RemoveEntry removes a entry from the dictionary. Returns true if the entry
241
241
// was removed, false otherwise
242
242
func (s * Spell ) RemoveEntry (word string , opts ... DictionaryOption ) (bool , error ) {
243
- dictParams := s .defaultDictParams ()
243
+ dictOpts := s .defaultDictOptions ()
244
244
245
245
for _ , opt := range opts {
246
- if err := opt (dictParams ); err != nil {
246
+ if err := opt (dictOpts ); err != nil {
247
247
return false , err
248
248
}
249
249
}
250
250
251
- return s .library .remove (dictParams .name , word ), nil
251
+ return s .library .remove (dictOpts .name , word ), nil
252
252
}
253
253
254
254
// Save a representation of spell to disk at filename
@@ -305,7 +305,7 @@ func (s SuggestionList) String() string {
305
305
}
306
306
307
307
type lookupParams struct {
308
- dictParams * dictParams
308
+ dictOpts * dictOptions
309
309
distanceFunction func ([]rune , []rune , int ) int
310
310
editDistance uint32
311
311
prefixLength uint32
@@ -315,7 +315,7 @@ type lookupParams struct {
315
315
316
316
func (s * Spell ) defaultLookupParams () * lookupParams {
317
317
return & lookupParams {
318
- dictParams : s . defaultDictParams (),
318
+ dictOpts : s . defaultDictOptions (),
319
319
distanceFunction : strmet .DamerauLevenshteinRunes ,
320
320
editDistance : s .MaxEditDistance ,
321
321
prefixLength : s .PrefixLength ,
@@ -346,7 +346,7 @@ type LookupOption func(*lookupParams) error
346
346
func DictionaryOpts (opts ... DictionaryOption ) LookupOption {
347
347
return func (params * lookupParams ) error {
348
348
for _ , opt := range opts {
349
- if err := opt (params .dictParams ); err != nil {
349
+ if err := opt (params .dictOpts ); err != nil {
350
350
return err
351
351
}
352
352
}
@@ -404,7 +404,7 @@ func PrefixLength(prefixLength uint32) LookupOption {
404
404
}
405
405
}
406
406
407
- func (s * Spell ) newDictSuggestion (input string , dist int , dp * dictParams ) Suggestion {
407
+ func (s * Spell ) newDictSuggestion (input string , dist int , dp * dictOptions ) Suggestion {
408
408
entry , _ := s .library .load (dp .name , input )
409
409
410
410
return Suggestion {
@@ -429,11 +429,11 @@ func (s *Spell) Lookup(input string, opts ...LookupOption) (SuggestionList, erro
429
429
}
430
430
431
431
results := SuggestionList {}
432
- dict := lookupParams .dictParams .name
432
+ dict := lookupParams .dictOpts .name
433
433
434
434
// Check for an exact match
435
435
if _ , exists := s .library .load (dict , input ); exists {
436
- results = append (results , s .newDictSuggestion (input , 0 , lookupParams .dictParams ))
436
+ results = append (results , s .newDictSuggestion (input , 0 , lookupParams .dictOpts ))
437
437
438
438
if lookupParams .suggestionLevel != LevelAll {
439
439
return results , nil
@@ -556,14 +556,14 @@ func (s *Spell) Lookup(input string, opts ...LookupOption) (SuggestionList, erro
556
556
results = SuggestionList {}
557
557
}
558
558
case LevelBest :
559
- entry , _ := s .library .load (lookupParams .dictParams .name , suggestion .str )
559
+ entry , _ := s .library .load (lookupParams .dictOpts .name , suggestion .str )
560
560
561
561
curFreq := entry .Frequency
562
562
closestFreq := results [0 ].Frequency
563
563
564
564
if dist < editDistance || curFreq > closestFreq {
565
565
editDistance = dist
566
- results [0 ] = s .newDictSuggestion (suggestion .str , dist , lookupParams .dictParams )
566
+ results [0 ] = s .newDictSuggestion (suggestion .str , dist , lookupParams .dictOpts )
567
567
}
568
568
continue
569
569
}
@@ -574,7 +574,7 @@ func (s *Spell) Lookup(input string, opts ...LookupOption) (SuggestionList, erro
574
574
}
575
575
576
576
results = append (results ,
577
- s .newDictSuggestion (suggestion .str , dist , lookupParams .dictParams ))
577
+ s .newDictSuggestion (suggestion .str , dist , lookupParams .dictOpts ))
578
578
}
579
579
580
580
}
0 commit comments