-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsearch.go
More file actions
65 lines (52 loc) · 1.27 KB
/
search.go
File metadata and controls
65 lines (52 loc) · 1.27 KB
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
package main
import (
"fmt"
"math"
"sort"
)
func norm(a []float64) float64 {
var sum float64 = 0
for _, n := range a {
sum += n * n
}
return math.Sqrt(sum)
}
func cosineDistBetween(a, b []float64) float64 {
if len(a) != vectorDims {
panic(fmt.Sprintf("Vec a length mismatch: %d", len(a)))
}
if len(b) != vectorDims {
panic(fmt.Sprintf("Vec b length mismatch: %d", len(b)))
}
norms := norm(a) * norm(b)
if norms == 0 {
return math.MaxFloat64
}
var dotProduct float64 = 0
for i, ai := range a {
dotProduct += ai * b[i]
}
// We return a negative here so that we can sort by cosine distance
return -dotProduct / norms
}
func closestDocs(docSlice []MonocleDoc, docCoord []float64, n int) []MonocleDoc {
sort.Slice(docSlice, func(i, j int) bool {
return cosineDistBetween(docCoord, docSlice[i].coords) < cosineDistBetween(docCoord, docSlice[j].coords)
})
return docSlice[:n]
}
func documentVector(wordCoords map[string]([]float64), words []string) []float64 {
docVec := make([]float64, vectorDims)
for _, word := range words {
coords, ok := wordCoords[word]
if !ok {
continue
}
for i, c := range coords {
docVec[i] += c
}
}
// we do not normalize these average vectors because we perform only cosine
// similarity on them
return docVec
}