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