Spaces:
Running
Running
File size: 4,803 Bytes
b110593 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package classification
import (
"fmt"
"time"
libfilters "github.com/weaviate/weaviate/entities/filters"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/modulecapabilities"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/entities/search"
libclassification "github.com/weaviate/weaviate/usecases/classification"
)
type tfidfScorer interface {
GetAllTerms(docIndex int) []TermWithTfIdf
}
type contextualPreparationContext struct {
tfidf map[string]tfidfScorer // map[basedOnProp]scorer
targets map[string]search.Results // map[classifyProp]targets
}
func (c *Classifier) prepareContextualClassification(schema schema.Schema,
vectorRepo modulecapabilities.VectorClassSearchRepo, params models.Classification,
filters libclassification.Filters, items search.Results,
) (contextualPreparationContext, error) {
p := &contextualPreparer{
inputItems: items,
params: params,
repo: vectorRepo,
filters: filters,
schema: schema,
}
return p.do()
}
type contextualPreparer struct {
inputItems []search.Result
params models.Classification
repo modulecapabilities.VectorClassSearchRepo
filters libclassification.Filters
schema schema.Schema
}
func (p *contextualPreparer) do() (contextualPreparationContext, error) {
pctx := contextualPreparationContext{}
targets, err := p.findTargetsForProps()
if err != nil {
return pctx, err
}
pctx.targets = targets
tfidf, err := p.calculateTfidfForProps()
if err != nil {
return pctx, err
}
pctx.tfidf = tfidf
return pctx, nil
}
func (p *contextualPreparer) calculateTfidfForProps() (map[string]tfidfScorer, error) {
props := map[string]tfidfScorer{}
for _, basedOnName := range p.params.BasedOnProperties {
calc := NewTfIdfCalculator(len(p.inputItems))
for _, obj := range p.inputItems {
schemaMap, ok := obj.Schema.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("no or incorrect schema map present on source object '%s': %T", obj.ID, obj.Schema)
}
var docCorpus string
if basedOn, ok := schemaMap[basedOnName]; ok {
basedOnString, ok := basedOn.(string)
if !ok {
return nil, fmt.Errorf("property '%s' present on %s, but of unexpected type: want string, got %T",
basedOnName, obj.ID, basedOn)
}
docCorpus = basedOnString
}
calc.AddDoc(docCorpus)
}
calc.Calculate()
props[basedOnName] = calc
}
return props, nil
}
func (p *contextualPreparer) findTargetsForProps() (map[string]search.Results, error) {
targetsMap := map[string]search.Results{}
for _, targetProp := range p.params.ClassifyProperties {
class, err := p.classAndKindOfTarget(targetProp)
if err != nil {
return nil, fmt.Errorf("target prop '%s': find target class: %v", targetProp, err)
}
targets, err := p.findTargets(class)
if err != nil {
return nil, fmt.Errorf("target prop '%s': find targets: %v", targetProp, err)
}
targetsMap[targetProp] = targets
}
return targetsMap, nil
}
func (p *contextualPreparer) findTargets(class schema.ClassName) (search.Results, error) {
ctx, cancel := contextWithTimeout(30 * time.Second)
defer cancel()
res, err := p.repo.VectorClassSearch(ctx, modulecapabilities.VectorClassSearchParams{
Filters: p.filters.Target(),
Pagination: &libfilters.Pagination{
Limit: 10000,
},
ClassName: string(class),
Properties: []string{"id"},
})
if err != nil {
return nil, fmt.Errorf("search closest target: %v", err)
}
if len(res) == 0 {
return nil, fmt.Errorf("no potential targets found of class '%s'", class)
}
return res, nil
}
func (p *contextualPreparer) classAndKindOfTarget(propName string) (schema.ClassName, error) {
prop, err := p.schema.GetProperty(schema.ClassName(p.params.Class), schema.PropertyName(propName))
if err != nil {
return "", fmt.Errorf("get target prop '%s': %v", propName, err)
}
dataType, err := p.schema.FindPropertyDataType(prop.DataType)
if err != nil {
return "", fmt.Errorf("extract dataType of prop '%s': %v", propName, err)
}
// we have passed validation, so it is safe to assume that this is a ref prop
targetClasses := dataType.Classes()
// len=1 is guaranteed from validation
targetClass := targetClasses[0]
return targetClass, nil
}
|