Skip to content

Commit

Permalink
in_target_language: avoid ZeroDivisionError (fixes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
adbar committed Oct 5, 2022
1 parent 6102563 commit c87066a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion simplemma/langdetect.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def in_target_language(text: str, lang: Optional[Tuple[str]] = None) -> float:
if candidate is not None:
in_target += 1
break
return in_target / total
if total > 0 and in_target > 0:
return in_target / total
return 0


def _return_default() -> List[Tuple[str, float]]:
Expand Down
1 change: 1 addition & 0 deletions tests/test_langdetect.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_detection():
lang=("cs", "sk"),
) == [("cs", 0.625), ("unk", 0.375), ("sk", 0.125)]
# target language
assert in_target_language("", lang="en") == 0
assert (
in_target_language(
"opera post physica posita (τὰ μετὰ τὰ φυσικά)", lang=("la",)
Expand Down

0 comments on commit c87066a

Please sign in to comment.