> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# ner

> Find labelled spans of text with a tagger.

Span tagging: the interface a concept extractor can be built on, apart from the LLM.

## `ner.base`

The span-tagger interface. Nothing here imports a model or an SDK.

#### `NerError`

The call cannot be answered: too many labels for the model, a model error, a bad input.

#### `Span`

| Field | Type | Default |
| - | - | - |
| `start` | `int` | required |
| `end` | `int` | required |
| `text` | `str` | required |
| `label` | `str` | required |
| `score` | `float` | required |

#### `SpanTagger`

Finds labelled spans in text, with no generation.

Construct with `SpanTagger(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `recognize(self, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] \| None]` | One list of spans per text, or None for a text the model cannot take (for example, longer than its fixed input). |
| `arecognize(self, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] \| None]` | — |

## `ner.candidates`

Spans as candidates: the join between a tagger and the extractor's grounding.

#### `spans_to_candidates`

```python theme={null}
spans_to_candidates(spans: Iterable[Span], label_to_type: Mapping[str, str], *, min_score: float = 0.0, describe: Callable[[Span], str] = <function _default_description at 0x1042a6660>) -> list[dict]
```

Candidate dicts (word, type, description, quote) with the shape of the extraction schema's candidate.

## `ner.fake`

A scripted tagger for tests: it finds the phrases it was given, under the labels it was given.

#### `FakeSpanTagger`

`rules`: label -> phrases to find (case-sensitive, every occurrence).

Construct with `FakeSpanTagger(rules: Mapping[str, Sequence[str]], *, labels_per_call: int \| None = None, max_chars: int \| None = None, score: float = 0.9)`.

| Method | What it does |
| - | - |
| `recognize(self, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] \| None]` | — |
| `arecognize(self, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] \| None]` | — |

## `ner.labels`

Working with a tagger that carries few labels per call: split the labels, run each group, merge the spans.

#### `arecognize_all`

```python theme={null}
arecognize_all(tagger: SpanTagger, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] | None]
```

#### `clashes`

```python theme={null}
clashes(a_start: int, a_end: int, b_start: int, b_end: int, *, flat: bool) -> bool
```

Whether two spans (end exclusive, in any unit: characters or words) cannot both be kept.

#### `merge_spans`

```python theme={null}
merge_spans(groups: Iterable[Sequence[Span]], *, flat: bool = True) -> list[Span]
```

One list of spans that do not clash, from several lists (for example, one per group of labels).

#### `recognize_all`

```python theme={null}
recognize_all(tagger: SpanTagger, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] | None]
```

`tagger.recognize` for any number of labels.

## `ner.litert_gliner`

GLiNER as a LiteRT model on this machine: zero-shot NER (any labels you name, no training) from a `.tflite`, as a `SpanTagger`.

#### `Encoded`

| Field | Type | Default |
| - | - | - |
| `arrays` | `dict[str, np.ndarray]` | required |
| `n_words` | `list[int]` | required |
| `texts` | `list[str]` | required |
| `starts` | `list[list[int]]` | required |
| `ends` | `list[list[int]]` | required |

#### `LiteRtGliner`

`SpanTagger` over a GLiNER `.tflite`.

Construct with `LiteRtGliner(tflite_path: str \| pathlib.Path, *, tokenizer: str \| pathlib.Path = 'gliner-community/gliner_small-v2.5', cache_dir: str \| pathlib.Path \| None = None, device: str = 'gpu', local_files_only: bool = False, encoder_kind: Literal['auto', 'tokenizers', 'gliner'] = 'auto', runtime: _Runtime \| None = None, encoder: _Encoder \| None = None)`.

| Method | What it does |
| - | - |
| `(property)` | — |
| `(property)` | — |
| `(property)` | — |
| `(property)` | — |
| `recognize(self, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] \| None]` | — |
| `arecognize(self, texts: Sequence[str], labels: Sequence[str], *, threshold: float = 0.5, flat: bool = True) -> list[list[Span] \| None]` | — |

#### `decode_spans`

```python theme={null}
decode_spans(logits: numpy.ndarray, n_words: int, n_labels: int, threshold: float, flat: bool) -> list[tuple[int, int, int, float]]
```

logits: (words, max\_width, label\_slots) of one text.

#### `to_spans`

```python theme={null}
to_spans(words: list[tuple[int, int, int, float]], text: str, starts: list[int], ends: list[int], labels: list[str]) -> list[Span]
```

Word spans to character spans of `text`.

## `ner.tokenizers_encoder`

GLiNER's input preparation without PyTorch: the `tokenizers` library, a regex and numpy.

#### `TokenizersEncoder`

Implements the encoder protocol of `litert_gliner`.

Construct with `TokenizersEncoder(checkpoint: str \| pathlib.Path, *, cache_dir: str \| pathlib.Path \| None = None, local_files_only: bool = False)`.

| Method | What it does |
| - | - |
| `from_parts(cls, tokenizer: Any, config: dict) -> 'TokenizersEncoder'` | For tests: a ready tokenizer object and the config dict. |
| `encode(self, texts: list[str], labels: list[str])` | — |

## `ner.windows`

Text longer than a tagger can take: cut it into overlapping word windows, tag each, move the spans back, merge.

#### `recognize_windowed`

```python theme={null}
recognize_windowed(tagger: SpanTagger, text: str, labels: Sequence[str], *, size: int = 10, overlap: int = 2, threshold: float = 0.5, flat: bool = True) -> list[Span]
```

The spans of one long text, in its own coordinates.

#### `word_windows`

```python theme={null}
word_windows(text: str, size: int, overlap: int = 2) -> list[tuple[int, str]]
```

\[(character offset of the window in `text`, window text)], `size` words each, stepping `size - overlap` words.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.