Skip to content

Repository files navigation

Case File Analysis

A comprehensive Python library for parsing and analyzing case file documents using Large Language Models (LLMs). The library supports multiple parsing strategies, from structured JSON extraction to iterative knowledge graph construction, enabling flexible analysis of unstructured case file data.

Structure of this Repository

  • src/casefile_parser/: Contains source files for building the casefile_parser package, which is used by the Lambdas and Jupyter notebooks.
  • nibrs/specs/: Ontology specifications and a guide for developers can be found here.
  • nibrs/sample_reports/: Variations of the mark43 nibrs_AllFields_Sample.xml sample report in XML, JSON, and OpenCypher edge list formats can be found here.
  • nibrs/specs/data_element_maps/: Data element code taxonomic maps sourced from the Washington Association of Sheriffs and Police Chiefs can be found here.

nibrs diagram showing the structure of reports

Object Taxonomies

The NIBRS "Property" data element taxonomy in their dev guide is a bit restrictive and makes it difficult to place weapons, clothing, damaged/stolen goods, and other major categories of things within the same embedding space. This has given me endless headaches for the last year. Thankfully, Google's Product Taxonomy is great for this. Embedding an object within the shared feature space is then just an issue of locating the most relevant branch of the taxonomy. As such, the paths to leaves of version 2021-09-21 of the taxonomy have been one-hot encoded and stored in src/casefile_parser/taxonomies/OBJECTS.tsv

Features

  • Multi-Provider LLM Support: Seamlessly integrates with AWS Bedrock, Anthropic Claude, OpenAI GPT, and Cohere (I've only tested the most recent version with Claude)
  • Dual Parsing Strategies: JSON-based structured extraction and graph-based iterative enrichment
  • Schema-Driven Extraction: Configurable schemas ensure consistent and validated output
  • Iterative Enrichment: LangGraph-powered workflows for progressive knowledge discovery
  • Enterprise-Grade: Comprehensive error handling, logging, and monitoring
  • Batch Processing: Efficient processing of large document collections
  • Type Safety: Full type hints and Pydantic validation throughout

Documentation

For detailed documentation, API reference, and advanced usage examples, see:

Quick Start

Installation

# Install package in development mode with anthropic support
pip install -e git+https://github.com/IsaacFigNewton/case-file-analysis.git[anthropic]

Basic Usage

WARNING: THIS SECTION IS OUT-OF-DATE, see docs/, lambdas/, and examples/ for examples of usage.

JSON Parser (Structured Extraction)

from casefile_parser.parsers.json_parser import JSONParser

# Initialize parser
parser = JSONParser()

# Parse document
text = "Officer Smith responded to a robbery at 123 Main St..."
result = parser.call_llm(text)

# Access results
print(f"Case ID: {result.incident_case_id}")
print(f"Primary Offense: {result.primary_offense_type}")

Triplet Parser (Knowledge Graph) [Deprecated]

from casefile_parser.parsers.triplet_parser import TripletParser

# Initialize with schema
parser = TripletParser(schema_path="schemas/CaseFileSchema.csv")

# Parse and build graph
graph = parser.parse(text)

# Explore graph
for node in graph.data['nodes']:
    print(f"Entity: {node.id} (Type: {node.type})")

Configuration

Create a .env file in your project root based on .env.example:

# API Keys
ANTHROPIC_API_KEY=your_key_here
OPENAI_API_KEY=your_key_here
COHERE_API_KEY=your_key_here

# AWS Configuration
AWS_PROFILE=default
AWS_REGION=us-east-1

# Logging
LOG_LEVEL=INFO
LOG_FORMAT=json

Architecture

The library follows a modular architecture with clear separation of concerns:

  • Core Components: LLMProviderManager, CaseFileManager, TextPreprocessor
  • Parser Implementations: JSONParser for structured data, TripletParser for graph construction
  • Schema Management: Pydantic models for validation and type safety
  • Interfaces: Abstract base classes ensuring consistent APIs

Testing

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=src --cov-report=html --cov-report=term-missing

# Run specific categories
pytest -m unit          # Unit tests only
pytest -m integration   # Integration tests only
pytest -m "not slow"    # Skip slow tests

Test Structure

The comprehensive test suite covers:

  • Parser Tests: JSON and triplet parser functionality
  • Postprocessing Tests: Entity extraction, graph transformation, schema validation
  • Core Tests: File management, LLM provider integration, logging
  • Integration Tests: End-to-end pipeline testing

Prerequisites

pip install pytest pytest-cov

Project Structure

src/casefile_parser/
├── parsers/                    # Parser implementations
│   ├── json_parser/           # Structured JSON extraction
│   └── triplet_parser/        # Knowledge graph construction
├── interfaces/                # Abstract base classes
├── schemas/                   # Data models and validation
├── preprocessing/             # Text cleaning utilities
├── postprocessing/           # Graph processing utilities
└── utils/                    # Utility modules

tests/                         # Comprehensive test suite
├── parsers/                  # Parser-specific tests
├── postprocessing/           # Postprocessing tests
└── conftest.py              # Shared fixtures and configuration

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Write tests for your changes
  4. Ensure all tests pass (pytest)
  5. Commit changes (git commit -m 'Add amazing feature')
  6. Push to branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Development Setup

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode
pip install -e .[dev]

# Install pre-commit hooks
pre-commit install

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Contact

For questions, issues, or contributions, please:

  • Open an issue on GitHub
  • Contact the maintainers (Isaac)
  • Join our community discussions

About

Cleaned up and pushing what I have from the original repo

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages