git.iko.soy/typeable/nixpkgs
on master help sign in
python3Packages.spacy-transformers: add passthru annotation test
commit636b3581f6d1ec5bd75054ef803a74c8a3ab0226 authorDaniël de Kok <me@danieldk.eu> dateSun 16 May 2021, 10:11 (6 years ago) parent51f3986
3 files changed+94
pkgs/development/python-modules/spacy-transformers/annotation-test/annotate.py+68 pkgs/development/python-modules/spacy-transformers/annotation-test/default.nix+23 pkgs/development/python-modules/spacy-transformers/default.nix+3
pkgs/development/python-modules/spacy-transformers/annotation-test/annotate.pynew file+68
@@ -0,0 +1,68 @@
1+import pytest
2+import spacy
3+
4+en_text = (
5+ "When Sebastian Thrun started working on self-driving cars at "
6+ "Google in 2007, few people outside of the company took him "
7+ "seriously. “I can tell you very senior CEOs of major American "
8+ "car companies would shake my hand and turn away because I wasn’t "
9+ "worth talking to,” said Thrun, in an interview with Recode earlier "
10+ "this week.")
11+
12+
13+@pytest.fixture
14+def en_core_web_trf():
15+ return spacy.load("en_core_web_trf")
16+
17+
18+@pytest.fixture
19+def doc_en_core_web_trf(en_core_web_trf):
20+ return en_core_web_trf(en_text)
21+
22+
23+def test_entities(doc_en_core_web_trf):
24+ entities = list(map(lambda e: (e.text, e.label_),
25+ doc_en_core_web_trf.ents))
26+
27+ assert entities == [
28+ ('Sebastian Thrun', 'PERSON'),
29+ ('Google', 'ORG'),
30+ ('2007', 'DATE'),
31+ ('American', 'NORP'),
32+ ('Thrun', 'PERSON'),
33+ ('Recode', 'ORG'),
34+ ('earlier this week', 'DATE'),
35+ ]
36+
37+
38+def test_nouns(doc_en_core_web_trf):
39+ assert [
40+ chunk.text for chunk in doc_en_core_web_trf.noun_chunks] == [
41+ 'Sebastian Thrun',
42+ 'self-driving cars',
43+ 'Google',
44+ 'few people',
45+ 'the company',
46+ 'him',
47+ 'I',
48+ 'you',
49+ 'very senior CEOs',
50+ 'major American car companies',
51+ 'my hand',
52+ 'I',
53+ 'Thrun',
54+ 'an interview',
55+ 'Recode']
56+
57+
58+def test_verbs(doc_en_core_web_trf):
59+ assert [
60+ token.lemma_ for token in doc_en_core_web_trf if token.pos_ == "VERB"] == [
61+ 'start',
62+ 'take',
63+ 'tell',
64+ 'shake',
65+ 'turn',
66+ 'be',
67+ 'talk',
68+ 'say']
pkgs/development/python-modules/spacy-transformers/annotation-test/default.nixnew file+23
@@ -0,0 +1,23 @@
1+{ stdenv, pytest, spacy_models }:
2+
3+stdenv.mkDerivation {
4+ name = "spacy-transformers-annotation-test";
5+
6+ src = ./.;
7+
8+ dontConfigure = true;
9+ dontBuild = true;
10+ doCheck = true;
11+
12+ checkInputs = [ pytest spacy_models.en_core_web_trf ];
13+
14+ checkPhase = ''
15+ pytest annotate.py
16+ '';
17+
18+ installPhase = ''
19+ touch $out
20+ '';
21+
22+ meta.timeout = 60;
23+}
pkgs/development/python-modules/spacy-transformers/default.nix+3
@@ -1,4 +1,5 @@
11{ lib
2+, callPackage
23, fetchPypi
34, buildPythonPackage
45, pytorch
@@ -30,6 +31,8 @@ buildPythonPackage rec {
3031
3132 pythonImportsCheck = [ "spacy_transformers" ];
3233
34+ passthru.tests.annotation = callPackage ./annotation-test { };
35+
3336 meta = with lib; {
3437 description = "spaCy pipelines for pretrained BERT, XLNet and GPT-2";
3538 homepage = "https://github.com/explosion/spacy-transformers";