Skip to content

Commit

Permalink
Catch ValueError by Voikko.analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Nov 2, 2023
1 parent ff1d32c commit 5ca8144
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion annif/analyzer/voikko.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import voikko.libvoikko

from annif.exception import OperationFailedException

from . import analyzer


Expand All @@ -27,7 +29,13 @@ def __getstate__(self) -> dict[str, str | None]:
def _normalize_word(self, word: str) -> str:
if self.voikko is None:
self.voikko = voikko.libvoikko.Voikko(self.param)
result = self.voikko.analyze(word)
try:
result = self.voikko.analyze(word)
except ValueError as err:
raise OperationFailedException(
f"Voikko error in analysis of word '{word}'"
) from err

if len(result) > 0 and "BASEFORM" in result[0]:
return result[0]["BASEFORM"]
return word
15 changes: 15 additions & 0 deletions tests/test_analyzer_voikko.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Unit tests for voikko analyzer in Annif"""

from unittest import mock

import pytest

import annif.analyzer
from annif.exception import OperationFailedException

voikko = pytest.importorskip("annif.analyzer.voikko")

Expand All @@ -18,3 +21,15 @@ def test_voikko_finnish_analyzer_normalize_word():
assert analyzer._normalize_word("xyzzy") == "xyzzy"
assert analyzer._normalize_word("vanhat") == "vanha"
assert analyzer._normalize_word("koirien") == "koira"


def test_voikko_analyze_valueerror():
analyzer = annif.analyzer.get_analyzer("voikko(fi)")
with mock.patch(
"voikko.libvoikko.Voikko.analyze",
side_effect=ValueError,
):
with pytest.raises(
OperationFailedException, match="Voikko error in analysis of word 'kissa'"
):
assert analyzer._normalize_word("kissa")

0 comments on commit 5ca8144

Please sign in to comment.