github.com/weaviate/weaviate@v1.24.6/modules/text-spellcheck/additional/spellcheck/spellcheck_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 spellcheck 13 14 import ( 15 "context" 16 "errors" 17 "fmt" 18 "strings" 19 20 "github.com/weaviate/weaviate/entities/models" 21 "github.com/weaviate/weaviate/entities/search" 22 spellcheckmodels "github.com/weaviate/weaviate/modules/text-spellcheck/additional/models" 23 "github.com/weaviate/weaviate/modules/text-spellcheck/ent" 24 ) 25 26 func (p *SpellCheckProvider) findSpellCheck(ctx context.Context, 27 in []search.Result, params *Params, limit *int, 28 argumentModuleParams map[string]interface{}, 29 ) ([]search.Result, error) { 30 if len(in) > 0 { 31 name, texts, err := p.paramHelper.getTexts(argumentModuleParams) 32 if err != nil { 33 return in, errors.New("cannot get texts") 34 } 35 spellCheckAdditionalProperty, err := p.performSpellCheck(ctx, name, texts) 36 if err != nil { 37 return in, err 38 } 39 for i := range in { 40 ap := in[i].AdditionalProperties 41 if ap == nil { 42 ap = models.AdditionalProperties{} 43 } 44 ap["spellCheck"] = spellCheckAdditionalProperty 45 in[i].AdditionalProperties = ap 46 } 47 } 48 return in, nil 49 } 50 51 func (p *SpellCheckProvider) performSpellCheck(ctx context.Context, name string, texts []string) ([]*spellcheckmodels.SpellCheck, error) { 52 if len(texts) == 0 { 53 return []*spellcheckmodels.SpellCheck{}, nil 54 } 55 spellCheckResult, err := p.spellCheck.Check(ctx, texts) 56 if err != nil { 57 return nil, err 58 } 59 return p.getSpellCheckAdditionalProperty(name, spellCheckResult), nil 60 } 61 62 func (p *SpellCheckProvider) getSpellCheckAdditionalProperty(name string, spellCheckResult *ent.SpellCheckResult) []*spellcheckmodels.SpellCheck { 63 spellCheck := []*spellcheckmodels.SpellCheck{} 64 for i, t := range spellCheckResult.Text { 65 spellCheck = append(spellCheck, p.getSpellCheckAdditionalPropertyObject(t, p.getSpellCheckLocation(name, i), spellCheckResult)) 66 } 67 return spellCheck 68 } 69 70 func (p *SpellCheckProvider) getSpellCheckLocation(name string, i int) string { 71 if name == "nearText" { 72 return fmt.Sprintf("nearText.concepts[%v]", i) 73 } 74 return "ask.question" 75 } 76 77 func (p *SpellCheckProvider) getSpellCheckAdditionalPropertyObject(originalText, location string, spellCheckResult *ent.SpellCheckResult) *spellcheckmodels.SpellCheck { 78 didYouMean := originalText 79 changes := []spellcheckmodels.SpellCheckChange{} 80 for _, change := range spellCheckResult.Changes { 81 if strings.Contains(strings.ToLower(didYouMean), change.Original) { 82 didYouMean = strings.ReplaceAll(strings.ToLower(didYouMean), change.Original, change.Correction) 83 change := spellcheckmodels.SpellCheckChange{ 84 Original: change.Original, 85 Corrected: change.Correction, 86 } 87 changes = append(changes, change) 88 } 89 } 90 return &spellcheckmodels.SpellCheck{ 91 OriginalText: originalText, 92 DidYouMean: didYouMean, 93 Location: location, 94 NumberOfCorrections: len(changes), 95 Changes: changes, 96 } 97 }