How harshildarji/bundesrecht works
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: references.py References · lookup.py Lookup · __init__.py Bundesrecht API · test_bundesrecht.py Tests
Components 18
Public API
User-facing entry points and facade.
- Bundesrecht API · Public package surface exposing the main entry points.
bundesrecht/__init__.py - parse_reference · Structural citation parser entry point.
bundesrecht/__init__.py - normalise · Canonical citation normalisation entry point.
bundesrecht/__init__.py - Bundesrecht · Stateful corpus-backed lookup interface.
bundesrecht/__init__.py
Core pipeline
Parse, normalise, and resolve layers.
- References · Citation syntax, hierarchy, and reference data model.
bundesrecht/references.py - LawReference · Structured law citation object.
bundesrecht/references.py - ParagraphRef · Parsed paragraph-level reference and substructure.
bundesrecht/references.py - SubReference · Nested citation fragment for Absatz, Satz, Nummer, and similar parts.
bundesrecht/references.py - Normaliser · Expands abbreviations and splits references into canonical strings.
bundesrecht/normaliser.py - Lookup · Resolves normalised references against loaded law data.
bundesrecht/lookup.py - QueryResult · Resolution output with depth, note, matched law, and resolved text accessors.
bundesrecht/lookup.py
Corpus data
Pinned law corpus and load boundary.
Tooling
Tests, examples, CI, and release support.
- Tests · Repository test coverage for parsing, normalisation, and lookup behavior.
test_bundesrecht.py - Examples · Interactive notebooks demonstrating parsing, normalising, and resolving.
examples/resolving.ipynb - Eval · Evaluation scripts and reported results for reference handling quality.
eval/eval_bundesrecht.py - CI / Release · Workflow and release automation for validation and publishing.
.github/workflows/tests.yml
Connections 21
- 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.