github.com/weaviate/weaviate@v1.24.6/modules/ner-transformers/additional/tokens/tokens_result.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 tokens
    13  
    14  import (
    15  	"context"
    16  	"errors"
    17  	"fmt"
    18  
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/search"
    21  	"github.com/weaviate/weaviate/modules/ner-transformers/ent"
    22  )
    23  
    24  func (p *TokenProvider) findTokens(ctx context.Context,
    25  	in []search.Result, params *Params,
    26  ) ([]search.Result, error) {
    27  	if len(in) > 0 {
    28  
    29  		if len(in) == 0 {
    30  			return nil, nil
    31  		}
    32  
    33  		if params == nil {
    34  			return nil, fmt.Errorf("no params provided")
    35  		}
    36  
    37  		properties := params.GetProperties()
    38  
    39  		// check if user parameter values are valid
    40  		if len(properties) == 0 {
    41  			return in, errors.New("no properties provided")
    42  		}
    43  
    44  		for i := range in { // for each result of the general GraphQL Query
    45  			ap := in[i].AdditionalProperties
    46  			if ap == nil {
    47  				ap = models.AdditionalProperties{}
    48  			}
    49  
    50  			// check if the schema of the GraphQL data object contains the properties and they are text or string values
    51  			textProperties := map[string]string{}
    52  			schema := in[i].Object().Properties.(map[string]interface{})
    53  			for property, value := range schema {
    54  				if p.containsProperty(property, properties) {
    55  					if valueString, ok := value.(string); ok && len(valueString) > 0 {
    56  						textProperties[property] = valueString
    57  					}
    58  				}
    59  			}
    60  
    61  			certainty := params.GetCertainty()
    62  			limit := params.GetLimit()
    63  			tokensList := []ent.TokenResult{}
    64  
    65  			// for each text property result, call the NER function and add to additional result
    66  			for property, value := range textProperties {
    67  
    68  				if limit != nil && len(tokensList) > *limit {
    69  					break
    70  				}
    71  
    72  				tokens, err := p.ner.GetTokens(ctx, property, value)
    73  				if err != nil {
    74  					return in, err
    75  				}
    76  
    77  				tokens = cutOffByCertainty(tokens, certainty)
    78  
    79  				tokensList = append(tokensList, tokens...)
    80  			}
    81  
    82  			if limit != nil && len(tokensList) > *limit {
    83  				ap["tokens"] = tokensList[:*limit]
    84  			} else {
    85  				ap["tokens"] = tokensList
    86  			}
    87  
    88  			in[i].AdditionalProperties = ap
    89  		}
    90  	}
    91  	return in, nil
    92  }
    93  
    94  func cutOffByCertainty(tokens []ent.TokenResult, certainty *float64) []ent.TokenResult {
    95  	minCertainty := 0.0
    96  	if certainty != nil {
    97  		minCertainty = *certainty
    98  	}
    99  	a := 0
   100  	for _, x := range tokens {
   101  		if x.Certainty >= minCertainty {
   102  			tokens[a] = x
   103  			a++
   104  		}
   105  	}
   106  	tokens = tokens[:a]
   107  
   108  	return tokens
   109  }
   110  
   111  func (p *TokenProvider) containsProperty(property string, properties []string) bool {
   112  	if len(properties) == 0 {
   113  		return true
   114  	}
   115  	for i := range properties {
   116  		if properties[i] == property {
   117  			return true
   118  		}
   119  	}
   120  	return false
   121  }