Space aware span postprocessing

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

Space aware span postprocessing is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Clean up character-level span predictions by removing isolated space characters at span boundaries

How to use this skill

Skill file

Space-Aware Span Post-Processing

Overview

Tokenizers (especially BPE/RoBERTa) often include leading/trailing spaces in token offsets. This causes span predictions to include stray space characters at boundaries. Post-process by removing isolated space predictions and bridging spaces between positive predictions.

Quick Start

import numpy as np

def postprocess_spaces(predictions, text):
    """Fix space artifacts in character-level span predictions."""
    preds = np.copy(predictions)
    for i in range(1, len(text) - 1):
        if text[i] == " ":
            if preds[i] and not preds[i - 1]:    # leading space
                preds[i] = 0
            if preds[i] and not preds[i + 1]:    # trailing space
                preds[i] = 0
            if preds[i - 1] and preds[i + 1]:    # bridge gap
                preds[i] = 1
    return preds

Workflow

  1. Get binary character-level predictions (after thresholding)
  2. Scan for space characters
  3. Remove spaces at span boundaries (no positive neighbor on one side)
  4. Bridge spaces between two positive neighbors (merge adjacent 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.