Test that evaluate can evaluate multiple textcat components separately with custom scorers.
(en_vocab)
| 184 | |
| 185 | |
| 186 | def 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 | |
| 238 | def vector_modification_pipe(doc): |
nothing calls this directly
no test coverage detected
searching dependent graphs…