Words To Data - Convert Legal Documents Into Diffable Data Structures
Overview
words_to_data parses US Code titles and Public Laws (bills) from USLM XML format, providing structured access to legislative text, the ability to track changes between document versions, and tools for annotating how bills amend existing law.
Available for both Rust and Python with high-performance Rust core and ergonomic Python bindings via PyO3.
Features
- Parse USC and Public Law documents - Extract hierarchical structure from USLM XML files
- Rich text content - Capture heading, chapeau, proviso, content, and continuation fields
- Bill amendment extraction - Identify USC references and amending actions from bills
- Hierarchical diffing - Compute word-level differences between document versions
- Legal diff annotations - Link bill amendments to corresponding USC changes with verification status tracking
- Amendment similarity scoring - Calculate similarity between bill amendments for analysis
- Python bindings - Full API access from Python with PyO3
Installation
Rust
Add to your Cargo.toml:
[]
= "0.2.0"
Python
Note: Pre-built wheels are available for Linux x86_64. Other platforms will build from source (requires Rust toolchain).
Getting Data
- Title data: https://uscode.house.gov/download/download.shtml
- Bill data: https://congress.gov
Quick Start
Parse a US Code Document
Rust:
use parse;
Python:
=
=
Compute a Diff Between Versions
Rust:
use ;
Python:
=
=
=
=
Extract Amendments from a Bill
Rust:
use parse_bill_amendments;
Python:
=
Core Concepts
USLM Elements
Documents are represented as trees of USLMElement structures. Each element contains:
- ElementData: Metadata, text content, and identification
- Children: Nested child elements forming the document hierarchy
The library uses two types of paths:
-
Structural Path: Full hierarchy including all elements Example:
uscodedocument_26/title_26/subtitle_A/chapter_1/section_174 -
USLM ID: Official USLM identifier (excludes structural-only elements) Example:
/us/usc/t26/s174/a/1
Text Content Fields
Each element can contain up to five distinct text fields:
- Heading: Section or subsection title
- Chapeau: Opening text before enumerated items
- Proviso: Conditional or qualifying clauses
- Content: Main body text
- Continuation: Text appearing after child elements
Diffs
The TreeDiff structure mirrors the element hierarchy and tracks:
- Field changes: Word-level differences in text content fields
- Added elements: New child elements in the newer version
- Removed elements: Elements that existed in the older version
- Child diffs: Recursive diffs for matching child elements
Diffs are computed using word-level granularity via the similar crate.
Legal Diff Annotations
The LegalDiff structure wraps a TreeDiff and adds an annotation layer for linking code changes to their legislative source:
- ChangeAnnotation: Links one or more diff paths to a bill amendment
- BillReference: Identifies the bill and specific amendment text that caused a change
- AnnotationMetadata: Tracks verification status, confidence scores, annotator identity, and reasoning
- AnnotationStatus:
Pending,Verified,Disputed, orRejected
This enables building training datasets for ML models that predict how bills will modify existing law.
Amendment Actions
Bills can perform these operations on existing code:
Amend, Add, Delete, Insert, Redesignate, Repeal, Move, Strike, StrikeAndInsert
Annotator Tool
The annotator/ directory contains a Tauri desktop application for manually creating training datasets. It allows annotators to:
- Load old and new USC XML versions alongside a bill
- Select amendments from the bill
- Highlight the specific text that causes each change
- Link amendments to affected code sections
- Export annotations as JSON
See annotator/README.md for setup instructions.
Note: The annotator is an early prototype and may change significantly.
API Documentation
Rust
Generate and view the full API documentation:
Development
# Run Rust tests
# Build and install Python bindings locally
# Run Python tests
License
MIT