Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: display explicit error message when model not downloaded #156

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions nlpretext/token/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from typing import List, Union
from typing import Any, List, Union
import nltk
from sacremoses import MosesTokenizer, MosesDetokenizer
import spacy

class LanguageNotHandled(Exception):
pass


class LanguageNotInstalledError(Exception):
pass


class SpacyModel:
class SingletonSpacyModel:
def __init__(self, lang):
def __init__(self, lang: str) -> None:
self.lang = lang
if lang == 'en':
self.model = spacy.load('en_core_web_sm')
self.model = _load_spacy_model('en_core_web_sm')
elif lang == 'fr':
self.model = spacy.load('fr_core_news_sm')
self.model = _load_spacy_model('fr_core_news_sm')
elif lang == 'ko':
self.model = spacy.blank('ko')
elif lang == 'ja':
Expand All @@ -48,6 +53,18 @@ def get_lang_model(self):
return self.model.lang


def _load_spacy_model(model: str) -> Any:
try:
return spacy.load(model)
except OSError:
raise LanguageNotInstalledError(
(
f'Model {model} is not installed. '
f'To install, run: python -m spacy download {model}'
)
)


def _get_spacy_tokenizer(lang: str):
"""
Function that gets the right tokenizer given the language
Expand Down
22 changes: 22 additions & 0 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
import spacy
from nlpretext.token.tokenizer import SpacyModel, LanguageNotInstalledError

@pytest.mark.parametrize(
"fake_input, expected_model_in_message",
[
("en", "en_core_web_sm"),
("fr", "fr_core_news_sm")
]
)
def test_get_spacy_tokenizer_when_model_not_downloaded(monkeypatch, fake_input, expected_model_in_message):

def mock_spacy_load(lang):
raise OSError(
"[E050] Can't find model 'en_core_web_sm'. It doesn't seem to be ..."
)

monkeypatch.setattr(spacy, "load", mock_spacy_load)
with pytest.raises(LanguageNotInstalledError) as e:
SpacyModel.SingletonSpacyModel(fake_input)
assert expected_model_in_message in str(e.value)