Code4Struct: Code Generation for Few-Shot Event Structure Prediction

Xingyao Wang, Sha Li, Heng Ji

Abstract

Event Argument Extraction (EAE) is a critical task in natural language processing that involves identifying structured information from unstructured text. Traditional approaches often struggle with complex, hierarchical event structures in low-resource settings. This paper introduces Code4Struct, a framework that reimagines EAE as a code generation task. By mapping event ontologies to Python class definitions, Code4Struct leverages the structural reasoning capabilities of code-trained Large Language Models (LLMs). Experimental results on the ACE05-E benchmark demonstrate that Code4Struct outperforms previous state-of-the-art models by nearly 30% in few-shot scenarios, rivaling supervised models trained on significantly larger datasets.

Introduction

The goal of Event Argument Extraction (EAE) is to determine "who did what to whom, where, and when." For instance, given the sentence "Kelly arrived in Seoul from Beijing," an EAE system must identify "Kelly" as the agent, "Seoul" as the destination, and "Beijing" as the origin of a "Transport" event.

While Large Language Models (LLMs) show promise in few-shot learning, they often fail to adhere to the rigid, hierarchical constraints required for structured output when prompted with natural language. This "structural gap" between messy human language and precise database schemas remains a significant challenge. Code4Struct addresses this by using programming language (PL) syntax as a bridge.

Methods

The core innovation of Code4Struct is the translation of an event ontology into a Python script. This approach moves beyond simple prompt engineering toward "structure engineering."

Overview of the Code4Struct framework: from Event Ontology to Event Instantiation via code generation.

Ontology as Code

Every event type in the ontology is converted into a Python class. This allows the framework to exploit several key features of programming languages:

  • Inheritance: Hierarchical relationships (e.g., a Transport event being a subtype of Movement) are represented via class inheritance, enabling knowledge sharing across related event types.
  • Type Annotations: Python type hints (e.g., List[GPE | ORG | PER]) provide explicit constraints on which entity types can fill specific roles.
  • Docstrings: Natural language descriptions are embedded within class docstrings to provide semantic context to the LLM.

Prompting for Instantiation

To extract arguments, the system constructs a prompt containing these class definitions and a small number of examples. The LLM is then tasked with "completing the code" by instantiating the appropriate class with entities extracted from the target sentence.

Evaluation

The framework was evaluated on the ACE05-E (Automatic Content Extraction 2005) dataset, which features a complex hierarchy of 33 event types. The researchers focused on few-shot performance, specifically the 20-shot setting.

Results and Discussion

Code4Struct demonstrates a significant "structural prior" in code-trained LLMs like OpenAI's Codex. These models are inherently better at following the rigid syntax of a Python class than following equivalent natural language instructions.

Performance of Code4Struct across different LLMs and k-shot settings on the ACE05-E dataset.

Key Findings

  • Superior Few-Shot Performance: In a 20-shot setting, Code4Struct (using text-davinci-003) achieved a 29.5% absolute F1 score improvement over the previous state-of-the-art model, DEGREE.
  • Data Efficiency: Remarkably, the 20-shot model performed comparably to supervised models trained on the entire ACE05-E dataset (over 4,200 instances), suggesting that structure is a powerful substitute for data volume.
  • Zero-Shot Transfer via Inheritance: The inheritance feature allowed the model to predict arguments for unseen event types by leveraging examples from "sibling" classes, boosting zero-shot performance by 12% absolute F1.

Conclusion

Code4Struct provides a robust method for bridging the gap between natural language and structured data. By framing extraction as code generation, it effectively utilizes the structural reasoning of LLMs. Future work will explore expanding this framework to deeper ontologies and other structured tasks like relation prediction, as well as addressing the inference costs associated with large-scale LLMs.

Summary updated

BibTeX
@inproceedings{wang2023code,
  author = {Xingyao Wang and Sha Li and Heng Ji},
  title = {{Code4Struct: Code Generation for Few-Shot Event Structure Prediction}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  url = {https://openreview.net/forum?id=Xqkq9DKCBN0},
}
Figure