Skip to content

Commit

Permalink
Show friendlier message if loading spaCy model fails
Browse files Browse the repository at this point in the history
  • Loading branch information
osma committed Jan 20, 2022
1 parent 3753170 commit 8ad7445
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion annif/analyzer/spacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import spacy
from . import analyzer
from annif.exception import OperationFailedException
import annif.util

_KEY_LOWERCASE = 'lowercase'
Expand All @@ -12,7 +13,12 @@ class SpacyAnalyzer(analyzer.Analyzer):

def __init__(self, param, **kwargs):
self.param = param
self.nlp = spacy.load(param, exclude=['ner', 'parser'])
try:
self.nlp = spacy.load(param, exclude=['ner', 'parser'])
except IOError as err:
raise OperationFailedException(
f"Loading spaCy model '{param}' failed - " +
f"please download the model.\n{err}")
if _KEY_LOWERCASE in kwargs:
self.lowercase = annif.util.boolean(kwargs[_KEY_LOWERCASE])
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_analyzer_spacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import pytest
import annif.analyzer
from annif.exception import OperationFailedException

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


def test_spacy_model_not_found():
with pytest.raises(OperationFailedException) as excinfo:
annif.analyzer.get_analyzer("spacy(not_found)")
assert "Loading spaCy model 'not_found' failed" in str(excinfo.value)


def test_spacy_english_tokenize_words():
analyzer = annif.analyzer.get_analyzer("spacy(en_core_web_sm)")
words = analyzer.tokenize_words("""
Expand Down

0 comments on commit 8ad7445

Please sign in to comment.