MCPcopy
hub / github.com/explosion/spaCy / test_evaluate_multiple_textcat_separate

Function test_evaluate_multiple_textcat_separate

spacy/tests/test_language.py:186–235  ·  view source on GitHub ↗

Test that evaluate can evaluate multiple textcat components separately with custom scorers.

(en_vocab)

Source from the content-addressed store, hash-verified

184
185
186def test_evaluate_multiple_textcat_separate(en_vocab):
187 """Test that evaluate can evaluate multiple textcat components separately
188 with custom scorers."""
189
190 def custom_textcat_score(examples, **kwargs):
191 scores = Scorer.score_cats(
192 examples,
193 "cats",
194 multi_label=False,
195 **kwargs,
196 )
197 return {f"custom_{k}": v for k, v in scores.items()}
198
199 @spacy.registry.scorers("test_custom_textcat_scorer")
200 def make_custom_textcat_scorer():
201 return custom_textcat_score
202
203 nlp = Language(en_vocab)
204 textcat = nlp.add_pipe(
205 "textcat",
206 config={"scorer": {"@scorers": "test_custom_textcat_scorer"}},
207 )
208 for label in ("POSITIVE", "NEGATIVE"):
209 textcat.add_label(label)
210 textcat_multilabel = nlp.add_pipe("textcat_multilabel")
211 for label in ("FEATURE", "REQUEST", "BUG", "QUESTION"):
212 textcat_multilabel.add_label(label)
213 nlp.initialize()
214
215 annots = {
216 "cats": {
217 "POSITIVE": 1.0,
218 "NEGATIVE": 0.0,
219 "FEATURE": 1.0,
220 "QUESTION": 1.0,
221 "POSITIVE": 1.0,
222 "NEGATIVE": 0.0,
223 }
224 }
225 doc = nlp.make_doc("hello world")
226 example = Example.from_dict(doc, annots)
227 scores = nlp.evaluate([example])
228 # check custom scores for the textcat pipe
229 assert "custom_cats_f_per_type" in scores
230 labels = nlp.get_pipe("textcat").labels
231 assert set(scores["custom_cats_f_per_type"].keys()) == set(labels)
232 # check default scores for the textcat_multilabel pipe
233 assert "cats_f_per_type" in scores
234 labels = nlp.get_pipe("textcat_multilabel").labels
235 assert set(scores["cats_f_per_type"].keys()) == set(labels)
236
237
238def vector_modification_pipe(doc):

Callers

nothing calls this directly

Calls 8

add_pipeMethod · 0.95
initializeMethod · 0.95
make_docMethod · 0.95
evaluateMethod · 0.95
get_pipeMethod · 0.95
LanguageClass · 0.90
from_dictMethod · 0.80
add_labelMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…