[6c353a]: / medacy / tests / pipelines / test_pipeline.py

Download this file

43 lines (30 with data), 1.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import unittest
import spacy
from spacy.tokens import Doc
from medacy.pipelines.testing_pipeline import TestingPipeline
from medacy.tests.sample_data import sample_dataset
class TestPipeline(unittest.TestCase):
"""Unit tests for medacy.pipelines.testing_pipeline.TestingPipeline"""
def test_pipeline(self):
# Get the entities, the pipeline, and an extra spaCy pipeline
entities = sample_dataset.get_labels(as_list=True)
pipeline = TestingPipeline(entities)
if "gold_annotator" in pipeline.spacy_pipeline.pipe_names:
pipeline.spacy_pipeline.remove_pipe("gold_annotator")
spacy_pipeline = spacy.load('en_core_web_sm')
# Create a sample Doc
sample_doc_path = sample_dataset.data_files[0].txt_path
with open(sample_doc_path) as f:
text = f.read()
# The doc must be passed through a separate spaCy pipeline first
doc = spacy_pipeline(text)
doc = pipeline(doc)
self.assertIsInstance(doc, Doc)
# Check that the first token has all the expected spaCy features
spacy_features = pipeline.get_feature_extractor().spacy_features
token = doc[0]
for feature in spacy_features:
result = hasattr(token, feature)
self.assertTrue(result)
if __name__ == '__main__':
unittest.main()