tercul-backend/linguistics/adapter_lingua.go
Damir Mukimov 4957117cb6 Initial commit: Tercul Go project with comprehensive architecture
- Core Go application with GraphQL API using gqlgen
- Comprehensive data models for literary works, authors, translations
- Repository pattern with caching layer
- Authentication and authorization system
- Linguistics analysis capabilities with multiple adapters
- Vector search integration with Weaviate
- Docker containerization support
- Python data migration and analysis scripts
- Clean architecture with proper separation of concerns
- Production-ready configuration and middleware
- Proper .gitignore excluding vendor/, database files, and build artifacts
2025-08-13 07:42:32 +02:00

37 lines
1.0 KiB
Go

package linguistics
import (
"strings"
lingua "github.com/pemistahl/lingua-go"
)
// LinguaLanguageDetector implements LanguageDetector using lingua-go
type LinguaLanguageDetector struct {
detector lingua.LanguageDetector
}
// NewLinguaLanguageDetector builds a detector for all supported languages
func NewLinguaLanguageDetector() *LinguaLanguageDetector {
det := lingua.NewLanguageDetectorBuilder().FromAllLanguages().Build()
return &LinguaLanguageDetector{detector: det}
}
// DetectLanguage returns a lowercase ISO 639-1 code if possible
func (l *LinguaLanguageDetector) DetectLanguage(text string) (string, bool) {
lang, ok := l.detector.DetectLanguageOf(text)
if !ok {
return "", false
}
// Prefer ISO 639-1 when available else fallback to ISO 639-3
if s := lang.IsoCode639_1().String(); s != "" {
return s, true
}
if s := lang.IsoCode639_3().String(); s != "" {
return s, true
}
// fallback to language name
return strings.ToLower(lang.String()), true
}