mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2025-04-22 23:09:16 +00:00
Catch cant detect errors in lang detection
This commit is contained in:
parent
cfb1a92170
commit
97775d1ca4
1 changed files with 13 additions and 6 deletions
|
@ -3,7 +3,8 @@ from langdetect import DetectorFactory
|
||||||
|
|
||||||
DetectorFactory.seed = 0
|
DetectorFactory.seed = 0
|
||||||
|
|
||||||
from langdetect import detect_langs
|
from langdetect import detect_langs, LangDetectException
|
||||||
|
from langdetect.lang_detect_exception import ErrorCode
|
||||||
from lexilang.detector import detect as lldetect
|
from lexilang.detector import detect as lldetect
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,11 +36,17 @@ class Detector:
|
||||||
if conf > 0:
|
if conf > 0:
|
||||||
return [Language(code, round(conf * 100))]
|
return [Language(code, round(conf * 100))]
|
||||||
|
|
||||||
|
try:
|
||||||
top_3_choices = [lang for lang in detect_langs(text) if check_lang(self.langcodes, lang)][:3]
|
top_3_choices = [lang for lang in detect_langs(text) if check_lang(self.langcodes, lang)][:3]
|
||||||
if not len(top_3_choices):
|
if not len(top_3_choices):
|
||||||
return [Language("en", 0)]
|
return [Language("en", 0)]
|
||||||
if top_3_choices[0].prob == 0:
|
if top_3_choices[0].prob == 0:
|
||||||
return [Language("en", 0)]
|
return [Language("en", 0)]
|
||||||
|
except LangDetectException as e:
|
||||||
|
if e.code == ErrorCode.CantDetectError:
|
||||||
|
return [Language("en", 0)]
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
|
||||||
return [Language(normalized_lang_code(lang), round(lang.prob * 100)) for lang in top_3_choices]
|
return [Language(normalized_lang_code(lang), round(lang.prob * 100)) for lang in top_3_choices]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue