# harshildarji/bundesrecht architecture

> Architecture diagram of the GitHub repository harshildarji/bundesrecht, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/harshildarji/bundesrecht
- Repository: https://github.com/harshildarji/bundesrecht

## Overview

1. **Purpose and top-level shape**
   - This repository is a small, dependency-free Python library for German federal-law citations.
   - Its architecture is intentionally layered: **parse → normalise → resolve**.
   - The public package surface is centered in `bundesrecht/__init__.py`, with most logic split across `references.py`, `normaliser.py`, `lookup.py`, and `_corpus.py`.

2. **Public API boundary**
   - The repo exposes three main entry points:
     - `parse_reference()` for structural parsing only,
     - `normalise()` for canonicalizing citation strings,
     - `Bundesrecht()` for corpus-backed lookup and resolution.
   - This is the core architectural boundary: everything else supports one of these three functions or the data types they return.

3. **Parsing subsystem**
   - `bundesrecht/references.py` defines the citation data model and parse logic.
   - It converts raw law citation text into structured objects: `LawReference`, `ParagraphRef`, and `SubReference`.
   - This layer does not require corpus data; it only understands citation syntax and hierarchy such as paragraph, Absatz, Satz, Nummer, Buchstabe, ranges, `ff.`, `f.`, and `i.V.m.`-style links.

4. **Normalisation subsystem**
   - `bundesrecht/normaliser.py` builds on the parser to expand and canonicalize references into clean, queryable strings.
   - It handles abbreviation expansion and multi-target splitting, such as:
     - `S.` → `Satz`
     - `i.V.m.` variants
     - comma-separated subreferences
     - paragraph ranges like `§§ 12-15`
     - `f.` and optionally `ff.`
   - Conceptually, this layer is a transformation pipeline from parsed structure to a list of canonical citation strings.

5. **Corpus and domain data model**
   - `bundesrecht/_corpus.py` is the corpus loading and validation layer.
   - It owns the law dataset shape and produces `LawData` objects, which represent one statute plus metadata, sections, footnotes, and source information.
   - The README shows the corpus is loaded from a cached `gesetze.jsonl` file keyed by the package-pinned dataset commit, so the library’s resolution behavior is tied to a specific external corpus version.

6. **Resolution subsystem**
   - `bundesrecht/lookup.py` is the corpus-backed resolver.
   - `Bundesrecht.query()` first normalises input, then resolves each canonical reference against loaded law data.
   - Resolution is hierarchical: it tries to match section, then Absatz, then Satz, then Nummer, then Buchstabe, then Unterbuchstabe, returning a `QueryResult` that records how far it got.

7. **Result objects and resolution metadata**
   - `QueryResult` is the main output of resolution.
   - It packages together the parsed reference, the matched law data, the resolved section dict, the specific resolved paragraph object, and derived text accessors like `full_text()` and `titel()`.
   - The `resolved_depth` and `resolution_note` fields are important architectural signals: they show that lookup is not strictly binary; partial resolution is a first-class outcome.

8. **Data flow from input to answer**
   - For parse-only use: raw string → `parse_reference()` → `LawReference`.
   - For canonicalisation: raw string → parse/expand in `normaliser.py` → list of canonical strings.
   - For lookup: raw string → normalise → resolve against `Bundesrecht` corpus → `QueryResult` objects.
   - This separation means the parsing/normalisation stack can be used independently of the dataset, while lookup is the only subsystem that depends on corpus availability.

9. **Corpus loading and caching boundary**
   - The package does not bundle the full laws dataset inside the wheel.
   - On first use, `Bundesrecht()` loads a commit-pinned corpus from a local cache or downloads the exact matching `gesetze.jsonl` from Hugging Face.
   - The cache root is configurable via `BUNDESRECHT_CACHE_DIR`, and a user can bypass the network entirely with `local_path=...`.
   - This is an important deployment boundary: code ships via PyPI, data ships separately and is validated at load time.

10. **Repository validation and quality gates**
   - `test_bundesrecht.py` and `conftest.py` indicate a conventional test suite for the parsing/normalisation/resolution behavior.
   - `.github/workflows/tests.yml` and `release.yml` show CI/CD automation around validation and packaging/release.
   - `scripts/release.sh` suggests release orchestration is scripted, while `pyproject.toml` is the package metadata and build configuration anchor.

11. **Examples and evaluation artifacts**
   - The `examples/` notebooks document intended use cases for parsing, normalising, resolving, and deduplication/provision handling.
   - The `eval/` directory contains benchmark/evaluation code and result reporting, so the project also tracks quality of reference handling, not just API correctness.
   - These are not runtime components, but they help infer expected system behavior and edge cases.

