|
| 1 | +package redisearch |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/gomodule/redigo/redis" |
| 6 | + "sort" |
| 7 | +) |
| 8 | + |
| 9 | +// SpellCheckOptions are options which are passed when performing spelling correction on a query |
| 10 | +type SpellCheckOptions struct { |
| 11 | + Distance int |
| 12 | + ExclusionDicts []string |
| 13 | + InclusionDicts []string |
| 14 | +} |
| 15 | + |
| 16 | +func NewSpellCheckOptionsDefaults() *SpellCheckOptions { |
| 17 | + return &SpellCheckOptions{ |
| 18 | + Distance: 1, |
| 19 | + ExclusionDicts: make([]string, 0), |
| 20 | + InclusionDicts: make([]string, 0), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func NewSpellCheckOptions(distance int) *SpellCheckOptions { |
| 25 | + return &SpellCheckOptions{ |
| 26 | + Distance: distance, |
| 27 | + ExclusionDicts: make([]string, 0), |
| 28 | + InclusionDicts: make([]string, 0), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// SetDistance Sets the the maximal Levenshtein distance for spelling suggestions (default: 1, max: 4) |
| 33 | +func (s *SpellCheckOptions) SetDistance(distance int) (*SpellCheckOptions, error) { |
| 34 | + if distance < 1 || distance > 4 { |
| 35 | + return s, fmt.Errorf("The maximal Levenshtein distance for spelling suggestions should be between [1,4]. Got %d", distance) |
| 36 | + } else { |
| 37 | + s.Distance = distance |
| 38 | + } |
| 39 | + return s, nil |
| 40 | +} |
| 41 | + |
| 42 | +// AddExclusionDict adds a custom dictionary named {dictname} to the exclusion list |
| 43 | +func (s *SpellCheckOptions) AddExclusionDict(dictname string) *SpellCheckOptions { |
| 44 | + s.ExclusionDicts = append(s.ExclusionDicts, dictname) |
| 45 | + return s |
| 46 | +} |
| 47 | + |
| 48 | +// AddInclusionDict adds a custom dictionary named {dictname} to the inclusion list |
| 49 | +func (s *SpellCheckOptions) AddInclusionDict(dictname string) *SpellCheckOptions { |
| 50 | + s.InclusionDicts = append(s.InclusionDicts, dictname) |
| 51 | + return s |
| 52 | +} |
| 53 | + |
| 54 | +func (s SpellCheckOptions) serialize() redis.Args { |
| 55 | + args := redis.Args{} |
| 56 | + if s.Distance > 1 { |
| 57 | + args = args.Add("DISTANCE").Add(s.Distance) |
| 58 | + } |
| 59 | + for _, exclusion := range s.ExclusionDicts { |
| 60 | + args = args.Add("TERMS").Add("EXCLUDE").Add(exclusion) |
| 61 | + } |
| 62 | + for _, inclusion := range s.InclusionDicts { |
| 63 | + args = args.Add("TERMS").Add("INCLUDE").Add(inclusion) |
| 64 | + } |
| 65 | + return args |
| 66 | +} |
| 67 | + |
| 68 | +// MisspelledSuggestion is a single suggestion from the spelling corrections |
| 69 | +type MisspelledSuggestion struct { |
| 70 | + Suggestion string |
| 71 | + Score float32 |
| 72 | +} |
| 73 | + |
| 74 | +// NewMisspelledSuggestion creates a MisspelledSuggestion with the specific term and score |
| 75 | +func NewMisspelledSuggestion(term string, score float32) MisspelledSuggestion { |
| 76 | + return MisspelledSuggestion{ |
| 77 | + Suggestion: term, |
| 78 | + Score: score, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// MisspelledTerm contains the misspelled term and a sortable list of suggestions returned from an engine |
| 83 | +type MisspelledTerm struct { |
| 84 | + Term string |
| 85 | + // MisspelledSuggestionList is a sortable list of suggestions returned from an engine |
| 86 | + MisspelledSuggestionList []MisspelledSuggestion |
| 87 | +} |
| 88 | + |
| 89 | +func NewMisspelledTerm(term string) MisspelledTerm { |
| 90 | + return MisspelledTerm{ |
| 91 | + Term: term, |
| 92 | + MisspelledSuggestionList: make([]MisspelledSuggestion, 0), |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (l MisspelledTerm) Len() int { return len(l.MisspelledSuggestionList) } |
| 97 | +func (l MisspelledTerm) Swap(i, j int) { |
| 98 | + l.MisspelledSuggestionList[i], l.MisspelledSuggestionList[j] = l.MisspelledSuggestionList[j], l.MisspelledSuggestionList[i] |
| 99 | +} |
| 100 | +func (l MisspelledTerm) Less(i, j int) bool { |
| 101 | + return l.MisspelledSuggestionList[i].Score > l.MisspelledSuggestionList[j].Score |
| 102 | +} //reverse sorting |
| 103 | + |
| 104 | +// Sort the SuggestionList |
| 105 | +func (l MisspelledTerm) Sort() { |
| 106 | + sort.Sort(l) |
| 107 | +} |
| 108 | + |
| 109 | +// convert the result from a redis spelling correction on a query to a proper MisspelledTerm object |
| 110 | +func loadMisspelledTerm(arr []interface{}, termIdx, suggIdx int) (missT MisspelledTerm, err error) { |
| 111 | + term, err := redis.String(arr[termIdx], err) |
| 112 | + if err != nil { |
| 113 | + return MisspelledTerm{}, fmt.Errorf("Could not parse term: %s", err) |
| 114 | + } |
| 115 | + missT = NewMisspelledTerm(term) |
| 116 | + lst, err := redis.Values(arr[suggIdx], err) |
| 117 | + if err != nil { |
| 118 | + return MisspelledTerm{}, fmt.Errorf("Could not get the array of suggestions for spelling corrections on term %s. Error: %s", term, err) |
| 119 | + } |
| 120 | + for i := 0; i < len(lst); i++ { |
| 121 | + innerLst, err := redis.Values(lst[i], err) |
| 122 | + if err != nil { |
| 123 | + return MisspelledTerm{}, fmt.Errorf("Could not get the inner array of suggestions for spelling corrections on term %s. Error: %s", term, err) |
| 124 | + } |
| 125 | + score, err := redis.Float64(innerLst[0], err) |
| 126 | + if err != nil { |
| 127 | + return MisspelledTerm{}, fmt.Errorf("Could not parse score: %s", err) |
| 128 | + } |
| 129 | + suggestion, err := redis.String(innerLst[1], err) |
| 130 | + if err != nil { |
| 131 | + return MisspelledTerm{}, fmt.Errorf("Could not parse suggestion: %s", err) |
| 132 | + } |
| 133 | + missT.MisspelledSuggestionList = append(missT.MisspelledSuggestionList, NewMisspelledSuggestion(suggestion, float32(score))) |
| 134 | + } |
| 135 | + |
| 136 | + return missT, nil |
| 137 | +} |
0 commit comments