Model Slug Extraction: Dictionary Lookup Over Pure Regex

When extracting structured identifiers (model names, product versions, package names) from unstructured text, a dictionary of known values with normalization beats regex-only extraction. Regex handles the general case; the dictionary handles the important cases correctly.

Tags

Model Slug Extraction: Dictionary Lookup Over Pure Regex

The Lesson

When extracting structured identifiers (model names, product versions, package names) from unstructured text, a dictionary of known values with normalization beats regex-only extraction. Regex handles the general case; the dictionary handles the important cases correctly.

Context

A data pipeline extracts model slugs (e.g., "claude-3-opus", "gpt-4o") from event titles and page content to link events to specific AI models. The initial implementation used regex patterns to match model name patterns — sequences of letters, digits, and hyphens that look like model identifiers. This worked for well-formatted titles but failed on real-world text where model names appear in unexpected formats.

What Happened

  1. Regex-only extraction worked for clean titles like "Announcing GPT-4o" but failed on CamelCase concatenations like "launchedClaude3.5Sonnet" (common in JavaScript-rendered pages where whitespace is stripped).
  2. A _repair_whitespace() function was added to insert spaces at CamelCase boundaries before regex matching. This fixed concatenated names but introduced false positives on non-model CamelCase words.
  3. A dictionary of ~50 known model names was built (_KNOWN_MODEL_NAMES), mapping display names to canonical slugs. The extraction pipeline tries the dictionary first, with normalization (lowercasing, collapsing hyphens/underscores/spaces), then falls back to regex on whitespace-repaired text.
  4. A backfill-slugs CLI command was added to scan existing events with NULL slugs and apply the improved extraction retroactively.
  5. The dictionary approach correctly handled 22 test cases covering whitespace repair, dictionary matching, regex fallback, HTML concatenation, and multi-model titles.

Key Insights

Examples

Regex-only (before):

Dictionary + normalization (after):

Applicability

Applies to any entity extraction task where there's a known universe of high-value entities: product names, package identifiers, API versions, stock tickers. Does NOT apply when the entity space is open-ended (e.g., extracting arbitrary person names) — there the dictionary can't cover enough ground to be useful.

Related Lessons