Skip to content
Back to skills

Bpe Offset Char Alignment

ASecurity

Reconstruct character-level offsets for BPE tokens by decoding each token individually and accumulating lengths for precise span mapping

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
developmentpython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill bpe-offset-char-alignment --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Bpe Offset Char Alignment?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Bpe Offset Char Alignment
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-bpe-offset-char-alignment/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-bpe-offset-char-alignment)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-bpe-offset-char-alignment
description: Reconstruct character-level offsets for BPE tokens by decoding each token individually and accumulating lengths for precise span mapping
domain: nlp
---

# 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

```python
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

- **Decode-based**: more robust than relying on tokenizer's `.offsets()` which can be buggy
- **Leading spaces**: BPE tokens often include leading spaces — decoded length accounts for this
- **Overlap check**: a token belongs to the span if any part of it overlaps the character range
- **Prefix offset**: add prefix token count (CLS, SEP, query) when mapping to full input positions

## References

- Source: [tensorflow-roberta-0-705](https://www.kaggle.com/code/cdeotte/tensorflow-roberta-0-705)
- Competition: Tweet Sentiment Extraction

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.