Bpe offset char alignment

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

Bpe offset char alignment is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Reconstruct character-level offsets for BPE tokens by decoding each token individually and accumulating lengths for precise span mapping

How to use this skill

Skill file

BPE Offset Character Alignment

Overview

When a tokenizer doesn't provide reliable character offsets (e.g. some HuggingFace tokenizers with added whitespace), reconstruct them by decoding each BPE token individually and tracking the cumulative character position. Then map character-level span labels to token-level start/end indices by checking which tokens overlap with the annotated span.

Quick Start

def compute_offsets(token_ids, tokenizer):
    """Reconstruct character offsets by decoding each token."""
    offsets = []
    idx = 0
    for tid in token_ids:
        word = tokenizer.decode([tid])
        offsets.append((idx, idx + len(word)))
        idx += len(word)
    return offsets

def char_span_to_token_span(offsets, char_start, char_end):
    """Map character-level span to token indices."""
    token_start, token_end = None, None
    for i, (a, b) in enumerate(offsets):
        if a < char_end and b > char_start:
            if token_start is None:
                token_start = i
            token_end = i
    return token_start, token_end

# Usage
enc = tokenizer.encode(text, add_special_tokens=False)
offsets = compute_offsets(enc.ids, tokenizer)

# Find where selected_text starts in the original text
char_start = text.find(selected_text)
char_end = char_start + len(selected_text)
tok_start, tok_end = char_span_to_token_span(offsets, char_start, char_end)

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.