Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mathetake committed Apr 19, 2019
1 parent 13b1416 commit f40fe07
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
45 changes: 30 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,53 @@ The implemented algorithm is truly inspired by Annoy (https://github.com/spotify
## usage

```golang
package main

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/mathetake/gann/index"

"github.com/mathetake/gann"
"github.com/mathetake/gann/metrics"
)

func main() {
var dim = 3
var nTrees = 2
var k = 10
var nItem = 1000
var (
dim = 3
nTrees = 2
k = 10
nItem = 1000
)

rawItems := make([][]float32, 0, nItem)
func main(t *testing.T) {
rawItems := make([][]float64, 0, nItem)
rand.Seed(time.Now().UnixNano())

for i := 0; i < nItem; i++ {
item := make([]float32, 0, dim)
item := make([]float64, 0, dim)
for j := 0; j < dim; j++ {
item = append(item, rand.Float32())
item = append(item, rand.Float64())
}
rawItems = append(rawItems, item)
}

// create index
gIDx := index.GetIndex(rawItems, dim, nTrees, k, true)
gIDx.Build()
idx, err := gann.CreateNewIndex(rawItems, dim, nTrees, k, metrics.TypeCosineDistance)
if err != nil {
// error handling
return
}

// search
q := []float64{0.1, 0.02, 0.001}
res, err := idx.GetANNbyVector(q, 5, 10)
if err != nil {
// error handling
return
}

// do search
q := []float32{0.1, 0.02, 0.001}
ann, _ := gIDx.GetANNbyVector(q, 5, 10)
fmt.Printf("res: %v\n", res)
}
```

Expand Down
2 changes: 1 addition & 1 deletion metrics/cosine.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *cosineDistance) GetSplittingVector(vs [][]float64) []float64 {

c1 = make([]float64, c.dim)
it1 := int(float64(lvs)*cosineMetricsCentroidCalcRatio + 1)
for i := 0; i < int(float32(lc1)*cosineMetricsCentroidCalcRatio+1); i++ {
for i := 0; i < int(float64(lc1)*cosineMetricsCentroidCalcRatio+1); i++ {
for d := 0; d < c.dim; d++ {
c1[d] += clusterToVecs[1][rand.Intn(lc1)][d] / float64(it1)
}
Expand Down

0 comments on commit f40fe07

Please sign in to comment.