-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathtest_rehearse.py
211 lines (193 loc) · 6.25 KB
/
test_rehearse.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from typing import List
import pytest
import spacy
from spacy.training import Example
TRAIN_DATA = [
(
"Who is Kofi Annan?",
{
"entities": [(7, 18, "PERSON")],
"tags": ["PRON", "AUX", "PROPN", "PRON", "PUNCT"],
"heads": [1, 1, 3, 1, 1],
"deps": ["attr", "ROOT", "compound", "nsubj", "punct"],
"morphs": [
"",
"Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin",
"Number=Sing",
"Number=Sing",
"PunctType=Peri",
],
"cats": {"question": 1.0},
},
),
(
"Who is Steve Jobs?",
{
"entities": [(7, 17, "PERSON")],
"tags": ["PRON", "AUX", "PROPN", "PRON", "PUNCT"],
"heads": [1, 1, 3, 1, 1],
"deps": ["attr", "ROOT", "compound", "nsubj", "punct"],
"morphs": [
"",
"Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin",
"Number=Sing",
"Number=Sing",
"PunctType=Peri",
],
"cats": {"question": 1.0},
},
),
(
"Bob is a nice person.",
{
"entities": [(0, 3, "PERSON")],
"tags": ["PROPN", "AUX", "DET", "ADJ", "NOUN", "PUNCT"],
"heads": [1, 1, 4, 4, 1, 1],
"deps": ["nsubj", "ROOT", "det", "amod", "attr", "punct"],
"morphs": [
"Number=Sing",
"Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin",
"Definite=Ind|PronType=Art",
"Degree=Pos",
"Number=Sing",
"PunctType=Peri",
],
"cats": {"statement": 1.0},
},
),
(
"Hi Anil, how are you?",
{
"entities": [(3, 7, "PERSON")],
"tags": ["INTJ", "PROPN", "PUNCT", "ADV", "AUX", "PRON", "PUNCT"],
"deps": ["intj", "npadvmod", "punct", "advmod", "ROOT", "nsubj", "punct"],
"heads": [4, 0, 4, 4, 4, 4, 4],
"morphs": [
"",
"Number=Sing",
"PunctType=Comm",
"",
"Mood=Ind|Tense=Pres|VerbForm=Fin",
"Case=Nom|Person=2|PronType=Prs",
"PunctType=Peri",
],
"cats": {"greeting": 1.0, "question": 1.0},
},
),
(
"I like London and Berlin.",
{
"entities": [(7, 13, "LOC"), (18, 24, "LOC")],
"tags": ["PROPN", "VERB", "PROPN", "CCONJ", "PROPN", "PUNCT"],
"deps": ["nsubj", "ROOT", "dobj", "cc", "conj", "punct"],
"heads": [1, 1, 1, 2, 2, 1],
"morphs": [
"Case=Nom|Number=Sing|Person=1|PronType=Prs",
"Tense=Pres|VerbForm=Fin",
"Number=Sing",
"ConjType=Cmp",
"Number=Sing",
"PunctType=Peri",
],
"cats": {"statement": 1.0},
},
),
]
REHEARSE_DATA = [
(
"Hi Anil",
{
"entities": [(3, 7, "PERSON")],
"tags": ["INTJ", "PROPN"],
"deps": ["ROOT", "npadvmod"],
"heads": [0, 0],
"morphs": ["", "Number=Sing"],
"cats": {"greeting": 1.0},
},
),
(
"Hi Ravish, how you doing?",
{
"entities": [(3, 9, "PERSON")],
"tags": ["INTJ", "PROPN", "PUNCT", "ADV", "AUX", "PRON", "PUNCT"],
"deps": ["intj", "ROOT", "punct", "advmod", "nsubj", "advcl", "punct"],
"heads": [1, 1, 1, 5, 5, 1, 1],
"morphs": [
"",
"VerbForm=Inf",
"PunctType=Comm",
"",
"Case=Nom|Person=2|PronType=Prs",
"Aspect=Prog|Tense=Pres|VerbForm=Part",
"PunctType=Peri",
],
"cats": {"greeting": 1.0, "question": 1.0},
},
),
# UTENSIL new label
(
"Natasha bought new forks.",
{
"entities": [(0, 7, "PERSON"), (19, 24, "UTENSIL")],
"tags": ["PROPN", "VERB", "ADJ", "NOUN", "PUNCT"],
"deps": ["nsubj", "ROOT", "amod", "dobj", "punct"],
"heads": [1, 1, 3, 1, 1],
"morphs": [
"Number=Sing",
"Tense=Past|VerbForm=Fin",
"Degree=Pos",
"Number=Plur",
"PunctType=Peri",
],
"cats": {"statement": 1.0},
},
),
]
def _add_ner_label(ner, data):
for _, annotations in data:
for ent in annotations["entities"]:
ner.add_label(ent[2])
def _add_tagger_label(tagger, data):
for _, annotations in data:
for tag in annotations["tags"]:
tagger.add_label(tag)
def _add_parser_label(parser, data):
for _, annotations in data:
for dep in annotations["deps"]:
parser.add_label(dep)
def _add_textcat_label(textcat, data):
for _, annotations in data:
for cat in annotations["cats"]:
textcat.add_label(cat)
def _optimize(nlp, component: str, data: List, rehearse: bool):
"""Run either train or rehearse."""
pipe = nlp.get_pipe(component)
if component == "ner":
_add_ner_label(pipe, data)
elif component == "tagger":
_add_tagger_label(pipe, data)
elif component == "parser":
_add_parser_label(pipe, data)
elif component == "textcat_multilabel":
_add_textcat_label(pipe, data)
else:
raise NotImplementedError
if rehearse:
optimizer = nlp.resume_training()
else:
optimizer = nlp.initialize()
for _ in range(5):
for text, annotation in data:
doc = nlp.make_doc(text)
example = Example.from_dict(doc, annotation)
if rehearse:
nlp.rehearse([example], sgd=optimizer)
else:
nlp.update([example], sgd=optimizer)
return nlp
@pytest.mark.parametrize("component", ["ner", "tagger", "parser", "textcat_multilabel"])
def test_rehearse(component):
nlp = spacy.blank("en")
nlp.add_pipe(component)
nlp = _optimize(nlp, component, TRAIN_DATA, False)
_optimize(nlp, component, REHEARSE_DATA, True)