Fix div by zero error

This commit is contained in:
Piero Toffanin 2024-07-16 16:03:22 -04:00
parent 8ba054c786
commit 9184c382db

View file

@ -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]