github.com/weaviate/weaviate@v1.24.6/modules/text2vec-contextionary/additional/sempath/builder_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package sempath 13 14 import ( 15 "context" 16 "testing" 17 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 "github.com/weaviate/weaviate/entities/search" 21 txt2vecmodels "github.com/weaviate/weaviate/modules/text2vec-contextionary/additional/models" 22 ) 23 24 func TestSemanticPathBuilder(t *testing.T) { 25 t.Skip("go1.20 change") 26 c11y := &fakeC11y{} 27 b := New(c11y) 28 29 b.fixedSeed = 1000 // control randomness in unit test 30 31 input := []search.Result{ 32 { 33 ID: "7fe919ed-2ef6-4087-856c-a307046bf895", 34 ClassName: "Foo", 35 Vector: []float32{1, 0.1}, 36 }, 37 } 38 searchVector := []float32{0.3, 0.3} 39 40 c11y.neighbors = []*txt2vecmodels.NearestNeighbors{ 41 { 42 Neighbors: []*txt2vecmodels.NearestNeighbor{ 43 { 44 Concept: "good1", 45 Vector: []float32{0.5, 0.1}, 46 }, 47 { 48 Concept: "good2", 49 Vector: []float32{0.7, 0.2}, 50 }, 51 { 52 Concept: "good3", 53 Vector: []float32{0.9, 0.1}, 54 }, 55 { 56 Concept: "good4", 57 Vector: []float32{0.55, 0.1}, 58 }, 59 { 60 Concept: "good5", 61 Vector: []float32{0.77, 0.2}, 62 }, 63 { 64 Concept: "good6", 65 Vector: []float32{0.99, 0.1}, 66 }, 67 { 68 Concept: "bad1", 69 Vector: []float32{-0.1, -3}, 70 }, 71 { 72 Concept: "bad2", 73 Vector: []float32{-0.15, -2.75}, 74 }, 75 { 76 Concept: "bad3", 77 Vector: []float32{-0.22, -2.35}, 78 }, 79 { 80 Concept: "bad4", 81 Vector: []float32{0.1, -3.3}, 82 }, 83 { 84 Concept: "bad5", 85 Vector: []float32{0.15, -2.5}, 86 }, 87 { 88 Concept: "bad6", 89 Vector: []float32{-0.4, -2.25}, 90 }, 91 }, 92 }, 93 } 94 95 res, err := b.CalculatePath(input, &Params{SearchVector: searchVector}) 96 require.Nil(t, err) 97 98 expectedPath := &txt2vecmodels.SemanticPath{ 99 Path: []*txt2vecmodels.SemanticPathElement{ 100 { 101 Concept: "good5", 102 DistanceToNext: ptFloat32(0.00029218197), 103 DistanceToQuery: 0.13783735, 104 DistanceToResult: 0.011904657, 105 }, 106 { 107 Concept: "good2", 108 DistanceToNext: ptFloat32(0.014019072), 109 DistanceToPrevious: ptFloat32(0.00029218197), 110 DistanceToQuery: 0.12584269, 111 DistanceToResult: 0.015912116, 112 }, 113 { 114 Concept: "good3", 115 DistanceToNext: ptFloat32(4.9889088e-05), 116 DistanceToPrevious: ptFloat32(0.014019072), 117 DistanceToQuery: 0.21913117, 118 DistanceToResult: 6.0379505e-05, 119 }, 120 { 121 Concept: "good6", 122 DistanceToNext: ptFloat32(0.0046744347), 123 DistanceToPrevious: ptFloat32(4.9889088e-05), 124 DistanceToQuery: 0.2254098, 125 DistanceToResult: 5.364418e-07, 126 }, 127 { 128 Concept: "good1", 129 DistanceToNext: ptFloat32(0.00015383959), 130 DistanceToPrevious: ptFloat32(0.0046744347), 131 DistanceToQuery: 0.16794968, 132 DistanceToResult: 0.004771471, 133 }, 134 { 135 Concept: "good4", 136 DistanceToPrevious: ptFloat32(0.00015383959), 137 DistanceToQuery: 0.17780781, 138 DistanceToResult: 0.003213048, 139 }, 140 }, 141 } 142 143 require.Len(t, res, 1) 144 require.NotNil(t, res[0].AdditionalProperties) 145 semanticPath, semanticPathOK := res[0].AdditionalProperties["semanticPath"] 146 assert.True(t, semanticPathOK) 147 semanticPathElement, semanticPathElementOK := semanticPath.(*txt2vecmodels.SemanticPath) 148 assert.True(t, semanticPathElementOK) 149 assert.Equal(t, expectedPath, semanticPathElement) 150 } 151 152 type fakeC11y struct { 153 neighbors []*txt2vecmodels.NearestNeighbors 154 } 155 156 func (f *fakeC11y) MultiNearestWordsByVector(ctx context.Context, vectors [][]float32, k, n int) ([]*txt2vecmodels.NearestNeighbors, error) { 157 return f.neighbors, nil 158 } 159 160 func ptFloat32(in float32) *float32 { 161 return &in 162 }