def test_load_nlp_model_version_lt3(self, spacy_mock): spacy_mock.configure_mock( __version__="2.3.7") # some version less than 3.0 task = SpacyNLP(text="This is some text", spacy_model_name="en_core_web_sm") spacy_mock.load.assert_called_once_with("en_core_web_sm", disable=[], component_cfg={}) assert task.nlp is not None
import spacy # load a spacy language model nlp = spacy.load("en_core_web_sm") # add a custom component to language model def custom_component(doc): # do something with document return doc nlp.add_pipe(custom_component, name="custom") # create flow for NLP with Flow("Natural Language Processing") as flow: # create a spaCy doc from text, the equivalent of nlp('This is some text') doc = SpacyNLP(text="This is some text", nlp=nlp) # extract default components from language model pipeline tagger = SpacyTagger(nlp) parser = SpacyParser(nlp) ner = SpacyNER(nlp) # extract a custom component from language model pipeline by name custom_pipeline_component = SpacyComponent("custom") flow.run()
def test_text_passed_to_run(self): nlp = MagicMock() task = SpacyNLP(text="This is some text", nlp=nlp) task.run() assert nlp.call_args[0][0] == "This is some text"
def test_bad_model_raises_error(self): with pytest.raises(ValueError) as exc: task = SpacyNLP(text="This is some text", spacy_model_name="not_a_spacy_model") assert "not_a_spacy_model" in str(exc.value)
def test_initialization(self): task = SpacyNLP(text="This is some text", nlp=spacy.blank("en")) assert task.text == "This is some text"
def test_bad_model_raises_error(self): with pytest.raises(ValueError, match="not_a_spacy_model"): task = SpacyNLP(text="This is some text", spacy_model_name="not_a_spacy_model")
def test_load_nlp_model(self): spacy.cli.download("en_core_web_sm") task = SpacyNLP(text="This is some text", spacy_model_name="en_core_web_sm") assert task.nlp is not None