From 9184c382db8f0331530e946cd3baa5b385f13528 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 16 Jul 2024 16:03:22 -0400 Subject: [PATCH] Fix div by zero error --- libretranslate/language.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretranslate/language.py b/libretranslate/language.py index 407f695..0709cad 100644 --- a/libretranslate/language.py +++ b/libretranslate/language.py @@ -72,7 +72,7 @@ def detect_languages(text): # sort the candidates descending based on the detected confidence candidates.sort( - key=lambda l: (l.confidence * l.text_length) / text_length_total, reverse=True + key=lambda l: 0 if text_length_total == 0 else (l.confidence * l.text_length) / text_length_total, reverse=True ) return [{"confidence": l.confidence, "language": l.code} for l in candidates]