Token to char span prediction

From wenmin-wu/ds-skills · View interactive version

Token to char span prediction is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Map token-level classifier outputs back to character-level spans via offset mapping, thresholding, and contiguous grouping

How to use this skill

Skill file

Token-to-Character Span Prediction

Overview

End-to-end pipeline for extracting text spans from token-level classifiers. Token probabilities are projected onto character positions using offset mappings, thresholded, and grouped into contiguous spans. Used in NER, clinical note annotation, and extractive QA.

Quick Start

import numpy as np
import itertools

def get_char_probs(texts, predictions, tokenizer):
    """Project per-token probabilities onto character positions."""
    results = [np.zeros(len(t)) for t in texts]
    for i, (text, pred) in enumerate(zip(texts, predictions)):
        enc = tokenizer(text, add_special_tokens=True, return_offsets_mapping=True)
        for (start, end), p in zip(enc["offset_mapping"], pred):
            results[i][start:end] = p
    return results

def get_spans(char_probs, threshold=0.5):
    """Threshold char probs and group consecutive indices into spans."""
    spans = []
    for probs in char_probs:
        indices = np.where(probs >= threshold)[0]
        groups = [list(g) for _, g in itertools.groupby(
            indices, key=lambda n, c=itertools.count(): n - next(c))]
        spans.append([(min(g), max(g) + 1) for g in groups])
    return spans

Workflow

  1. Run token-level classifier (e.g. DeBERTa) → sigmoid probabilities per token
  2. Map token probs → character probs via offset_mapping
  3. Sweep threshold on OOF data (typically 0.45–0.55)
  4. Group contiguous above-threshold characters into spans

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.