2019-10-08 13:20:55 +03:00
|
|
|
import itertools
|
2021-01-29 03:51:21 +03:00
|
|
|
import logging
|
2024-02-12 16:39:38 +03:00
|
|
|
import warnings
|
2021-01-29 03:51:21 +03:00
|
|
|
from unittest import mock
|
2023-06-26 12:41:03 +03:00
|
|
|
|
2019-07-27 18:30:18 +03:00
|
|
|
import pytest
|
2023-06-26 12:41:03 +03:00
|
|
|
from thinc.api import Config, CupyOps, NumpyOps, get_array_module, get_current_ops
|
|
|
|
|
|
|
|
import spacy
|
|
|
|
from spacy.lang.de import German
|
|
|
|
from spacy.lang.en import English
|
2019-10-08 13:20:55 +03:00
|
|
|
from spacy.language import Language
|
2023-01-09 13:43:48 +03:00
|
|
|
from spacy.scorer import Scorer
|
2019-10-08 13:20:55 +03:00
|
|
|
from spacy.tokens import Doc, Span
|
2020-09-09 11:31:03 +03:00
|
|
|
from spacy.training import Example
|
2023-06-26 12:41:03 +03:00
|
|
|
from spacy.util import (
|
|
|
|
find_matching_language,
|
|
|
|
ignore_error,
|
|
|
|
load_model_from_config,
|
|
|
|
raise_error,
|
|
|
|
registry,
|
|
|
|
)
|
|
|
|
from spacy.vocab import Vocab
|
2019-10-08 13:20:55 +03:00
|
|
|
|
|
|
|
from .util import add_vecs_to_vocab, assert_docs_equal
|
2019-07-27 18:30:18 +03:00
|
|
|
|
2021-10-21 17:14:23 +03:00
|
|
|
try:
|
|
|
|
import torch
|
|
|
|
|
|
|
|
# Ensure that we don't deadlock in multiprocessing tests.
|
|
|
|
torch.set_num_threads(1)
|
|
|
|
torch.set_num_interop_threads(1)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2023-02-03 17:22:25 +03:00
|
|
|
TAGGER_CFG_STRING = """
|
|
|
|
[nlp]
|
|
|
|
lang = "en"
|
|
|
|
pipeline = ["tok2vec","tagger"]
|
|
|
|
|
|
|
|
[components]
|
|
|
|
|
|
|
|
[components.tagger]
|
|
|
|
factory = "tagger"
|
|
|
|
|
|
|
|
[components.tagger.model]
|
|
|
|
@architectures = "spacy.Tagger.v2"
|
|
|
|
nO = null
|
|
|
|
|
|
|
|
[components.tagger.model.tok2vec]
|
|
|
|
@architectures = "spacy.Tok2VecListener.v1"
|
|
|
|
width = ${components.tok2vec.model.encode.width}
|
|
|
|
|
|
|
|
[components.tok2vec]
|
|
|
|
factory = "tok2vec"
|
|
|
|
|
|
|
|
[components.tok2vec.model]
|
|
|
|
@architectures = "spacy.Tok2Vec.v2"
|
|
|
|
|
|
|
|
[components.tok2vec.model.embed]
|
|
|
|
@architectures = "spacy.MultiHashEmbed.v1"
|
|
|
|
width = ${components.tok2vec.model.encode.width}
|
|
|
|
rows = [2000, 1000, 1000, 1000]
|
|
|
|
attrs = ["NORM", "PREFIX", "SUFFIX", "SHAPE"]
|
|
|
|
include_static_vectors = false
|
|
|
|
|
|
|
|
[components.tok2vec.model.encode]
|
|
|
|
@architectures = "spacy.MaxoutWindowEncoder.v2"
|
|
|
|
width = 96
|
|
|
|
depth = 4
|
|
|
|
window_size = 1
|
|
|
|
maxout_pieces = 3
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
TAGGER_TRAIN_DATA = [
|
|
|
|
("I like green eggs", {"tags": ["N", "V", "J", "N"]}),
|
|
|
|
("Eat blue ham", {"tags": ["V", "J", "N"]}),
|
|
|
|
]
|
|
|
|
|
2021-10-21 17:14:23 +03:00
|
|
|
|
2023-01-30 14:44:11 +03:00
|
|
|
TAGGER_TRAIN_DATA = [
|
|
|
|
("I like green eggs", {"tags": ["N", "V", "J", "N"]}),
|
|
|
|
("Eat blue ham", {"tags": ["V", "J", "N"]}),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
def evil_component(doc):
|
|
|
|
if "2" in doc.text:
|
|
|
|
raise ValueError("no dice")
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def perhaps_set_sentences(doc):
|
|
|
|
if not doc.text.startswith("4"):
|
|
|
|
doc[-1].is_sent_start = True
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def assert_sents_error(doc):
|
|
|
|
if not doc.has_annotation("SENT_START"):
|
|
|
|
raise ValueError("no sents")
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def warn_error(proc_name, proc, docs, e):
|
|
|
|
logger = logging.getLogger("spacy")
|
2023-02-02 13:15:22 +03:00
|
|
|
logger.warning("Trouble with component %s.", proc_name)
|
2021-05-17 14:28:39 +03:00
|
|
|
|
|
|
|
|
2019-07-27 18:30:18 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def nlp():
|
|
|
|
nlp = Language(Vocab())
|
2020-07-22 14:42:59 +03:00
|
|
|
textcat = nlp.add_pipe("textcat")
|
2019-07-27 18:30:18 +03:00
|
|
|
for label in ("POSITIVE", "NEGATIVE"):
|
|
|
|
textcat.add_label(label)
|
2020-09-28 22:35:09 +03:00
|
|
|
nlp.initialize()
|
2019-07-27 18:30:18 +03:00
|
|
|
return nlp
|
|
|
|
|
|
|
|
|
|
|
|
def test_language_update(nlp):
|
|
|
|
text = "hello world"
|
|
|
|
annots = {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}
|
2019-08-06 12:01:25 +03:00
|
|
|
wrongkeyannots = {"LABEL": True}
|
2019-07-27 18:30:18 +03:00
|
|
|
doc = Doc(nlp.vocab, words=text.split(" "))
|
2020-07-06 14:02:36 +03:00
|
|
|
example = Example.from_dict(doc, annots)
|
|
|
|
nlp.update([example])
|
|
|
|
|
|
|
|
# Not allowed to call with just one Example
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
nlp.update(example)
|
|
|
|
|
|
|
|
# Update with text and dict: not supported anymore since v.3
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
nlp.update((text, annots))
|
2019-07-27 18:30:18 +03:00
|
|
|
# Update with doc object and dict
|
2020-07-06 14:02:36 +03:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
nlp.update((doc, annots))
|
|
|
|
|
|
|
|
# Create examples badly
|
2019-08-06 12:01:25 +03:00
|
|
|
with pytest.raises(ValueError):
|
2020-07-06 14:02:36 +03:00
|
|
|
example = Example.from_dict(doc, None)
|
2020-06-26 20:34:12 +03:00
|
|
|
with pytest.raises(KeyError):
|
2020-07-06 14:02:36 +03:00
|
|
|
example = Example.from_dict(doc, wrongkeyannots)
|
2019-07-27 18:30:18 +03:00
|
|
|
|
|
|
|
|
2023-02-03 17:22:25 +03:00
|
|
|
def test_language_update_updates():
|
|
|
|
config = Config().from_str(TAGGER_CFG_STRING)
|
|
|
|
nlp = load_model_from_config(config, auto_fill=True, validate=True)
|
|
|
|
|
|
|
|
train_examples = []
|
|
|
|
for t in TAGGER_TRAIN_DATA:
|
|
|
|
train_examples.append(Example.from_dict(nlp.make_doc(t[0]), t[1]))
|
|
|
|
|
|
|
|
optimizer = nlp.initialize(get_examples=lambda: train_examples)
|
|
|
|
|
|
|
|
docs_before_update = list(nlp.pipe([eg.predicted.copy() for eg in train_examples]))
|
|
|
|
nlp.update(train_examples, sgd=optimizer)
|
|
|
|
docs_after_update = list(nlp.pipe([eg.predicted.copy() for eg in train_examples]))
|
|
|
|
|
|
|
|
xp = get_array_module(docs_after_update[0].tensor)
|
|
|
|
assert xp.any(
|
|
|
|
xp.not_equal(docs_before_update[0].tensor, docs_after_update[0].tensor)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-30 10:30:42 +03:00
|
|
|
def test_language_update_does_not_update_with_sgd_false():
|
|
|
|
config = Config().from_str(TAGGER_CFG_STRING)
|
|
|
|
nlp = load_model_from_config(config, auto_fill=True, validate=True)
|
|
|
|
|
|
|
|
train_examples = []
|
|
|
|
for t in TAGGER_TRAIN_DATA:
|
|
|
|
train_examples.append(Example.from_dict(nlp.make_doc(t[0]), t[1]))
|
|
|
|
|
|
|
|
nlp.initialize(get_examples=lambda: train_examples)
|
|
|
|
|
|
|
|
docs_before_update = list(nlp.pipe([eg.predicted.copy() for eg in train_examples]))
|
|
|
|
nlp.update(train_examples, sgd=False)
|
|
|
|
docs_after_update = list(nlp.pipe([eg.predicted.copy() for eg in train_examples]))
|
|
|
|
|
|
|
|
xp = get_array_module(docs_after_update[0].tensor)
|
|
|
|
xp.testing.assert_equal(docs_before_update[0].tensor, docs_after_update[0].tensor)
|
|
|
|
|
|
|
|
|
2019-07-27 18:30:18 +03:00
|
|
|
def test_language_evaluate(nlp):
|
|
|
|
text = "hello world"
|
2020-06-26 20:34:12 +03:00
|
|
|
annots = {"doc_annotation": {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}}
|
2019-07-27 18:30:18 +03:00
|
|
|
doc = Doc(nlp.vocab, words=text.split(" "))
|
2020-07-06 14:02:36 +03:00
|
|
|
example = Example.from_dict(doc, annots)
|
2020-12-31 02:45:50 +03:00
|
|
|
scores = nlp.evaluate([example])
|
|
|
|
assert scores["speed"] > 0
|
|
|
|
|
|
|
|
# test with generator
|
|
|
|
scores = nlp.evaluate(eg for eg in [example])
|
|
|
|
assert scores["speed"] > 0
|
2020-07-06 14:02:36 +03:00
|
|
|
|
|
|
|
# Not allowed to call with just one Example
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
nlp.evaluate(example)
|
|
|
|
|
|
|
|
# Evaluate with text and dict: not supported anymore since v.3
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
nlp.evaluate([(text, annots)])
|
2019-07-27 18:30:18 +03:00
|
|
|
# Evaluate with doc object and dict
|
2020-07-06 14:02:36 +03:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
nlp.evaluate([(doc, annots)])
|
|
|
|
with pytest.raises(TypeError):
|
2020-06-26 20:34:12 +03:00
|
|
|
nlp.evaluate([text, annots])
|
2019-10-08 13:20:55 +03:00
|
|
|
|
|
|
|
|
2019-11-16 22:20:37 +03:00
|
|
|
def test_evaluate_no_pipe(nlp):
|
|
|
|
"""Test that docs are processed correctly within Language.pipe if the
|
|
|
|
component doesn't expose a .pipe method."""
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
@Language.component("test_evaluate_no_pipe")
|
2019-11-16 22:20:37 +03:00
|
|
|
def pipe(doc):
|
|
|
|
return doc
|
|
|
|
|
|
|
|
text = "hello world"
|
|
|
|
annots = {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}
|
|
|
|
nlp = Language(Vocab())
|
2020-07-06 14:02:36 +03:00
|
|
|
doc = nlp(text)
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.add_pipe("test_evaluate_no_pipe")
|
2020-07-06 14:02:36 +03:00
|
|
|
nlp.evaluate([Example.from_dict(doc, annots)])
|
2019-11-16 22:20:37 +03:00
|
|
|
|
|
|
|
|
2023-01-09 13:43:48 +03:00
|
|
|
def test_evaluate_textcat_multilabel(en_vocab):
|
|
|
|
"""Test that evaluate works with a multilabel textcat pipe."""
|
|
|
|
nlp = Language(en_vocab)
|
|
|
|
textcat_multilabel = nlp.add_pipe("textcat_multilabel")
|
|
|
|
for label in ("FEATURE", "REQUEST", "BUG", "QUESTION"):
|
|
|
|
textcat_multilabel.add_label(label)
|
|
|
|
nlp.initialize()
|
|
|
|
|
|
|
|
annots = {"cats": {"FEATURE": 1.0, "QUESTION": 1.0}}
|
|
|
|
doc = nlp.make_doc("hello world")
|
|
|
|
example = Example.from_dict(doc, annots)
|
|
|
|
scores = nlp.evaluate([example])
|
|
|
|
labels = nlp.get_pipe("textcat_multilabel").labels
|
|
|
|
for label in labels:
|
|
|
|
assert scores["cats_f_per_type"].get(label) is not None
|
|
|
|
for key in example.reference.cats.keys():
|
|
|
|
if key not in labels:
|
|
|
|
assert scores["cats_f_per_type"].get(key) is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_evaluate_multiple_textcat_final(en_vocab):
|
|
|
|
"""Test that evaluate evaluates the final textcat component in a pipeline
|
|
|
|
with more than one textcat or textcat_multilabel."""
|
|
|
|
nlp = Language(en_vocab)
|
|
|
|
textcat = nlp.add_pipe("textcat")
|
|
|
|
for label in ("POSITIVE", "NEGATIVE"):
|
|
|
|
textcat.add_label(label)
|
|
|
|
textcat_multilabel = nlp.add_pipe("textcat_multilabel")
|
|
|
|
for label in ("FEATURE", "REQUEST", "BUG", "QUESTION"):
|
|
|
|
textcat_multilabel.add_label(label)
|
|
|
|
nlp.initialize()
|
|
|
|
|
|
|
|
annots = {
|
|
|
|
"cats": {
|
|
|
|
"POSITIVE": 1.0,
|
|
|
|
"NEGATIVE": 0.0,
|
|
|
|
"FEATURE": 1.0,
|
|
|
|
"QUESTION": 1.0,
|
|
|
|
"POSITIVE": 1.0,
|
|
|
|
"NEGATIVE": 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doc = nlp.make_doc("hello world")
|
|
|
|
example = Example.from_dict(doc, annots)
|
|
|
|
scores = nlp.evaluate([example])
|
|
|
|
# get the labels from the final pipe
|
|
|
|
labels = nlp.get_pipe(nlp.pipe_names[-1]).labels
|
|
|
|
for label in labels:
|
|
|
|
assert scores["cats_f_per_type"].get(label) is not None
|
|
|
|
for key in example.reference.cats.keys():
|
|
|
|
if key not in labels:
|
|
|
|
assert scores["cats_f_per_type"].get(key) is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_evaluate_multiple_textcat_separate(en_vocab):
|
|
|
|
"""Test that evaluate can evaluate multiple textcat components separately
|
|
|
|
with custom scorers."""
|
|
|
|
|
|
|
|
def custom_textcat_score(examples, **kwargs):
|
|
|
|
scores = Scorer.score_cats(
|
|
|
|
examples,
|
|
|
|
"cats",
|
|
|
|
multi_label=False,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
return {f"custom_{k}": v for k, v in scores.items()}
|
|
|
|
|
|
|
|
@spacy.registry.scorers("test_custom_textcat_scorer")
|
|
|
|
def make_custom_textcat_scorer():
|
|
|
|
return custom_textcat_score
|
|
|
|
|
|
|
|
nlp = Language(en_vocab)
|
|
|
|
textcat = nlp.add_pipe(
|
|
|
|
"textcat",
|
|
|
|
config={"scorer": {"@scorers": "test_custom_textcat_scorer"}},
|
|
|
|
)
|
|
|
|
for label in ("POSITIVE", "NEGATIVE"):
|
|
|
|
textcat.add_label(label)
|
|
|
|
textcat_multilabel = nlp.add_pipe("textcat_multilabel")
|
|
|
|
for label in ("FEATURE", "REQUEST", "BUG", "QUESTION"):
|
|
|
|
textcat_multilabel.add_label(label)
|
|
|
|
nlp.initialize()
|
|
|
|
|
|
|
|
annots = {
|
|
|
|
"cats": {
|
|
|
|
"POSITIVE": 1.0,
|
|
|
|
"NEGATIVE": 0.0,
|
|
|
|
"FEATURE": 1.0,
|
|
|
|
"QUESTION": 1.0,
|
|
|
|
"POSITIVE": 1.0,
|
|
|
|
"NEGATIVE": 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doc = nlp.make_doc("hello world")
|
|
|
|
example = Example.from_dict(doc, annots)
|
|
|
|
scores = nlp.evaluate([example])
|
|
|
|
# check custom scores for the textcat pipe
|
|
|
|
assert "custom_cats_f_per_type" in scores
|
|
|
|
labels = nlp.get_pipe("textcat").labels
|
|
|
|
assert set(scores["custom_cats_f_per_type"].keys()) == set(labels)
|
|
|
|
# check default scores for the textcat_multilabel pipe
|
|
|
|
assert "cats_f_per_type" in scores
|
|
|
|
labels = nlp.get_pipe("textcat_multilabel").labels
|
|
|
|
assert set(scores["cats_f_per_type"].keys()) == set(labels)
|
|
|
|
|
|
|
|
|
2019-10-08 13:20:55 +03:00
|
|
|
def vector_modification_pipe(doc):
|
|
|
|
doc.vector += 1
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def userdata_pipe(doc):
|
|
|
|
doc.user_data["foo"] = "bar"
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def ner_pipe(doc):
|
|
|
|
span = Span(doc, 0, 1, label="FIRST")
|
|
|
|
doc.ents += (span,)
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def sample_vectors():
|
|
|
|
return [
|
|
|
|
("spacy", [-0.1, -0.2, -0.3]),
|
|
|
|
("world", [-0.2, -0.3, -0.4]),
|
|
|
|
("pipe", [0.7, 0.8, 0.9]),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def nlp2(nlp, sample_vectors):
|
2021-06-28 12:48:00 +03:00
|
|
|
Language.component(
|
|
|
|
"test_language_vector_modification_pipe", func=vector_modification_pipe
|
|
|
|
)
|
2021-05-17 14:28:39 +03:00
|
|
|
Language.component("test_language_userdata_pipe", func=userdata_pipe)
|
|
|
|
Language.component("test_language_ner_pipe", func=ner_pipe)
|
2019-10-08 13:20:55 +03:00
|
|
|
add_vecs_to_vocab(nlp.vocab, sample_vectors)
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.add_pipe("test_language_vector_modification_pipe")
|
|
|
|
nlp.add_pipe("test_language_ner_pipe")
|
|
|
|
nlp.add_pipe("test_language_userdata_pipe")
|
2019-10-08 13:20:55 +03:00
|
|
|
return nlp
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def texts():
|
|
|
|
data = [
|
|
|
|
"Hello world.",
|
|
|
|
"This is spacy.",
|
|
|
|
"You can use multiprocessing with pipe method.",
|
|
|
|
"Please try!",
|
|
|
|
]
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe(nlp2, n_process, texts):
|
2021-04-22 15:58:29 +03:00
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
texts = texts * 10
|
|
|
|
expecteds = [nlp2(text) for text in texts]
|
|
|
|
docs = nlp2.pipe(texts, n_process=n_process, batch_size=2)
|
2019-10-08 13:20:55 +03:00
|
|
|
|
2021-04-22 15:58:29 +03:00
|
|
|
for doc, expected_doc in zip(docs, expecteds):
|
|
|
|
assert_docs_equal(doc, expected_doc)
|
2019-10-08 13:20:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_stream(nlp2, n_process, texts):
|
2021-04-22 15:58:29 +03:00
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
# check if nlp.pipe can handle infinite length iterator properly.
|
|
|
|
stream_texts = itertools.cycle(texts)
|
|
|
|
texts0, texts1 = itertools.tee(stream_texts)
|
|
|
|
expecteds = (nlp2(text) for text in texts0)
|
|
|
|
docs = nlp2.pipe(texts1, n_process=n_process, batch_size=2)
|
|
|
|
|
|
|
|
n_fetch = 20
|
|
|
|
for doc, expected_doc in itertools.islice(zip(docs, expecteds), n_fetch):
|
|
|
|
assert_docs_equal(doc, expected_doc)
|
2020-07-22 14:42:59 +03:00
|
|
|
|
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_error_handler(n_process):
|
2021-01-29 03:51:21 +03:00
|
|
|
"""Test that the error handling of nlp.pipe works well"""
|
2021-05-17 14:28:39 +03:00
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
nlp = English()
|
|
|
|
nlp.add_pipe("merge_subtokens")
|
|
|
|
nlp.initialize()
|
|
|
|
texts = ["Curious to see what will happen to this text.", "And this one."]
|
|
|
|
# the pipeline fails because there's no parser
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp(texts[0])
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
nlp.set_error_handler(raise_error)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(texts, n_process=n_process))
|
2024-04-29 12:10:17 +03:00
|
|
|
# set explicitly to ignoring
|
2021-05-17 14:28:39 +03:00
|
|
|
nlp.set_error_handler(ignore_error)
|
|
|
|
docs = list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
assert len(docs) == 0
|
2021-01-29 03:51:21 +03:00
|
|
|
nlp(texts[0])
|
|
|
|
|
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_error_handler_custom(en_vocab, n_process):
|
2021-01-29 03:51:21 +03:00
|
|
|
"""Test the error handling of a custom component that has no pipe method"""
|
2021-05-17 14:28:39 +03:00
|
|
|
Language.component("my_evil_component", func=evil_component)
|
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
nlp = English()
|
|
|
|
nlp.add_pipe("my_evil_component")
|
|
|
|
texts = ["TEXT 111", "TEXT 222", "TEXT 333", "TEXT 342", "TEXT 666"]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# the evil custom component throws an error
|
|
|
|
list(nlp.pipe(texts))
|
|
|
|
|
|
|
|
nlp.set_error_handler(warn_error)
|
|
|
|
logger = logging.getLogger("spacy")
|
|
|
|
with mock.patch.object(logger, "warning") as mock_warning:
|
|
|
|
# the errors by the evil custom component raise a warning for each
|
|
|
|
# bad doc
|
|
|
|
docs = list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
# HACK/TODO? the warnings in child processes don't seem to be
|
|
|
|
# detected by the mock logger
|
|
|
|
if n_process == 1:
|
|
|
|
mock_warning.assert_called()
|
|
|
|
assert mock_warning.call_count == 2
|
|
|
|
assert len(docs) + mock_warning.call_count == len(texts)
|
|
|
|
assert [doc.text for doc in docs] == ["TEXT 111", "TEXT 333", "TEXT 666"]
|
2021-01-30 04:52:33 +03:00
|
|
|
|
2021-01-29 03:51:21 +03:00
|
|
|
|
2021-11-03 12:57:34 +03:00
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_error_handler_input_as_tuples(en_vocab, n_process):
|
|
|
|
"""Test the error handling of nlp.pipe with input as tuples"""
|
|
|
|
Language.component("my_evil_component", func=evil_component)
|
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
nlp = English()
|
|
|
|
nlp.add_pipe("my_evil_component")
|
|
|
|
texts = [
|
|
|
|
("TEXT 111", 111),
|
|
|
|
("TEXT 222", 222),
|
|
|
|
("TEXT 333", 333),
|
|
|
|
("TEXT 342", 342),
|
|
|
|
("TEXT 666", 666),
|
|
|
|
]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(texts, as_tuples=True))
|
|
|
|
nlp.set_error_handler(warn_error)
|
|
|
|
logger = logging.getLogger("spacy")
|
|
|
|
with mock.patch.object(logger, "warning") as mock_warning:
|
|
|
|
tuples = list(nlp.pipe(texts, as_tuples=True, n_process=n_process))
|
|
|
|
# HACK/TODO? the warnings in child processes don't seem to be
|
|
|
|
# detected by the mock logger
|
|
|
|
if n_process == 1:
|
|
|
|
mock_warning.assert_called()
|
|
|
|
assert mock_warning.call_count == 2
|
|
|
|
assert len(tuples) + mock_warning.call_count == len(texts)
|
|
|
|
assert (tuples[0][0].text, tuples[0][1]) == ("TEXT 111", 111)
|
|
|
|
assert (tuples[1][0].text, tuples[1][1]) == ("TEXT 333", 333)
|
|
|
|
assert (tuples[2][0].text, tuples[2][1]) == ("TEXT 666", 666)
|
|
|
|
|
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_error_handler_pipe(en_vocab, n_process):
|
|
|
|
"""Test the error handling of a component's pipe method"""
|
|
|
|
Language.component("my_perhaps_sentences", func=perhaps_set_sentences)
|
|
|
|
Language.component("assert_sents_error", func=assert_sents_error)
|
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
texts = [f"{str(i)} is enough. Done" for i in range(100)]
|
|
|
|
nlp = English()
|
|
|
|
nlp.add_pipe("my_perhaps_sentences")
|
|
|
|
nlp.add_pipe("assert_sents_error")
|
|
|
|
nlp.initialize()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# assert_sents_error requires sentence boundaries, will throw an error otherwise
|
|
|
|
docs = list(nlp.pipe(texts, n_process=n_process, batch_size=10))
|
|
|
|
nlp.set_error_handler(ignore_error)
|
|
|
|
docs = list(nlp.pipe(texts, n_process=n_process, batch_size=10))
|
|
|
|
# we lose/ignore the failing 4,40-49 docs
|
|
|
|
assert len(docs) == 89
|
2021-01-29 03:51:21 +03:00
|
|
|
|
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_error_handler_make_doc_actual(n_process):
|
|
|
|
"""Test the error handling for make_doc"""
|
|
|
|
# TODO: fix so that the following test is the actual behavior
|
2021-01-30 04:52:33 +03:00
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
nlp = English()
|
|
|
|
nlp.max_length = 10
|
|
|
|
texts = ["12345678901234567890", "12345"] * 10
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
nlp.default_error_handler = ignore_error
|
|
|
|
if n_process == 1:
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
else:
|
|
|
|
docs = list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
assert len(docs) == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_language_pipe_error_handler_make_doc_preferred(n_process):
|
|
|
|
"""Test the error handling for make_doc"""
|
2021-01-29 03:51:21 +03:00
|
|
|
|
2021-05-17 14:28:39 +03:00
|
|
|
ops = get_current_ops()
|
|
|
|
if isinstance(ops, NumpyOps) or n_process < 2:
|
|
|
|
nlp = English()
|
|
|
|
nlp.max_length = 10
|
|
|
|
texts = ["12345678901234567890", "12345"] * 10
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
nlp.default_error_handler = ignore_error
|
|
|
|
docs = list(nlp.pipe(texts, n_process=n_process))
|
|
|
|
assert len(docs) == 0
|
2021-01-29 03:51:21 +03:00
|
|
|
|
|
|
|
|
2020-08-05 20:47:54 +03:00
|
|
|
def test_language_from_config_before_after_init():
|
|
|
|
name = "test_language_from_config_before_after_init"
|
|
|
|
ran_before = False
|
|
|
|
ran_after = False
|
|
|
|
ran_after_pipeline = False
|
2021-01-12 13:29:31 +03:00
|
|
|
ran_before_init = False
|
|
|
|
ran_after_init = False
|
2020-08-05 20:47:54 +03:00
|
|
|
|
|
|
|
@registry.callbacks(f"{name}_before")
|
|
|
|
def make_before_creation():
|
|
|
|
def before_creation(lang_cls):
|
|
|
|
nonlocal ran_before
|
|
|
|
ran_before = True
|
|
|
|
assert lang_cls is English
|
|
|
|
lang_cls.Defaults.foo = "bar"
|
|
|
|
return lang_cls
|
|
|
|
|
|
|
|
return before_creation
|
|
|
|
|
|
|
|
@registry.callbacks(f"{name}_after")
|
|
|
|
def make_after_creation():
|
|
|
|
def after_creation(nlp):
|
|
|
|
nonlocal ran_after
|
|
|
|
ran_after = True
|
|
|
|
assert isinstance(nlp, English)
|
|
|
|
assert nlp.pipe_names == []
|
|
|
|
assert nlp.Defaults.foo == "bar"
|
|
|
|
nlp.meta["foo"] = "bar"
|
|
|
|
return nlp
|
|
|
|
|
|
|
|
return after_creation
|
|
|
|
|
|
|
|
@registry.callbacks(f"{name}_after_pipeline")
|
|
|
|
def make_after_pipeline_creation():
|
|
|
|
def after_pipeline_creation(nlp):
|
|
|
|
nonlocal ran_after_pipeline
|
|
|
|
ran_after_pipeline = True
|
|
|
|
assert isinstance(nlp, English)
|
|
|
|
assert nlp.pipe_names == ["sentencizer"]
|
|
|
|
assert nlp.Defaults.foo == "bar"
|
|
|
|
assert nlp.meta["foo"] == "bar"
|
|
|
|
nlp.meta["bar"] = "baz"
|
|
|
|
return nlp
|
|
|
|
|
|
|
|
return after_pipeline_creation
|
|
|
|
|
2021-01-12 13:29:31 +03:00
|
|
|
@registry.callbacks(f"{name}_before_init")
|
|
|
|
def make_before_init():
|
|
|
|
def before_init(nlp):
|
|
|
|
nonlocal ran_before_init
|
|
|
|
ran_before_init = True
|
|
|
|
nlp.meta["before_init"] = "before"
|
|
|
|
return nlp
|
|
|
|
|
|
|
|
return before_init
|
|
|
|
|
|
|
|
@registry.callbacks(f"{name}_after_init")
|
|
|
|
def make_after_init():
|
|
|
|
def after_init(nlp):
|
|
|
|
nonlocal ran_after_init
|
|
|
|
ran_after_init = True
|
|
|
|
nlp.meta["after_init"] = "after"
|
|
|
|
return nlp
|
|
|
|
|
|
|
|
return after_init
|
|
|
|
|
2020-08-05 20:47:54 +03:00
|
|
|
config = {
|
|
|
|
"nlp": {
|
|
|
|
"pipeline": ["sentencizer"],
|
|
|
|
"before_creation": {"@callbacks": f"{name}_before"},
|
|
|
|
"after_creation": {"@callbacks": f"{name}_after"},
|
|
|
|
"after_pipeline_creation": {"@callbacks": f"{name}_after_pipeline"},
|
|
|
|
},
|
|
|
|
"components": {"sentencizer": {"factory": "sentencizer"}},
|
2021-01-12 13:29:31 +03:00
|
|
|
"initialize": {
|
|
|
|
"before_init": {"@callbacks": f"{name}_before_init"},
|
|
|
|
"after_init": {"@callbacks": f"{name}_after_init"},
|
|
|
|
},
|
2020-08-05 20:47:54 +03:00
|
|
|
}
|
|
|
|
nlp = English.from_config(config)
|
|
|
|
assert nlp.Defaults.foo == "bar"
|
|
|
|
assert nlp.meta["foo"] == "bar"
|
|
|
|
assert nlp.meta["bar"] == "baz"
|
2021-01-12 13:29:31 +03:00
|
|
|
assert "before_init" not in nlp.meta
|
|
|
|
assert "after_init" not in nlp.meta
|
2020-08-05 20:47:54 +03:00
|
|
|
assert nlp.pipe_names == ["sentencizer"]
|
|
|
|
assert nlp("text")
|
2021-01-12 13:29:31 +03:00
|
|
|
nlp.initialize()
|
|
|
|
assert nlp.meta["before_init"] == "before"
|
|
|
|
assert nlp.meta["after_init"] == "after"
|
2021-01-15 03:57:36 +03:00
|
|
|
assert all(
|
|
|
|
[ran_before, ran_after, ran_after_pipeline, ran_before_init, ran_after_init]
|
|
|
|
)
|
2020-08-05 20:47:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_language_from_config_before_after_init_invalid():
|
|
|
|
"""Check that an error is raised if function doesn't return nlp."""
|
|
|
|
name = "test_language_from_config_before_after_init_invalid"
|
|
|
|
registry.callbacks(f"{name}_before1", func=lambda: lambda nlp: None)
|
|
|
|
registry.callbacks(f"{name}_before2", func=lambda: lambda nlp: nlp())
|
|
|
|
registry.callbacks(f"{name}_after1", func=lambda: lambda nlp: None)
|
|
|
|
registry.callbacks(f"{name}_after1", func=lambda: lambda nlp: English)
|
|
|
|
|
|
|
|
for callback_name in [f"{name}_before1", f"{name}_before2"]:
|
|
|
|
config = {"nlp": {"before_creation": {"@callbacks": callback_name}}}
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
English.from_config(config)
|
|
|
|
for callback_name in [f"{name}_after1", f"{name}_after2"]:
|
|
|
|
config = {"nlp": {"after_creation": {"@callbacks": callback_name}}}
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
English.from_config(config)
|
|
|
|
for callback_name in [f"{name}_after1", f"{name}_after2"]:
|
|
|
|
config = {"nlp": {"after_pipeline_creation": {"@callbacks": callback_name}}}
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
English.from_config(config)
|
2020-08-09 16:24:01 +03:00
|
|
|
|
|
|
|
|
2021-06-16 11:58:45 +03:00
|
|
|
def test_language_whitespace_tokenizer():
|
|
|
|
"""Test the custom whitespace tokenizer from the docs."""
|
|
|
|
|
|
|
|
class WhitespaceTokenizer:
|
|
|
|
def __init__(self, vocab):
|
|
|
|
self.vocab = vocab
|
|
|
|
|
|
|
|
def __call__(self, text):
|
2021-06-17 00:56:35 +03:00
|
|
|
words = text.split(" ")
|
|
|
|
spaces = [True] * len(words)
|
|
|
|
# Avoid zero-length tokens
|
|
|
|
for i, word in enumerate(words):
|
|
|
|
if word == "":
|
|
|
|
words[i] = " "
|
|
|
|
spaces[i] = False
|
|
|
|
# Remove the final trailing space
|
|
|
|
if words[-1] == " ":
|
|
|
|
words = words[0:-1]
|
|
|
|
spaces = spaces[0:-1]
|
|
|
|
else:
|
|
|
|
spaces[-1] = False
|
|
|
|
|
|
|
|
return Doc(self.vocab, words=words, spaces=spaces)
|
2021-06-16 11:58:45 +03:00
|
|
|
|
|
|
|
nlp = spacy.blank("en")
|
|
|
|
nlp.tokenizer = WhitespaceTokenizer(nlp.vocab)
|
2021-06-17 00:56:35 +03:00
|
|
|
text = " What's happened to me? he thought. It wasn't a dream. "
|
|
|
|
doc = nlp(text)
|
|
|
|
assert doc.text == text
|
2021-06-16 11:58:45 +03:00
|
|
|
|
|
|
|
|
2020-08-09 16:24:01 +03:00
|
|
|
def test_language_custom_tokenizer():
|
|
|
|
"""Test that a fully custom tokenizer can be plugged in via the registry."""
|
|
|
|
name = "test_language_custom_tokenizer"
|
|
|
|
|
|
|
|
class CustomTokenizer:
|
|
|
|
"""Dummy "tokenizer" that splits on spaces and adds prefix to each word."""
|
|
|
|
|
|
|
|
def __init__(self, nlp, prefix):
|
|
|
|
self.vocab = nlp.vocab
|
|
|
|
self.prefix = prefix
|
|
|
|
|
|
|
|
def __call__(self, text):
|
|
|
|
words = [f"{self.prefix}{word}" for word in text.split(" ")]
|
|
|
|
return Doc(self.vocab, words=words)
|
|
|
|
|
|
|
|
@registry.tokenizers(name)
|
|
|
|
def custom_create_tokenizer(prefix: str = "_"):
|
|
|
|
def create_tokenizer(nlp):
|
|
|
|
return CustomTokenizer(nlp, prefix=prefix)
|
|
|
|
|
|
|
|
return create_tokenizer
|
|
|
|
|
|
|
|
config = {"nlp": {"tokenizer": {"@tokenizers": name}}}
|
|
|
|
nlp = English.from_config(config)
|
|
|
|
doc = nlp("hello world")
|
|
|
|
assert [t.text for t in doc] == ["_hello", "_world"]
|
|
|
|
doc = list(nlp.pipe(["hello world"]))[0]
|
|
|
|
assert [t.text for t in doc] == ["_hello", "_world"]
|
2020-09-15 15:24:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_language_from_config_invalid_lang():
|
|
|
|
"""Test that calling Language.from_config raises an error and lang defined
|
|
|
|
in config needs to match language-specific subclasses."""
|
|
|
|
config = {"nlp": {"lang": "en"}}
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Language.from_config(config)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
German.from_config(config)
|
2020-09-15 15:24:42 +03:00
|
|
|
|
|
|
|
|
2020-09-15 12:12:12 +03:00
|
|
|
def test_spacy_blank():
|
|
|
|
nlp = spacy.blank("en")
|
|
|
|
assert nlp.config["training"]["dropout"] == 0.1
|
|
|
|
config = {"training": {"dropout": 0.2}}
|
|
|
|
meta = {"name": "my_custom_model"}
|
|
|
|
nlp = spacy.blank("en", config=config, meta=meta)
|
|
|
|
assert nlp.config["training"]["dropout"] == 0.2
|
|
|
|
assert nlp.meta["name"] == "my_custom_model"
|
2020-09-15 14:25:34 +03:00
|
|
|
|
|
|
|
|
2021-10-05 10:52:22 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"lang,target",
|
|
|
|
[
|
2021-11-03 12:57:34 +03:00
|
|
|
("en", "en"),
|
|
|
|
("fra", "fr"),
|
|
|
|
("fre", "fr"),
|
|
|
|
("iw", "he"),
|
2023-01-31 19:30:43 +03:00
|
|
|
("is", "isl"),
|
2021-11-03 12:57:34 +03:00
|
|
|
("mo", "ro"),
|
2023-01-31 19:30:43 +03:00
|
|
|
("mul", "mul"),
|
2021-11-03 12:57:34 +03:00
|
|
|
("no", "nb"),
|
|
|
|
("pt-BR", "pt"),
|
2023-01-31 19:30:43 +03:00
|
|
|
("xx", "mul"),
|
2021-11-03 12:57:34 +03:00
|
|
|
("zh-Hans", "zh"),
|
|
|
|
("zh-Hant", None),
|
|
|
|
("zxx", None),
|
|
|
|
],
|
2021-10-05 10:52:22 +03:00
|
|
|
)
|
|
|
|
def test_language_matching(lang, target):
|
|
|
|
"""
|
|
|
|
Test that we can look up languages by equivalent or nearly-equivalent
|
|
|
|
language codes.
|
|
|
|
"""
|
|
|
|
assert find_matching_language(lang) == target
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"lang,target",
|
|
|
|
[
|
2021-11-03 12:57:34 +03:00
|
|
|
("en", "en"),
|
|
|
|
("fra", "fr"),
|
|
|
|
("fre", "fr"),
|
|
|
|
("iw", "he"),
|
2023-01-31 19:30:43 +03:00
|
|
|
("is", "isl"),
|
2021-11-03 12:57:34 +03:00
|
|
|
("mo", "ro"),
|
2023-01-31 19:30:43 +03:00
|
|
|
("xx", "mul"),
|
2021-11-03 12:57:34 +03:00
|
|
|
("no", "nb"),
|
|
|
|
("pt-BR", "pt"),
|
|
|
|
("zh-Hans", "zh"),
|
|
|
|
],
|
2021-10-05 10:52:22 +03:00
|
|
|
)
|
|
|
|
def test_blank_languages(lang, target):
|
|
|
|
"""
|
|
|
|
Test that we can get spacy.blank in various languages, including codes
|
|
|
|
that are defined to be equivalent or that match by CLDR language matching.
|
|
|
|
"""
|
|
|
|
nlp = spacy.blank(lang)
|
|
|
|
assert nlp.lang == target
|
|
|
|
|
|
|
|
|
2020-09-29 22:39:28 +03:00
|
|
|
@pytest.mark.parametrize("value", [False, None, ["x", "y"], Language, Vocab])
|
2020-09-15 14:25:34 +03:00
|
|
|
def test_language_init_invalid_vocab(value):
|
|
|
|
err_fragment = "invalid value"
|
|
|
|
with pytest.raises(ValueError) as e:
|
|
|
|
Language(value)
|
2020-09-15 23:30:09 +03:00
|
|
|
assert err_fragment in str(e.value)
|
2021-06-21 10:33:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_language_source_and_vectors(nlp2):
|
|
|
|
nlp = Language(Vocab())
|
|
|
|
textcat = nlp.add_pipe("textcat")
|
|
|
|
for label in ("POSITIVE", "NEGATIVE"):
|
|
|
|
textcat.add_label(label)
|
|
|
|
nlp.initialize()
|
|
|
|
long_string = "thisisalongstring"
|
|
|
|
assert long_string not in nlp.vocab.strings
|
|
|
|
assert long_string not in nlp2.vocab.strings
|
|
|
|
nlp.vocab.strings.add(long_string)
|
|
|
|
assert nlp.vocab.vectors.to_bytes() != nlp2.vocab.vectors.to_bytes()
|
|
|
|
vectors_bytes = nlp.vocab.vectors.to_bytes()
|
2021-06-21 11:41:40 +03:00
|
|
|
with pytest.warns(UserWarning):
|
2021-06-21 10:33:33 +03:00
|
|
|
nlp2.add_pipe("textcat", name="textcat2", source=nlp)
|
|
|
|
# strings should be added
|
|
|
|
assert long_string in nlp2.vocab.strings
|
|
|
|
# vectors should remain unmodified
|
|
|
|
assert nlp.vocab.vectors.to_bytes() == vectors_bytes
|
2021-09-22 10:41:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("n_process", [1, 2])
|
|
|
|
def test_pass_doc_to_pipeline(nlp, n_process):
|
|
|
|
texts = ["cats", "dogs", "guinea pigs"]
|
|
|
|
docs = [nlp.make_doc(text) for text in texts]
|
|
|
|
assert not any(len(doc.cats) for doc in docs)
|
|
|
|
doc = nlp(docs[0])
|
|
|
|
assert doc.text == texts[0]
|
|
|
|
assert len(doc.cats) > 0
|
|
|
|
if isinstance(get_current_ops(), NumpyOps) or n_process < 2:
|
2024-02-12 16:39:38 +03:00
|
|
|
# Catch warnings to ensure that all worker processes exited
|
|
|
|
# succesfully.
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("error")
|
|
|
|
docs = nlp.pipe(docs, n_process=n_process)
|
|
|
|
assert [doc.text for doc in docs] == texts
|
|
|
|
assert all(len(doc.cats) for doc in docs)
|
2021-09-22 10:41:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_arg_to_pipeline(nlp):
|
|
|
|
str_list = ["This is a text.", "This is another."]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp(str_list) # type: ignore
|
|
|
|
assert len(list(nlp.pipe(str_list))) == 2
|
|
|
|
int_list = [1, 2, 3]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
list(nlp.pipe(int_list)) # type: ignore
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp(int_list) # type: ignore
|
2021-10-26 12:53:50 +03:00
|
|
|
|
|
|
|
|
2021-10-21 17:14:23 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not isinstance(get_current_ops(), CupyOps), reason="test requires GPU"
|
|
|
|
)
|
|
|
|
def test_multiprocessing_gpu_warning(nlp2, texts):
|
|
|
|
texts = texts * 10
|
|
|
|
docs = nlp2.pipe(texts, n_process=2, batch_size=2)
|
|
|
|
|
|
|
|
with pytest.warns(UserWarning, match="multiprocessing with GPU models"):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# Trigger multi-processing.
|
|
|
|
for _ in docs:
|
|
|
|
pass
|
2022-08-19 10:52:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_dot_in_factory_names(nlp):
|
|
|
|
Language.component("my_evil_component", func=evil_component)
|
|
|
|
nlp.add_pipe("my_evil_component")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="not permitted"):
|
|
|
|
Language.component("my.evil.component.v1", func=evil_component)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="not permitted"):
|
|
|
|
Language.factory("my.evil.component.v1", func=evil_component)
|
2022-09-01 20:37:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_component_return():
|
|
|
|
"""Test that an error is raised if components return a type other than a
|
|
|
|
doc."""
|
|
|
|
nlp = English()
|
|
|
|
|
|
|
|
@Language.component("test_component_good_pipe")
|
|
|
|
def good_pipe(doc):
|
|
|
|
return doc
|
|
|
|
|
|
|
|
nlp.add_pipe("test_component_good_pipe")
|
|
|
|
nlp("text")
|
|
|
|
nlp.remove_pipe("test_component_good_pipe")
|
|
|
|
|
|
|
|
@Language.component("test_component_bad_pipe")
|
|
|
|
def bad_pipe(doc):
|
|
|
|
return doc.text
|
|
|
|
|
|
|
|
nlp.add_pipe("test_component_bad_pipe")
|
|
|
|
with pytest.raises(ValueError, match="instead of a Doc"):
|
|
|
|
nlp("text")
|
2023-01-30 14:44:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.slow
|
|
|
|
@pytest.mark.parametrize("teacher_tagger_name", ["tagger", "teacher_tagger"])
|
|
|
|
def test_distill(teacher_tagger_name):
|
|
|
|
teacher = English()
|
|
|
|
teacher_tagger = teacher.add_pipe("tagger", name=teacher_tagger_name)
|
|
|
|
train_examples = []
|
|
|
|
for t in TAGGER_TRAIN_DATA:
|
|
|
|
train_examples.append(Example.from_dict(teacher.make_doc(t[0]), t[1]))
|
|
|
|
|
|
|
|
optimizer = teacher.initialize(get_examples=lambda: train_examples)
|
|
|
|
|
|
|
|
for i in range(50):
|
|
|
|
losses = {}
|
|
|
|
teacher.update(train_examples, sgd=optimizer, losses=losses)
|
|
|
|
assert losses[teacher_tagger_name] < 0.00001
|
|
|
|
|
|
|
|
student = English()
|
|
|
|
student_tagger = student.add_pipe("tagger")
|
|
|
|
student_tagger.min_tree_freq = 1
|
|
|
|
student_tagger.initialize(
|
|
|
|
get_examples=lambda: train_examples, labels=teacher_tagger.label_data
|
|
|
|
)
|
|
|
|
|
|
|
|
distill_examples = [
|
|
|
|
Example.from_dict(teacher.make_doc(t[0]), {}) for t in TAGGER_TRAIN_DATA
|
|
|
|
]
|
|
|
|
|
|
|
|
student_to_teacher = (
|
|
|
|
None
|
|
|
|
if teacher_tagger.name == student_tagger.name
|
|
|
|
else {student_tagger.name: teacher_tagger.name}
|
|
|
|
)
|
|
|
|
|
|
|
|
for i in range(50):
|
|
|
|
losses = {}
|
|
|
|
student.distill(
|
|
|
|
teacher,
|
|
|
|
distill_examples,
|
|
|
|
sgd=optimizer,
|
|
|
|
losses=losses,
|
|
|
|
student_to_teacher=student_to_teacher,
|
|
|
|
)
|
|
|
|
assert losses["tagger"] < 0.00001
|
|
|
|
|
|
|
|
test_text = "I like blue eggs"
|
|
|
|
doc = student(test_text)
|
|
|
|
assert doc[0].tag_ == "N"
|
|
|
|
assert doc[1].tag_ == "V"
|
|
|
|
assert doc[2].tag_ == "J"
|
|
|
|
assert doc[3].tag_ == "N"
|
|
|
|
|
|
|
|
# Do an extra update to check if annotates works, though we can't really
|
|
|
|
# validate the resuls, since the annotations are ephemeral.
|
|
|
|
student.distill(
|
|
|
|
teacher,
|
|
|
|
distill_examples,
|
|
|
|
sgd=optimizer,
|
|
|
|
losses=losses,
|
|
|
|
student_to_teacher=student_to_teacher,
|
|
|
|
annotates=["tagger"],
|
|
|
|
)
|