コード例 #1
0
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()
コード例 #2
0
    def test_nlp_model_provided(self):
        task = SpacyParser()
        with pytest.raises(ValueError) as exc:
            task.run()

        assert "A spaCy pipeline must be provided" == str(exc.value)
コード例 #3
0
 def test_initialization(self):
     task = SpacyParser()
     assert task.nlp is None
コード例 #4
0
 def test_get_parser(self):
     mock = Mock()
     mock.parser = "parser"
     task = SpacyParser(nlp=mock)
     parser = task.run()
     assert parser == "parser"
コード例 #5
0
 def test_nlp_model_provided(self):
     task = SpacyParser()
     with pytest.raises(ValueError,
                        match="A spaCy pipeline must be provided"):
         task.run()