completed Jan 2024 - Jul 2025

Extending Context Length for Enhanced Entity Extraction in Legal Contracts

#Legal NLP #Entity Extraction #RoBERTa #Long-Context Transformers #Positional Interpolation #Attention Mechanisms
Extending Context Length for Enhanced Entity Extraction in Legal Contracts

Overview

Legal contracts are lengthy and complex, with critical information dispersed across multiple clauses and paragraphs. Automatically extracting legal entities such as parties, effective dates, governing law, license grants and liability clauses is vital for automating contract review, but encoder-based transformer models such as RoBERTa are limited to 512 input tokens. Long contracts must therefore be segmented into short chunks, fragmenting context and reducing extraction accuracy. Large language models can process longer inputs, but they are resource intensive and prone to hallucination, which limits their reliability for high-precision legal tasks.

This project proposes an efficient approach for enhancing context comprehension in encoder-based models without modifying their core architecture. RoBERTa's absolute position embeddings are extended from 512 to 1026 positions using linear and cubic positional interpolation, allowing the model to process twice as much text in a single window. The project further introduces a hybrid layer-wise attention window, in which global attention is applied in the early layers and local attention in the later layers, capturing the broad context of a document early while reducing computational cost in deeper layers. The extended model is fine-tuned as an extractive question-answering system on the Contract Understanding Atticus Dataset (CUAD) together with 100 newly annotated contracts.

Motivation & Objectives

Directly modifying architectures to support longer contexts requires extensive pretraining and high computational cost, while sparse-attention models such as Longformer and BigBird often underperform in attention-intensive domains like legal text. A lightweight encoder that can see more of a contract at once offers a cost-effective and reliable alternative for real-world legal workflows. The project pursued the following objectives:

Longer Context – To develop a model that processes contexts beyond 512 tokens in order to better comprehend lengthy legal contracts.

Efficiency – To train a lightweight model that reduces computational cost while maintaining high performance.

Robust Extraction – To ensure robust entity extraction across diverse legal contract types and entity categories, including those that are under-represented in existing datasets.

Impact & Results

Performance

Extending RoBERTa-base to 1026 tokens with linear interpolation and fine-tuning on 408 CUAD contracts raised the Area Under the Precision-Recall Curve (AUPR) from 42.6% to 52.7% and precision at 80% recall (P@0.8R) from 31.1% to 46.1% compared with the standard 512-token model. This outperforms the previous state-of-the-art ConReader-large by 3.6 percentage points in AUPR (52.7% vs. 49.1%) and 1.9 points in P@0.8R (46.1% vs. 44.2%), while using roughly one-third of the parameters (124M vs. 364M). CUAD-specific fine-tuning outperformed both the generic pretrained (50.5% AUPR) and SQuAD-fine-tuned (51.7% AUPR) variants. Replacing party samples with those from a dedicated Party Extraction Dataset increased AUPR to 53.5%, and adding the newly annotated entity dataset raised it further to 54.5% with 40.2% P@0.8R. In the layer-wise attention study, the Global-6 Local-6 configuration achieved 53.0% AUPR and 38.7% P@0.8R, nearly matching the fully global model, whereas placing local attention first or gradually changing window sizes degraded performance.

Key Contributions

Interpolation-Based Context Extension

Linear and cubic positional interpolation that extends RoBERTa's context window from 512 to 1026 tokens without architectural changes or costly re-pretraining, delivering state-of-the-art AUPR on CUAD.

Hybrid Layer-Wise Attention Window

An attention schedule that applies global attention in the first six layers and local attention in the last six, retaining near-full accuracy while reducing the quadratic cost of self-attention in deeper layers.

Newly Annotated Contract Dataset

A curated set of 100 annotated contracts targeting low-resource CUAD entities such as Change of Control, Uncapped Liability, Joint IP Ownership and IP Ownership Assignment, improving performance where existing data was scarce.

Contract Analysis Application and Codebase

A locally runnable entity extraction application that returns span-level answers with confidence scores and JSON export, together with a modular codebase supporting configurable context lengths and attention window strategies.

Real-world Impact

The work shows that interpolation-based context extension, combined with strategically scheduled attention, offers a lightweight yet powerful alternative to large generative models for long-document legal analysis. By reducing the likelihood of missing critical clauses with a model one-third the size of the previous state of the art, the approach makes automated contract review more practical for legal professionals and researchers, and its locally runnable application enables efficient offline contract analysis. The released codebase provides a foundation for further research on longer context windows (such as 2054 tokens) and resource-efficient legal AI.

Gallery

Project poster presented at the Final Year Project Showcase 2025

Project poster presented at the Final Year Project Showcase 2025

Methodology: interpolated position embeddings for RoBERTa and the end-to-end entity extraction pipeline

Methodology: interpolated position embeddings for RoBERTa and the end-to-end entity extraction pipeline

Results on CUAD compared with RoBERTa-base and ConReader-large

Results on CUAD compared with RoBERTa-base and ConReader-large

Project Team

Lithurshan Kanagalingam

Lithurshan Kanagalingam

Undergraduate Researcher

Focused on machine learning and natural language processing for long-document legal entity extraction.

Pairavi Thanancheyan

Pairavi Thanancheyan

Undergraduate Researcher

Focused on data science and NLP techniques for extending the context length of encoder-based transformer models.

Sangaran Thevarasa

Sangaran Thevarasa

Undergraduate Researcher

Focused on machine learning and software engineering for the legal contract entity extraction system.