Data Extraction via Semantic Regular Expression Synthesis

Qiaochu Chen, Arko Banerjee, Çagatay Demiralp, Greg Durrett, Isil Dillig · DOI

TL;DR

This paper introduces semantic regular expressions, a powerful extension of standard regex that combines syntactic pattern matching with semantic reasoning (e.g., "is this a European country?"). To make these usable, the authors developed SMORE, a neurosymbolic tool that uses Large Language Models (LLMs) to sketch the structure of a query and a symbolic synthesis engine to fill in the precise logical details.

What Problem Does This Paper Address?

Standard regular expressions (regex) are the workhorse of text processing, but they are "blind" to meaning. A regex can easily find a string that looks like a date (e.g., \d{2}/\d{2}/\d{4}), but it cannot easily determine if that date falls on a weekend or if a specific word refers to a "business entity" versus a "person."

Currently, developers facing these "semantic" extraction tasks usually have to write complex post-processing scripts or rely entirely on LLMs. Relying solely on LLMs is expensive, slow, and often "hallucinates" or fails to respect strict negative constraints. The challenge is to create a system that has the formal rigor of regex but the "world knowledge" of a language model.

Methods

The authors propose a neurosymbolic approach that splits the labor between a neural model (GPT-3) and a symbolic program synthesizer. The process follows three main steps:

  1. Neural Sketch Generation: Given a few positive examples, the system prompts an LLM to generate a typed sketch. Instead of a finished program, the LLM produces a template like {□ : Name} , {□ : Country}, where the boxes are "holes" that need to be filled with specific logic.
  2. Compositional Synthesis: The system decomposes the problem. Instead of trying to solve the whole regex at once, it breaks it down into sub-problems for each hole in the sketch.
  3. Type-Directed Search: SMORE uses a formal type system (with subtype polymorphism) to fill these holes. For instance, if a hole is typed as Year, the synthesizer will only search for logical predicates relevant to years (like < 1900).
  4. Sketch Repair: If the symbolic engine finds that the LLM's initial sketch is impossible to complete (e.g., the LLM thought a field was a City but the examples are actually Countries), SMORE performs "fault localization" to identify the error and asks the LLM for a corrected sketch.

Datasets

The authors evaluated SMORE on 50 data extraction tasks spanning 10 diverse domains:

  • Domains: Business, Sales, Retail, Marketing, Account, Stock, Science, Server, Museum, and Exhibition.
  • Task Examples: Extracting "Restaurants created before 2000," "Products with Intel CPUs and >8GB RAM," or "European artists born before the 20th century."
  • Benchmarks: The tasks were designed to require a mix of syntactic formatting (commas, dashes) and semantic knowledge (geography, dates, technical specs).

Key Insights and Ideas

  • The "Neurosymbolic" Sweet Spot: LLMs are excellent at identifying semantic categories (the "what") but struggle with precise boolean logic and negative examples. Symbolic synthesizers are the opposite. By using the LLM to provide the structure (the sketch) and the symbolic engine to provide the logic, the authors get the best of both worlds.
  • Typed Holes as Pruning: The most clever part of the system is using types to prune the search space. By labeling a hole as Semantic(Date), the system doesn't waste time trying to see if the string "1995" matches a PhoneNumber pattern.
  • Decomposition is Key: Traditional regex synthesis is NP-hard because the search space explodes. By using the LLM's sketch to "divide and conquer," SMORE turns a massive search problem into several tiny, manageable ones.

Key Results

  • Superior Accuracy: SMORE achieved an average F1 score of 0.87, significantly outperforming baselines like ChatGPT-Exec (0.65) and FlashGPT (0.58).
  • Efficiency: SMORE solved 48 out of 50 tasks. In comparison, a purely neural approach (ChatGPT-Synth) only solved 6/50, and a version of SMORE without the LLM sketching (Smore-NoSketch) solved only 12/50.
  • Speed: The use of sketches provided a 14x speedup compared to variants that didn't use typed holes, proving that the "hints" from the LLM are computationally vital.
  • Human Performance: In a user study with 13 participants (including professional engineers), users were significantly more effective at solving complex extraction tasks using semantic regexes than standard regexes.

Limitations and Future Work

  • Granularity Issues: The authors noted that the system sometimes struggles with high-granularity tasks. For example, an entity matcher might fail to recognize "Alice" as a person's name when it's part of a business name like "Alice Chinese Bistro."
  • LLM Dependency: The system's "knowledge" is capped by the underlying LLM. If the LLM doesn't know a specific semantic category, the symbolic engine cannot invent it.
  • Future Directions: The authors suggest extending the DSL to handle more complex data structures beyond strings and improving the "Sketch Repair" mechanism to handle even more ambiguous user intent.

Summary updated

BibTeX
@article{chen2023data,
  author = {Qiaochu Chen and Arko Banerjee and Çagatay Demiralp and Greg Durrett and Isil Dillig},
  title = {{Data Extraction via Semantic Regular Expression Synthesis}},
  journal = {Proc. ACM Program. Lang.},
  year = {2023},
  doi = {10.1145/3622863},
}

Loading related content...

Figure