-
Notifications
You must be signed in to change notification settings - Fork 6
/
mapped.go
36 lines (30 loc) · 957 Bytes
/
mapped.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
package ann
// MappedANNer is an ANNer that will return the string values
// associated with the k nearest neighbors instead of their indices
type MappedANNer interface {
ANN(q []float64, k int) []string
}
// MockMappedANNer is a mockable MappedANNer
type MockMappedANNer struct {
ANNFn func(q []float64, k int) []string
}
// ANN calls the underlying ANN method
func (nn *MockMappedANNer) ANN(q []float64, k int) []string {
return nn.ANNFn(q, k)
}
// NewMappedANNer creates a new MappedANNer given an existing ANNer
// and a mapping from indices to a series of string values
func NewMappedANNer(nn ANNer, mapping []string) MappedANNer {
return &MockMappedANNer{
ANNFn: func(q []float64, k int) []string {
// Perform initial search retrieving indices
indices := nn.ANN(q, k)
// Retrieve the series of associated string values
vs := []string{}
for _, i := range indices {
vs = append(vs, mapping[i])
}
return vs
},
}
}