suggest.mx raw
1 package fuzzy
2
3 // DualIndex holds WordIndex instances for two language word lists.
4 // Built in the transdb package (which knows the lattice); queried here.
5 type DualIndex struct {
6 A *WordIndex // first language (e.g. EN)
7 B *WordIndex // second language (e.g. JA)
8 }
9
10 // NewDualIndex wraps two pre-built WordIndex instances.
11 func NewDualIndex(a, b *WordIndex) *DualIndex {
12 return &DualIndex{A: a, B: b}
13 }
14
15 // SuggestA returns fuzzy suggestions from the A index (max maxResults).
16 func (idx *DualIndex) SuggestA(query string, maxDist, maxResults int) []Match {
17 if idx == nil || idx.A == nil {
18 return nil
19 }
20 m := idx.A.Search(query, maxDist)
21 if len(m) > maxResults {
22 m = m[:maxResults]
23 }
24 return m
25 }
26
27 // SuggestB returns fuzzy suggestions from the B index.
28 func (idx *DualIndex) SuggestB(query string, maxDist, maxResults int) []Match {
29 if idx == nil || idx.B == nil {
30 return nil
31 }
32 m := idx.B.Search(query, maxDist)
33 if len(m) > maxResults {
34 m = m[:maxResults]
35 }
36 return m
37 }
38