github.com/weaviate/weaviate@v1.24.6/modules/text-spellcheck/module.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 modspellcheck
    13  
    14  import (
    15  	"context"
    16  	"net/http"
    17  	"os"
    18  	"time"
    19  
    20  	"github.com/pkg/errors"
    21  	"github.com/weaviate/weaviate/entities/modulecapabilities"
    22  	"github.com/weaviate/weaviate/entities/moduletools"
    23  	spellcheckadditional "github.com/weaviate/weaviate/modules/text-spellcheck/additional"
    24  	spellcheckadditionalspellcheck "github.com/weaviate/weaviate/modules/text-spellcheck/additional/spellcheck"
    25  	"github.com/weaviate/weaviate/modules/text-spellcheck/clients"
    26  	"github.com/weaviate/weaviate/modules/text-spellcheck/ent"
    27  	spellchecktexttransformer "github.com/weaviate/weaviate/modules/text-spellcheck/transformer"
    28  	spellchecktexttransformerautocorrect "github.com/weaviate/weaviate/modules/text-spellcheck/transformer/autocorrect"
    29  )
    30  
    31  func New() *SpellCheckModule {
    32  	return &SpellCheckModule{}
    33  }
    34  
    35  type SpellCheckModule struct {
    36  	spellCheck                   spellCheckClient
    37  	additionalPropertiesProvider modulecapabilities.AdditionalProperties
    38  	textTransformersProvider     modulecapabilities.TextTransformers
    39  }
    40  
    41  type spellCheckClient interface {
    42  	Check(ctx context.Context, text []string) (*ent.SpellCheckResult, error)
    43  	MetaInfo() (map[string]interface{}, error)
    44  }
    45  
    46  func (m *SpellCheckModule) Name() string {
    47  	return "text-spellcheck"
    48  }
    49  
    50  func (m *SpellCheckModule) Type() modulecapabilities.ModuleType {
    51  	return modulecapabilities.Extension
    52  }
    53  
    54  func (m *SpellCheckModule) Init(ctx context.Context,
    55  	params moduletools.ModuleInitParams,
    56  ) error {
    57  	uri := os.Getenv("SPELLCHECK_INFERENCE_API")
    58  	if uri == "" {
    59  		return errors.Errorf("required variable SPELLCHECK_INFERENCE_API is not set")
    60  	}
    61  
    62  	client := clients.New(uri, params.GetConfig().ModuleHttpClientTimeout, params.GetLogger())
    63  
    64  	if err := client.WaitForStartup(ctx, 1*time.Second); err != nil {
    65  		return errors.Wrap(err, "init remote spell check module")
    66  	}
    67  
    68  	m.spellCheck = client
    69  
    70  	m.initTextTransformers()
    71  	m.initAdditional()
    72  
    73  	return nil
    74  }
    75  
    76  func (m *SpellCheckModule) initTextTransformers() {
    77  	autocorrectProvider := spellchecktexttransformerautocorrect.New(m.spellCheck)
    78  	m.textTransformersProvider = spellchecktexttransformer.New(autocorrectProvider)
    79  }
    80  
    81  func (m *SpellCheckModule) initAdditional() {
    82  	spellCheckProvider := spellcheckadditionalspellcheck.New(m.spellCheck)
    83  	m.additionalPropertiesProvider = spellcheckadditional.New(spellCheckProvider)
    84  }
    85  
    86  func (m *SpellCheckModule) RootHandler() http.Handler {
    87  	// TODO: remove once this is a capability interface
    88  	return nil
    89  }
    90  
    91  func (m *SpellCheckModule) MetaInfo() (map[string]interface{}, error) {
    92  	return m.spellCheck.MetaInfo()
    93  }
    94  
    95  func (m *SpellCheckModule) AdditionalProperties() map[string]modulecapabilities.AdditionalProperty {
    96  	return m.additionalPropertiesProvider.AdditionalProperties()
    97  }
    98  
    99  func (m *SpellCheckModule) TextTransformers() map[string]modulecapabilities.TextTransform {
   100  	return m.textTransformersProvider.TextTransformers()
   101  }
   102  
   103  // verify we implement the modules.Module interface
   104  var (
   105  	_ = modulecapabilities.Module(New())
   106  	_ = modulecapabilities.AdditionalProperties(New())
   107  	_ = modulecapabilities.MetaProvider(New())
   108  	_ = modulecapabilities.TextTransformers(New())
   109  )