Interactive/pkg/internal/services/languages.go

28 lines
504 B
Go
Raw Normal View History

2024-07-13 15:16:40 +00:00
package services
import (
"strings"
"github.com/pemistahl/lingua-go"
2024-07-13 15:16:40 +00:00
)
var detector lingua.LanguageDetector
func CreateLanguageDetector() lingua.LanguageDetector {
return lingua.NewLanguageDetectorBuilder().
FromAllLanguages().
WithLowAccuracyMode().
Build()
}
func DetectLanguage(content string) string {
if detector == nil {
detector = CreateLanguageDetector()
}
if lang, ok := detector.DetectLanguageOf(content); ok {
return strings.ToLower(lang.String())
2024-07-13 15:16:40 +00:00
}
return "unknown"
}