mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-01 09:49:03 +08:00
fix(translate): improve auto translate language detection (#9375)
fix(translate): 调整语言检测阈值并增加回退逻辑 当文本较短时使用LLM检测语言,较长时优先使用franc检测 当franc检测失败时回退到LLM检测 同时将LLM检测的文本长度限制从50提高到100
This commit is contained in:
parent
4dabc214f2
commit
4615e97ad5
@ -29,7 +29,15 @@ export const detectLanguage = async (inputText: string): Promise<TranslateLangua
|
||||
switch (method) {
|
||||
case 'auto':
|
||||
// hard encoded threshold
|
||||
result = estimateTextTokens(text) < 50 ? await detectLanguageByLLM(text) : detectLanguageByFranc(text)
|
||||
if (estimateTextTokens(text) < 100) {
|
||||
result = await detectLanguageByLLM(text)
|
||||
} else {
|
||||
result = detectLanguageByFranc(text)
|
||||
// fallback to llm when franc fails
|
||||
if (result === UNKNOWN.langCode) {
|
||||
result = await detectLanguageByLLM(text)
|
||||
}
|
||||
}
|
||||
break
|
||||
case 'franc':
|
||||
result = detectLanguageByFranc(text)
|
||||
@ -48,7 +56,7 @@ const detectLanguageByLLM = async (inputText: string): Promise<TranslateLanguage
|
||||
logger.info('Detect langugage by llm')
|
||||
let detectedLang = ''
|
||||
await fetchLanguageDetection({
|
||||
text: sliceByTokens(inputText, 0, 50),
|
||||
text: sliceByTokens(inputText, 0, 100),
|
||||
onResponse: (text) => {
|
||||
detectedLang = text.replace(/^\s*\n+/g, '')
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user