From 7ae1f8021baaa3e298ee834a28187c86d6c50a1c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 28 Jul 2024 22:01:24 +0800 Subject: [PATCH] :bug: Try to fix the language detect issue --- pkg/internal/services/languages.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/internal/services/languages.go b/pkg/internal/services/languages.go index 2161625..63581ff 100644 --- a/pkg/internal/services/languages.go +++ b/pkg/internal/services/languages.go @@ -6,12 +6,20 @@ import ( "github.com/pemistahl/lingua-go" ) -func DetectLanguage(content string) string { - return "unknown" +var detector lingua.LanguageDetector - detector := lingua.NewLanguageDetectorBuilder(). - FromLanguages(lingua.AllLanguages()...). +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()) }