# Span Categorization

> Annotate overlapping and nested spans like longer phrases and expressions, and create training data for span categorization models like spaCy's SpanCategorizer.

Source: https://prodi.gy/docs/span-categorization

A common task in applied NLP is extracting spans of texts from documents,
including longer phrases or nested expressions. Prodigy lets you label
potentially overlapping and nested spans and create training data for models and
components like spaCy's
[`SpanCategorizer`](https://spacy.io/api/spancategorizer).

## Named Entity Recognition vs. Span Categorization

[Named entity recognition](https://prodi.gy/usage/named-entity-recognition) is very effective
for proper nouns and self-contained expressions like person names or products,
because it predicts single-token-based tags and takes advantage of clear span
boundaries. However, it's usually **not** the right tool for other types of
phrases, sentence fragments and overlapping spans with less consistent
boundaries and mixed lengths. For use cases like this, you typically want to
choose a model that predicts scores and labels over a selection of spans. We
call this type of component a **span categorizer**.

| Named Entity Recognition                                                                            | Span Categorization                                                             |
| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| spans are non-overlapping syntactic units like proper nouns (e.g. persons, organizations, products) | spans are potentially overlapping units like noun phrases or sentence fragments |
| model predicts single token-based tags like `B-PERSON` with one tag per token                       | model predicts scores and labels for suggested spans                            |
| takes advantage of clear token boundaries                                                           | less sensitive to exact token boundaries                                        |

Before you get started on your NLP project, it can often be very helpful to take
a step back and think about the type of information you _really_ need for your
downstream application and which NLP component is the **best fit to capture this
information** in your data and **annotate it consistently**. In some cases, it
can make more sense to opt for a
[text classifier](https://prodi.gy/features/text-classification) instead of a span categorizer,
which will predict one or more labels over the whole text and lets you capture
signals from multiple places across the text, instead of attaching them to
specific spans of text.

## Fully manual annotation

To get started with manual span annotation, all you need is a file with raw
input text you want to annotate and a
[spaCy pipeline](https://prodi.gy/docs/install#spacy-models) for tokenization (so the web app
knows what a word is and can allow more efficient highlighting). The following
command will start the web server with the [`spans.manual`](https://prodi.gy/docs/recipes#spans-manual) recipe, stream in
texts from `journal_papers.jsonl` and provides the label options `FACTOR`,
`CONDITION`, `METHOD` and `EFFECT`. The [`spans_manual`](https://prodi.gy/docs/api-interfaces#spans_manual) UI will then let
you annotate arbitrary and **potentially overlapping** spans that are displayed
below the tokens.

```bash title="Example"
prodigy spans.manual covid_articles blank:en ./journal_papers.jsonl --label FACTOR,CONDITION,METHOD,EFFECT
```

To make annotation faster, the selection automatically "snaps" to token
boundaries and you can double-click on a single token to select it. Hovering a
span label will highlight all tokens it refers to. To remove a span, you can
click on its label.

### Working with patterns

Just like [`ner.manual`](https://prodi.gy/docs/recipes#ner-manual), the [`spans.manual`](https://prodi.gy/docs/recipes#spans-manual) workflow supports providing
a [match patterns file](https://prodi.gy/docs/api-loaders#input-patterns). Match patterns are
typically provided as a JSONL (newline-delimited JSON) file and can be used to
pre-highlight spans for faster and more efficient annotation. Since spans can
overlap, the found matches are not filtered and **all matched spans** will be
pre-highlighted in the UI.

```bash title="Example"
prodigy spans.manual covid_articles blank:en ./journal_papers.jsonl --label CONDITION --patterns ./condition_patterns.jsonl
```

## Working with suggester functions

spaCy's [`SpanCategorizer`](https://spacy.io/api/spancategorizer) works by using
a **suggester function** to suggest spans that the model will then predict
labels for. A common strategy is to use ngrams of certain sizes, e.g. all
possible spans of lengths 1 to 10. You can also implement more sophisticated
suggesters, for instance to consider all noun phrases in `Doc.noun_chunks`, or
only certain spans defined by
[match patterns](https://spacy.io/usage/rule-based-matching).

### Letting Prodigy infer the best-matching ngram suggester

During development, it's often helpful to just start annotating and get a sense
for the data and the types of spans you're looking for. If you don't provide a
config with a suggester during training, Prodigy will use the collected
annotations to infer the best matching ngram suggester based on the available
spans. For example, if your shortest span is 1 token and your longest span is 5
tokens, the created ngram suggester will cover a range of `1` to `5`.

Setting the `--verbose` flag on [`train`](https://prodi.gy/docs/recipes#train) will output a detailed breakdown of
the spans found in the data. This is also useful for discovering common patterns
and outliers, or even potential mistakes. As always with machine learning, it's
very important that your training data is **representative** of what your model
will see at runtime.

```text title="Example (excerpt)"
$ prodigy train --spancat covid_articles,eval:covid_articles_eval --verbose

======================== Generating Prodigy config ========================

ℹ Auto-generating config with spaCy
Using 'spacy.ngram_range_suggester.v1' for 'spancat' with sizes 1 to 5 (inferred from data)
Sizes: [1, 2, 3, 4, 5]
  - 1 (298 spans)
  - 2 (96 spans)
  - 3 (15 spans)
  - 4 (1 spans)
  - 5 (8 spans)
✔ Generated training config
```

The inferred suggester sizes will be added to your `config.cfg` that's exported
with the trained pipeline and generated when you run [`data-to-spacy`](https://prodi.gy/docs/recipes#data-to-spacy). Once
you got a better sense for the types of spans and span sizes you're expecting,
it can often be useful to run different experiments with different suggesters
and compare the results.

```ini title="config.cfg (excerpt)"
[components.spancat.suggester]
@misc = "spacy.ngram_range_suggester.v1"
min_size = 1
max_size = 5
```

### Annotating with suggester functions

Prodigy lets you provide a suggester function when you annotate with
[`spans.manual`](https://prodi.gy/docs/recipes#spans-manual), so you can make sure that the spans you annotate match the
suggested spans you want to train on. This way, you'll never end up with
annotations describing spans that aren't ever suggested and that the model
couldn't learn from. The suggester is run via the
[`validate_answer`](https://prodi.gy/docs/custom-recipes#validate_answer) callback every time an
annotator submits an answer. If an annotated span doesn't match the suggested
spans, you'll see an **alert in the UI** with more information, and you'll only
be able to submit the annotation if the validation passes.

![Screenshot of validation alert for span suggester](https://prodi.gy/docs/recipes_spans_suggesters.jpg)

The name of the suggester function can be provided via `--suggester`. You can
use Prodigy's `-F` argument to load in a Python file containing the registered
suggester function, which should be registered in spaCy's
[`@misc` registry](https://spacy.io/api/top-level#registry). If your suggester
requires annotations from other components you can use the `--use_annotations`
argument, which uses the defined spaCy model to process the text and to save the
annotations.

```bash title="Example with suggester validation"
prodigy spans.manual covid_articles blank:en ./journal_papers.jsonl --label CONDITION --suggester 123_ngram_suggester.v1 -F ./suggester.py --use_annotations
```

```python title="suggester.py"
from spacy import registry
from spacy.pipeline.spancat import build_ngram_suggester

@registry.misc("123_ngram_suggester.v1")
def custom_ngram_suggester():
    return build_ngram_suggester(sizes=[1, 2, 3])  # all ngrams of size 1, 2 and 3
```

> **Warning: Important note on registered suggesters**
>
> Since Prodigy's CLI only takes the name of the registered function and can't
> easily pass arbitrary arguments to it, the suggester function you use shouldn't
> require any arguments. This means that you may have to wrap your custom
> suggester in another function that sets all arguments to the values you want to
> use.

When you train your span categorizer, you can define the suggester function to
use in your [`config.cfg`](https://spacy.io/usage/training#quickstart). If
you've validated your annotations against the same suggester, you'll know that
all examples you're training from match the suggested spans the model will see.

```ini title="config.cfg (excerpt)"
[components.spancat.suggester]
@misc = "spacy.ngram_suggester.v1"
sizes = [1, 2, 3]
```

## Editing text during annotation

For some use cases, it can be helpful to **edit the text** of an example during span annotation, e.g. to fix mistakes, typos or formatting. Enabling `--edit-text` on [`spans.manual`](https://prodi.gy/docs/recipes#spans-manual) adds an edit button  to the labels bar, which you can also toggle by pressing the [shortcut](https://prodi.gy/docs/api-web-app#shortcuts-spans) `command`+`e`.

```bash title="Example"
prodigy spans.manual covid_articles blank:en ./journal_papers.jsonl --label FACTOR,CONDITION,METHOD,EFFECT --edit-text
```

[Video: /docs/spans\_edit-text.mp4](https://prodi.gy/docs/spans_edit-text.mp4)

To save your edits, click the save button  or press `command`+`enter`. Because changes in the text will also change the tokenization and potentially the existing spans, the recipe implements an [event hook](https://prodi.gy/docs/custom-interfaces#custom-events) that's called when text edits are saved. It receives the existing example and the new text, and re-generates the example JSON to reflect the edits.

> **Note: More details and documentation**
>
> [![Example of custom event hook in recioe](https://prodi.gy/docs/recipe_edit-text.png)](https://prodi.gy/docs/named-entity-recognition#edit-text-custom)
>
> For examples and more details on customizing how edited tasks are regenerated, see the [documentation on editing text](https://prodi.gy/docs/named-entity-recognition#edit-text-custom) for named entity recognition, which works exactly the same way.

## Training a span categorizer

> **Warning: Important note**
>
> Keep in mind that the data you collect with [`spans.manual`](https://prodi.gy/docs/recipes#spans-manual) includes
> potentially overlapping spans, which can't be mapped to single token-based tags.
> This means that you **can't** use it to train a regular named entity recognizer
> or similar models that expect each token to have one label. If you're working
> with spaCy, you should train a
> [`SpanCategorizer`](https://spacy.io/api/spancategorizer). If you're using a
> custom implementation, make sure your model predicts scores and labels for
> spans, not token-based tags.

### Training a spaCy pipeline

The easiest way to run a quick experiment is to use Prodigy's [`train`](https://prodi.gy/docs/recipes#train)
workflow, which is a thin wrapper around spaCy's
[training](https://spacy.io/usage/training) API. The `--spancat` argument lets
you provide one or more datasets to train from. You can optionally provide
dedicated evaluation sets using the `eval:` prefix. If no evaluation set is
provided, a percentage of examples is held back for evaluation. If no custom
config is specified, Prodigy will take care of
[inferring the best-matching suggester](https://prodi.gy/docs/span-categorization#suggesters-inference) to use based on
the data.

```bash title="Example"
prodigy train ./model --spancat covid_articles,eval:covid_articles_eval
```

Instead of training with Prodigy, you can use the [`data-to-spacy`](https://prodi.gy/docs/recipes#data-to-spacy) command
to export a ready-to-use corpus and config for training with spaCy directly:

```text title="Example (excerpt)"
$ prodigy data-to-spacy ./corpus --spancat covid_articles
To use this data for training with spaCy, you can run:
python -m spacy train ./corpus/config.cfg --paths.train ./corpus/train.spacy --paths.dev ./corpus/dev.spacy
```

Predicted span annotations are available as a list of
[`Span`](https://spacy.io/api/span) objects via the
[`Doc.spans`](https://spacy.io/api/doc#spans), a dictionary of span groups. This
allows you to store multiple collections of spans from different sources on the
`Doc`. By default, annotations exported with Prodigy will use the key `"sc"` for
"**s**pan **c**ategorization":

```python title="Usage in spaCy"
nlp = spacy.load("./model")
doc = nlp("Septic shock and bacteremia were two independent risk factors.")
print(doc.spans)
# {'sc': [Septic shock, Septic shock, bacteremia, bacteremia]}
print([(span.text, span.label_) for span in doc.spans["sc"]])
# [('Septic shock', 'FACTOR'), ('Septic shock', 'CONDITION'), ('bacteremia', 'FACTOR'), ('bacteremia', 'CONDITION')]
```

### Exporting your annotations

The [`db-out`](https://prodi.gy/docs/recipes#db-out) exports annotations in a straightforward JSONL format. Spans
are provided as a list of `"spans"` with their respective start and end
character offsets, and the entity label. This should make it easy to convert and
use it to train any model. If you annotated using a manual interface, the data
will also include a `"tokens"` property so you can restore the tokenization.

```bash title="Example"
prodigy db-out covid_articles > ./annotations.jsonl
```