12. **Architectural summary for diagramming**
   - A good diagram would show:
     - a **pure parsing layer** at the bottom,
     - a **normalisation layer** above it,
     - a **dataset-backed resolution layer** above that,
     - and an external **Hugging Face JSONL corpus** as the data source for resolution.
   - The public API should be shown as three entry points mapped onto those layers, with `Bundesrecht` as the stateful facade over cached corpus data.

Main entry points: [`bundesrecht/references.py`](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py) (References), [`bundesrecht/lookup.py`](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/lookup.py) (Lookup), [`bundesrecht/__init__.py`](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py) (Bundesrecht API), [`test_bundesrecht.py`](https://github.com/harshildarji/bundesrecht/blob/main/test_bundesrecht.py) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_pkg_init["Bundesrecht API<br/>package facade<br/>[__init__.py]"]
  node_parse_reference["parse_reference<br/>parse entrypoint<br/>[__init__.py]"]
  node_normalise_fn["normalise<br/>[__init__.py]"]
  node_bundesrecht_class["Bundesrecht<br/>resolver facade<br/>[__init__.py]"]
end

subgraph group_core["Core pipeline"]
  node_references["References<br/>citation parser<br/>[references.py]"]
  node_law_reference["LawReference<br/>reference model<br/>[references.py]"]
  node_paragraph_ref["ParagraphRef<br/>subreference model<br/>[references.py]"]
  node_sub_reference["SubReference<br/>[references.py]"]
  node_normaliser["Normaliser<br/>citation transformer<br/>[normaliser.py]"]
  node_lookup["Lookup<br/>corpus resolver<br/>[lookup.py]"]
  node_query_result["QueryResult<br/>resolution result<br/>[lookup.py]"]
end

subgraph group_data["Corpus data"]
  node_corpus[("Corpus<br/>dataset loader<br/>[_corpus.py]")]
  node_law_data["LawData<br/>law record<br/>[_corpus.py]"]
  node_jsonl_dataset["gesetze.jsonl<br/>external corpus"]
end

subgraph group_tooling["Tooling"]
  node_tests["Tests<br/>validation suite"]
  node_examples["Examples<br/>usage notebooks<br/>[resolving.ipynb]"]
  node_eval["Eval<br/>benchmarking"]
  node_ci["CI / Release<br/>automation<br/>[tests.yml]"]
end

node_pkg_init -->|"exports"| node_parse_reference
node_pkg_init -->|"exports"| node_normalise_fn
node_pkg_init -->|"exports"| node_bundesrecht_class
node_parse_reference -->|"uses"| node_references
node_normalise_fn -->|"parses"| node_references
node_normalise_fn -->|"delegates"| node_normaliser
node_bundesrecht_class -->|"queries"| node_lookup
node_lookup -->|"normalises"| node_normaliser
node_lookup -->|"loads"| node_corpus
node_corpus -->|"builds"| node_law_data
node_corpus -->|"reads"| node_jsonl_dataset
node_lookup -->|"returns"| node_query_result
node_references -->|"defines"| node_law_reference
node_references -->|"defines"| node_paragraph_ref
node_references -->|"defines"| node_sub_reference
node_tests -->|"verifies"| node_references
node_tests -->|"verifies"| node_normaliser
node_tests -->|"verifies"| node_lookup
node_examples -->|"demonstrates"| node_pkg_init
node_eval -->|"benchmarks"| node_lookup
node_ci -->|"runs"| node_tests

click node_pkg_init "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py"
click node_parse_reference "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py"
click node_normalise_fn "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py"
click node_bundesrecht_class "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py"
click node_references "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py"
click node_law_reference "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py"
click node_paragraph_ref "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py"
click node_sub_reference "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py"
click node_normaliser "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/normaliser.py"
click node_lookup "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/lookup.py"
click node_query_result "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/lookup.py"
click node_corpus "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/_corpus.py"
click node_law_data "https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/_corpus.py"
click node_tests "https://github.com/harshildarji/bundesrecht/blob/main/test_bundesrecht.py"
click node_examples "https://github.com/harshildarji/bundesrecht/blob/main/examples/resolving.ipynb"
click node_eval "https://github.com/harshildarji/bundesrecht/blob/main/eval/eval_bundesrecht.py"
click node_ci "https://github.com/harshildarji/bundesrecht/blob/main/.github/workflows/tests.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pkg_init,node_parse_reference,node_normalise_fn,node_bundesrecht_class toneBlue
class node_references,node_law_reference,node_paragraph_ref,node_sub_reference,node_normaliser,node_lookup,node_query_result toneAmber
class node_corpus,node_law_data,node_jsonl_dataset toneMint
class node_tests,node_examples,node_eval,node_ci toneRose
```

## Components

### Public API

User-facing entry points and facade.

- [Bundesrecht API](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py): package facade · Public package surface exposing the main entry points. · `bundesrecht/__init__.py`
- [parse_reference](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py): parse entrypoint · Structural citation parser entry point. · `bundesrecht/__init__.py`
- [normalise](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py): normalise entrypoint · Canonical citation normalisation entry point. · `bundesrecht/__init__.py`
- [Bundesrecht](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/__init__.py): resolver facade · Stateful corpus-backed lookup interface. · `bundesrecht/__init__.py`

### Core pipeline

Parse, normalise, and resolve layers.

- [References](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py): citation parser · Citation syntax, hierarchy, and reference data model. · `bundesrecht/references.py`
- [LawReference](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py): reference model · Structured law citation object. · `bundesrecht/references.py`
- [ParagraphRef](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py): subreference model · Parsed paragraph-level reference and substructure. · `bundesrecht/references.py`
- [SubReference](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/references.py): subreference model · Nested citation fragment for Absatz, Satz, Nummer, and similar parts. · `bundesrecht/references.py`
- [Normaliser](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/normaliser.py): citation transformer · Expands abbreviations and splits references into canonical strings. · `bundesrecht/normaliser.py`
- [Lookup](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/lookup.py): corpus resolver · Resolves normalised references against loaded law data. · `bundesrecht/lookup.py`
- [QueryResult](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/lookup.py): resolution result · Resolution output with depth, note, matched law, and resolved text accessors. · `bundesrecht/lookup.py`

### Corpus data

Pinned law corpus and load boundary.

- [Corpus](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/_corpus.py): dataset loader · Loads, validates, and caches statute data. · `bundesrecht/_corpus.py`
- [LawData](https://github.com/harshildarji/bundesrecht/blob/main/bundesrecht/_corpus.py): law record · Single statute record with metadata, sections, footnotes, and source data. · `bundesrecht/_corpus.py`
- **gesetze.jsonl**: external corpus · Pinned external JSONL corpus used for resolution.

### Tooling

Tests, examples, CI, and release support.

- [Tests](https://github.com/harshildarji/bundesrecht/blob/main/test_bundesrecht.py): validation suite · Repository test coverage for parsing, normalisation, and lookup behavior. · `test_bundesrecht.py`
- [Examples](https://github.com/harshildarji/bundesrecht/blob/main/examples/resolving.ipynb): usage notebooks · Interactive notebooks demonstrating parsing, normalising, and resolving. · `examples/resolving.ipynb`
- [Eval](https://github.com/harshildarji/bundesrecht/blob/main/eval/eval_bundesrecht.py): benchmarking · Evaluation scripts and reported results for reference handling quality. · `eval/eval_bundesrecht.py`
- [CI / Release](https://github.com/harshildarji/bundesrecht/blob/main/.github/workflows/tests.yml): automation · Workflow and release automation for validation and publishing. · `.github/workflows/tests.yml`

## Connections

- Bundesrecht API → parse_reference: exports · Public API exposes parse-only entry point.
- Bundesrecht API → normalise: exports · Public API exposes canonicalisation entry point.
- Bundesrecht API → Bundesrecht: exports · Public API exposes resolver facade.
- parse_reference → References: uses · Parsing is implemented by the citation parser module.
- normalise → References: parses · Normalisation starts from structured parsed references.
- normalise → Normaliser: delegates · Normalisation logic expands and canonicalises citation strings.
- Bundesrecht → Lookup: queries · Facade delegates resolution to lookup layer.
- Lookup → Normaliser: normalises · Lookup resolves each input after canonicalisation.
- Lookup → Corpus: loads · Resolver depends on the corpus loader and cache boundary.
- Corpus → LawData: builds · Corpus loader produces validated law records.
- Corpus → gesetze.jsonl: reads · Corpus is sourced from the pinned external JSONL dataset.
- Lookup → QueryResult: returns · Resolution produces rich result objects with partial-match metadata.
- References → LawReference: defines · Parser defines the primary citation object.
- References → ParagraphRef: defines · Parser defines paragraph-level reference structure.
- References → SubReference: defines · Parser defines nested subreference structure.
- Tests → References: verifies · Tests cover parsing behavior.
- Tests → Normaliser: verifies · Tests cover normalisation behavior.
- Tests → Lookup: verifies · Tests cover corpus-backed resolution behavior.
- Examples → Bundesrecht API: demonstrates · Example notebooks illustrate public API usage.
- Eval → Lookup: benchmarks · Evaluation exercises resolver quality and edge cases.
- CI / Release → Tests: runs · Automated workflows validate the library.
