Sponsored by Deepsite.site

Portuguese Legal Document PDF Metadata Extractor

Created By
geek2geeks6 months ago
MCP server for extracting metadata from Portuguese legal documents using advanced PDF processing and database architecture
Content

Portuguese Legal Document PDF Metadata Extractor

A robust Python tool for extracting structured metadata from Portuguese legal document PDFs, specifically designed for European Case Law Identifier (ECLI) formatted documents.

🚀 Features

  • High Accuracy: 100% confidence score with 96.84% exact match rate
  • Production Ready: Two extractor variants optimized for different use cases
  • Robust Error Handling: Comprehensive validation and error recovery
  • Flexible Confidence Scoring: Works with or without ground truth data
  • User-Friendly Interface: Clear progress reporting and detailed feedback
  • Field Classification: Distinguishes between missing and legitimately empty fields

📁 Project Structure

├── production_extractor.py    # Production-ready extractor with user-friendly interface
├── robust_extractor.py        # Core robust extraction engine
├── run_test1.py              # Test runner for batch processing
├── ground_truth/             # Ground truth data for validation
│   └── ground_truth.json
├── pdfs/                     # Input PDF documents
│   ├── test1/               # Test subset
│   └── *.pdf                # Legal documents
├── IMPROVEMENTS_SUMMARY.md   # Performance improvements documentation
└── README.md                # This file

🔧 Installation

Prerequisites

  • Python 3.8+
  • Required packages:
pip install pdfplumber

Setup

  1. Clone or download the project files
  2. Install dependencies:
    pip install pdfplumber
    
  3. Ensure your PDF files are in the pdfs/ directory

📖 Usage

Basic Usage with Production Extractor

The PortugueseLegalPDFExtractor class provides a user-friendly, production-ready interface:

from production_extractor import PortugueseLegalPDFExtractor

# Initialize extractor with optional ground truth validation
extractor = PortugueseLegalPDFExtractor(
    ground_truth_path="ground_truth/ground_truth.json",  # Optional
    verbose=True  # Enable progress reporting
)

# Extract metadata from a single PDF
result = extractor.extract_metadata("pdfs/document.pdf")

# Process multiple PDFs in a directory
summary = extractor.extract_batch(
    pdf_directory="pdfs/",
    output_directory="results/"  # Optional - saves individual results
)

Command Line Interface

The production extractor includes a full CLI:

# Process single file
python production_extractor.py "pdfs/document.pdf" -o "results/"

# Process entire directory with ground truth validation
python production_extractor.py "pdfs/" -g "ground_truth/ground_truth.json" -o "results/"

# Quiet mode (suppress progress messages)
python production_extractor.py "pdfs/" -q

# Force single file processing
python production_extractor.py "pdfs/document.pdf" --single

📊 Performance Metrics

Current Performance (After Improvements)

  • Overall Confidence: 100.0%
  • Exact Match Rate: 96.84% (153/158 populated fields)
  • Acceptable Match Rate: 100.0%
  • Processing Speed: ~2-3 seconds per document

Field Classification

  • Data Fields: 14 fields with actual content
  • Empty Fields: 8 fields correctly identified as empty
  • Match Types: Exact, case differences, punctuation differences, partial matches

🎯 How It Works

Pattern Recognition

The extractor leverages discovered patterns in Portuguese legal documents:

  1. Fixed Relative Positions: Fields appear in predictable locations
  2. Synchronized Pairs: Left-right column field synchronization
  3. Predictable Order: Consistent field sequence across documents
  4. Table Structure: Metadata organized in structured tables

Confidence Calculation

Two confidence calculation modes:

  1. With Ground Truth: Accuracy-based scoring against known correct values
  2. Without Ground Truth: Heuristic-based scoring using extraction quality indicators

🛠️ Development

Key Components

PortugueseLegalPDFExtractor (production_extractor.py)

Main Features:

  • Dual Confidence Modes: Accuracy-based (with ground truth) and population-based (without)
  • Multiple Extraction Methods: Table-based (primary) and coordinate-based (fallback)
  • Comprehensive Validation: Field validation, ECLI extraction, date formatting
  • Batch Processing: Process entire directories with detailed summaries
  • Command Line Interface: Full CLI with flexible options
  • Progress Reporting: User-friendly status messages and error handling

📈 Recent Improvements

Version 2.0 Enhancements

  1. Fixed Confidence Calculation: Improved from 44.4% to 100.0% by properly handling empty fields
  2. Enhanced Field Classification: Clear distinction between missing and legitimately empty fields
  3. Per-Field Confidence: Individual confidence scores for each extracted field

See IMPROVEMENTS_SUMMARY.md for detailed information.

📄 License

This project is provided as-is for educational and research purposes.


Last updated: January 2025

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
CursorThe AI Code Editor
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
ChatWiseThe second fastest AI chatbot™
DeepChatYour AI Partner on Desktop
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
WindsurfThe new purpose-built IDE to harness magic
Playwright McpPlaywright MCP server
Amap Maps高德地图官方 MCP Server
Serper MCP ServerA Serper MCP Server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Tavily Mcp