2017-01-11 15:56:32 +03:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
from __future__ import unicode_literals
|
2015-06-07 18:53:14 +03:00
|
|
|
|
|
2017-01-13 04:23:50 +03:00
|
|
|
|
from io import StringIO, BytesIO
|
2017-01-13 01:38:47 +03:00
|
|
|
|
from pathlib import Path
|
2017-01-11 15:56:32 +03:00
|
|
|
|
import pytest
|
|
|
|
|
|
2017-05-29 23:14:31 +03:00
|
|
|
|
from .util import load_test_model
|
|
|
|
|
from ..tokens import Doc
|
|
|
|
|
from ..strings import StringStore
|
|
|
|
|
from .. import util
|
|
|
|
|
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
2017-10-14 14:11:18 +03:00
|
|
|
|
# These languages are used for generic tokenizer tests – only add a language
|
|
|
|
|
# here if it's using spaCy's tokenizer (not a different library)
|
|
|
|
|
# TODO: re-implement generic tokenizer tests
|
2017-10-31 17:50:13 +03:00
|
|
|
|
_languages = ['bn', 'da', 'de', 'en', 'es', 'fi', 'fr', 'ga', 'he', 'hu', 'id',
|
2018-06-22 12:14:03 +03:00
|
|
|
|
'it', 'nb', 'nl', 'pl', 'pt', 'ro', 'ru', 'sv', 'tr', 'ar', 'ut', 'tt',
|
2018-06-19 11:17:53 +03:00
|
|
|
|
'xx']
|
2018-05-15 01:27:19 +03:00
|
|
|
|
|
2017-09-04 16:19:18 +03:00
|
|
|
|
_models = {'en': ['en_core_web_sm'],
|
2018-05-30 15:10:20 +03:00
|
|
|
|
'de': ['de_core_news_sm'],
|
2017-11-02 01:02:00 +03:00
|
|
|
|
'fr': ['fr_core_news_sm'],
|
2018-05-30 15:10:20 +03:00
|
|
|
|
'xx': ['xx_ent_web_sm'],
|
2018-03-28 17:02:59 +03:00
|
|
|
|
'en_core_web_md': ['en_core_web_md'],
|
|
|
|
|
'es_core_news_md': ['es_core_news_md']}
|
2017-05-29 23:14:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# only used for tests that require loading the models
|
|
|
|
|
# in all other cases, use specific instances
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture(params=_models['en'])
|
2017-05-29 23:14:31 +03:00
|
|
|
|
def EN(request):
|
|
|
|
|
return load_test_model(request.param)
|
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture(params=_models['de'])
|
2017-05-29 23:14:31 +03:00
|
|
|
|
def DE(request):
|
|
|
|
|
return load_test_model(request.param)
|
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture(params=_models['fr'])
|
2017-05-29 23:14:31 +03:00
|
|
|
|
def FR(request):
|
|
|
|
|
return load_test_model(request.param)
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
|
|
|
|
|
2017-11-21 22:23:59 +03:00
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def RU(request):
|
2017-11-26 20:54:48 +03:00
|
|
|
|
pymorphy = pytest.importorskip('pymorphy2')
|
2017-11-30 12:29:38 +03:00
|
|
|
|
return util.get_lang_class('ru')()
|
2017-11-21 22:23:59 +03:00
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:26:13 +03:00
|
|
|
|
#@pytest.fixture(params=_languages)
|
|
|
|
|
#def tokenizer(request):
|
2018-05-15 01:27:19 +03:00
|
|
|
|
#lang = util.get_lang_class(request.param)
|
|
|
|
|
#return lang.Defaults.create_tokenizer()
|
2017-06-05 03:26:13 +03:00
|
|
|
|
|
2017-10-14 14:11:18 +03:00
|
|
|
|
|
2017-06-05 03:26:13 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def tokenizer():
|
|
|
|
|
return util.get_lang_class('xx').Defaults.create_tokenizer()
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-01-11 15:56:32 +03:00
|
|
|
|
def en_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('en').Defaults.create_tokenizer()
|
2015-06-07 18:53:14 +03:00
|
|
|
|
|
2016-09-26 12:57:54 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-01-11 15:56:32 +03:00
|
|
|
|
def en_vocab():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('en').Defaults.create_vocab()
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-10-07 01:38:51 +03:00
|
|
|
|
def en_parser(en_vocab):
|
|
|
|
|
nlp = util.get_lang_class('en')(en_vocab)
|
|
|
|
|
return nlp.create_pipe('parser')
|
2017-05-09 01:02:21 +03:00
|
|
|
|
|
2017-01-11 23:29:59 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-04-06 19:48:45 +03:00
|
|
|
|
def es_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('es').Defaults.create_tokenizer()
|
2017-04-06 19:48:45 +03:00
|
|
|
|
|
2017-01-11 23:29:59 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-01-11 15:56:32 +03:00
|
|
|
|
def de_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('de').Defaults.create_tokenizer()
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-01-24 12:55:02 +03:00
|
|
|
|
def fr_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('fr').Defaults.create_tokenizer()
|
2017-01-24 12:55:02 +03:00
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-01-11 15:56:32 +03:00
|
|
|
|
def hu_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('hu').Defaults.create_tokenizer()
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
2017-01-12 18:49:19 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-02-04 14:47:29 +03:00
|
|
|
|
def fi_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('fi').Defaults.create_tokenizer()
|
2017-02-04 14:47:29 +03:00
|
|
|
|
|
|
|
|
|
|
2018-05-12 16:20:04 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def ro_tokenizer():
|
|
|
|
|
return util.get_lang_class('ro').Defaults.create_tokenizer()
|
|
|
|
|
|
|
|
|
|
|
2017-08-20 08:17:14 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def id_tokenizer():
|
|
|
|
|
return util.get_lang_class('id').Defaults.create_tokenizer()
|
2017-02-04 14:47:29 +03:00
|
|
|
|
|
2017-10-14 14:11:18 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-02-04 17:21:34 +03:00
|
|
|
|
def sv_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('sv').Defaults.create_tokenizer()
|
2017-02-04 17:21:34 +03:00
|
|
|
|
|
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-03-05 04:11:26 +03:00
|
|
|
|
def bn_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('bn').Defaults.create_tokenizer()
|
2017-03-05 04:11:26 +03:00
|
|
|
|
|
2017-10-31 17:50:13 +03:00
|
|
|
|
|
2017-09-11 12:31:41 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def ga_tokenizer():
|
|
|
|
|
return util.get_lang_class('ga').Defaults.create_tokenizer()
|
|
|
|
|
|
2017-04-06 19:48:45 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-03-24 18:27:44 +03:00
|
|
|
|
def he_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('he').Defaults.create_tokenizer()
|
2017-03-24 18:27:44 +03:00
|
|
|
|
|
2017-10-14 14:11:18 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-04-27 00:21:41 +03:00
|
|
|
|
def nb_tokenizer():
|
2017-05-14 02:31:10 +03:00
|
|
|
|
return util.get_lang_class('nb').Defaults.create_tokenizer()
|
2017-05-09 01:02:21 +03:00
|
|
|
|
|
2017-07-03 16:43:06 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def da_tokenizer():
|
|
|
|
|
return util.get_lang_class('da').Defaults.create_tokenizer()
|
2017-03-24 18:27:44 +03:00
|
|
|
|
|
2017-10-14 14:11:39 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def ja_tokenizer():
|
2018-05-03 19:38:26 +03:00
|
|
|
|
janome = pytest.importorskip("MeCab")
|
2017-10-14 14:11:39 +03:00
|
|
|
|
return util.get_lang_class('ja').Defaults.create_tokenizer()
|
|
|
|
|
|
2017-09-26 17:36:27 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def th_tokenizer():
|
2017-09-26 19:54:15 +03:00
|
|
|
|
pythainlp = pytest.importorskip("pythainlp")
|
2017-09-26 17:36:27 +03:00
|
|
|
|
return util.get_lang_class('th').Defaults.create_tokenizer()
|
|
|
|
|
|
2017-12-01 17:04:32 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def tr_tokenizer():
|
|
|
|
|
return util.get_lang_class('tr').Defaults.create_tokenizer()
|
|
|
|
|
|
2018-06-19 11:17:53 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def tt_tokenizer():
|
|
|
|
|
return util.get_lang_class('tt').Defaults.create_tokenizer()
|
|
|
|
|
|
2018-05-15 01:27:19 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def ar_tokenizer():
|
|
|
|
|
return util.get_lang_class('ar').Defaults.create_tokenizer()
|
2017-03-24 18:27:44 +03:00
|
|
|
|
|
2018-06-22 12:14:03 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def ur_tokenizer():
|
|
|
|
|
return util.get_lang_class('ur').Defaults.create_tokenizer()
|
|
|
|
|
|
2017-11-21 22:23:59 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def ru_tokenizer():
|
2017-11-26 21:04:44 +03:00
|
|
|
|
pymorphy = pytest.importorskip('pymorphy2')
|
2017-11-21 22:23:59 +03:00
|
|
|
|
return util.get_lang_class('ru').Defaults.create_tokenizer()
|
|
|
|
|
|
|
|
|
|
|
2017-01-12 17:05:40 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def stringstore():
|
|
|
|
|
return StringStore()
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
2017-01-12 18:49:19 +03:00
|
|
|
|
|
2017-06-05 03:09:27 +03:00
|
|
|
|
@pytest.fixture
|
2017-01-12 23:56:32 +03:00
|
|
|
|
def en_entityrecognizer():
|
2018-05-15 01:27:19 +03:00
|
|
|
|
return util.get_lang_class('en').Defaults.create_entity()
|
2017-01-12 23:56:32 +03:00
|
|
|
|
|
|
|
|
|
|
2017-01-11 15:56:32 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def text_file():
|
|
|
|
|
return StringIO()
|
|
|
|
|
|
2017-10-14 14:11:18 +03:00
|
|
|
|
|
2017-01-13 04:23:50 +03:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def text_file_b():
|
|
|
|
|
return BytesIO()
|
|
|
|
|
|
2017-01-11 15:56:32 +03:00
|
|
|
|
|
2015-07-23 02:19:03 +03:00
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
|
parser.addoption("--models", action="store_true",
|
2018-05-15 01:27:19 +03:00
|
|
|
|
help="include tests that require full models")
|
2015-07-23 02:19:03 +03:00
|
|
|
|
parser.addoption("--vectors", action="store_true",
|
2018-05-15 01:27:19 +03:00
|
|
|
|
help="include word vectors tests")
|
2015-07-23 02:19:03 +03:00
|
|
|
|
parser.addoption("--slow", action="store_true",
|
2018-05-15 01:27:19 +03:00
|
|
|
|
help="include slow tests")
|
2015-07-23 02:19:03 +03:00
|
|
|
|
|
2017-05-29 23:10:16 +03:00
|
|
|
|
for lang in _languages + ['all']:
|
|
|
|
|
parser.addoption("--%s" % lang, action="store_true", help="Use %s models" % lang)
|
2018-03-28 17:02:59 +03:00
|
|
|
|
for model in _models:
|
|
|
|
|
if model not in _languages:
|
|
|
|
|
parser.addoption("--%s" % model, action="store_true", help="Use %s model" % model)
|
2017-05-29 23:10:16 +03:00
|
|
|
|
|
2015-07-23 02:19:03 +03:00
|
|
|
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
|
for opt in ['models', 'vectors', 'slow']:
|
|
|
|
|
if opt in item.keywords and not item.config.getoption("--%s" % opt):
|
|
|
|
|
pytest.skip("need --%s option to run" % opt)
|
2017-05-29 23:10:16 +03:00
|
|
|
|
|
|
|
|
|
# Check if test is marked with models and has arguments set, i.e. specific
|
|
|
|
|
# language. If so, skip test if flag not set.
|
|
|
|
|
if item.get_marker('models'):
|
|
|
|
|
for arg in item.get_marker('models').args:
|
|
|
|
|
if not item.config.getoption("--%s" % arg) and not item.config.getoption("--all"):
|
2017-06-05 03:09:27 +03:00
|
|
|
|
pytest.skip("need --%s or --all option to run" % arg)
|