Embedding aware punct normalization

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

Embedding aware punct normalization is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Use pretrained embedding vocab to decide which punctuation to keep, split, or remove before tokenization

How to use this skill

Skill file

Overview

Different pretrained embeddings tokenize punctuation differently. GoogleNews has a vector for & but not for ?. GloVe has vectors for ! and ?. Instead of applying uniform punctuation rules, query the embedding vocab to decide: (1) keep as a token if it has an embedding, (2) split around characters that act as word boundaries, (3) remove characters with no vector. This routinely pushes text coverage from ~90% to >99%.

Quick Start

def clean_text(x):
    x = str(x)
    # Split punctuation the embedding treats as word boundaries
    # (so 'foo/bar' becomes two tokens 'foo bar')
    for punct in "/-'":
        x = x.replace(punct, ' ')

    # Keep tokens known to have embeddings (surround with spaces so tokenizer splits them)
    for punct in '&':
        x = x.replace(punct, f' {punct} ')

    # Remove punctuation with no embedding coverage
    strip_chars = '?!.,"#$%\'()*+-/:;<=>@[\\]^_`{|}~' + '\u201c\u201d\u2018\u2019'
    for punct in strip_chars:
        x = x.replace(punct, '')
    return x

Workflow

  1. Build a dataset vocab frequency dict and check coverage against the pretrained embedding
  2. For each frequent OOV punctuation character, test: does the pretrained index contain it?
  3. Partition punctuation into 3 sets: keep-as-token, split-as-separator, remove
  4. Apply clean_text before tokenization
  5. Re-run coverage check — aim for > 99% text coverage

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.