tercul-backend/linguistics/factory.go
Damir Mukimov fa336cacf3
wip
2025-09-01 00:43:59 +02:00

109 lines
3.0 KiB
Go

package linguistics
import (
"tercul/internal/platform/cache"
"tercul/internal/platform/config"
"gorm.io/gorm"
)
// LinguisticsFactory provides easy access to all linguistics components
type LinguisticsFactory struct {
textAnalyzer TextAnalyzer
analysisCache AnalysisCache
analysisRepo AnalysisRepository
workAnalysisService WorkAnalysisService
analyzer Analyzer
}
// NewLinguisticsFactory creates a new LinguisticsFactory with all components
func NewLinguisticsFactory(
db *gorm.DB,
cache cache.Cache,
concurrency int,
cacheEnabled bool,
) *LinguisticsFactory {
// Create text analyzer and wire providers (prefer external libs when available)
textAnalyzer := NewBasicTextAnalyzer()
// Wire sentiment provider: GoVADER (configurable)
if config.Cfg.NLPUseVADER {
if sp, err := NewGoVADERSentimentProvider(); err == nil {
textAnalyzer = textAnalyzer.WithSentimentProvider(sp)
} else {
textAnalyzer = textAnalyzer.WithSentimentProvider(RuleBasedSentimentProvider{})
}
} else {
textAnalyzer = textAnalyzer.WithSentimentProvider(RuleBasedSentimentProvider{})
}
// Wire language detector: lingua-go (configurable)
if config.Cfg.NLPUseLingua {
textAnalyzer = textAnalyzer.WithLanguageDetector(NewLinguaLanguageDetector())
}
// Wire keyword provider: lightweight TF-IDF approximation (configurable)
if config.Cfg.NLPUseTFIDF {
textAnalyzer = textAnalyzer.WithKeywordProvider(NewTFIDFKeywordProvider())
}
// Create cache components
memoryCache := NewMemoryAnalysisCache(cacheEnabled)
redisCache := NewRedisAnalysisCache(cache, cacheEnabled)
analysisCache := NewCompositeAnalysisCache(memoryCache, redisCache, cacheEnabled)
// Create repository
analysisRepo := NewGORMAnalysisRepository(db)
// Create work analysis service
workAnalysisService := NewWorkAnalysisService(
textAnalyzer,
analysisCache,
analysisRepo,
concurrency,
cacheEnabled,
)
// Create analyzer that combines text analysis and work analysis
analyzer := NewBasicAnalyzer(
textAnalyzer,
workAnalysisService,
cache,
concurrency,
cacheEnabled,
)
return &LinguisticsFactory{
textAnalyzer: textAnalyzer,
analysisCache: analysisCache,
analysisRepo: analysisRepo,
workAnalysisService: workAnalysisService,
analyzer: analyzer,
}
}
// GetTextAnalyzer returns the text analyzer
func (f *LinguisticsFactory) GetTextAnalyzer() TextAnalyzer {
return f.textAnalyzer
}
// GetAnalysisCache returns the analysis cache
func (f *LinguisticsFactory) GetAnalysisCache() AnalysisCache {
return f.analysisCache
}
// GetAnalysisRepository returns the analysis repository
func (f *LinguisticsFactory) GetAnalysisRepository() AnalysisRepository {
return f.analysisRepo
}
// GetWorkAnalysisService returns the work analysis service
func (f *LinguisticsFactory) GetWorkAnalysisService() WorkAnalysisService {
return f.workAnalysisService
}
// GetAnalyzer returns the analyzer
func (f *LinguisticsFactory) GetAnalyzer() Analyzer {
return f.analyzer
}