code / legex /scrapers /au.py
anonymous
[code] Initial release of the code.
6f5156a
import logging
import re
from datetime import date
from datasets import load_dataset
from legex.models.base import Case
from legex.scrapers.base import BaseScraper
DATASET_NAME = "isaacus/high-court-of-australia-cases"
log = logging.getLogger(__name__)
_RE_CITATION = re.compile(r"\[(\d{4})\]\s*HCA\s*(\d+)")
# HCA docket, for example"S94/2025", "P7/2024", "B15/2024". Registry letters seen: S/B/C/P/M/H/D.
_RE_DOCKET = re.compile(r"\b([SBCPMHD])(\d+)/(\d{4})\b")
HCOURT_URL_TEMPLATE = "https://www.hcourt.gov.au/cases-and-judgments/cases/decided/case-{letter}{num}{year}"
class AUScraper(BaseScraper):
country = "Australia"
def scrape(
self,
start_date: date | None = None,
end_date: date | None = None,
) -> list[Case]:
log.info(f"Loading {DATASET_NAME} …")
dataset = load_dataset(DATASET_NAME, split="corpus")
cases = [self._row_to_case(row) for row in dataset]
cases = [c for c in cases if c is not None]
if start_date or end_date:
cases = [
c for c in cases
if (start_date is None or c.decision_date >= start_date)
and (end_date is None or c.decision_date <= end_date)
]
cases.sort(key=lambda c: c.decision_date or date.min, reverse=True)
log.info(f"{len(cases)} cases after filtering")
return cases
@staticmethod
def _row_to_case(row: dict) -> Case | None:
raw_date = row.get("date")
if not raw_date:
return None
citation = row.get("citation") or ""
m = _RE_CITATION.search(citation)
case_id = f"[{m.group(1)}] HCA {m.group(2)}" if m else citation or None
decision_date = raw_date.date() if hasattr(raw_date, "date") else date.fromisoformat(str(raw_date)[:10])
# We prefer the authoritative hcourt.gov.au per-case URL, built from the docket
# number found in the first 2000 chars of the decision text. This gives around 95% hit rate.
# For the remaining ~5% with no docket match we fall back to the dataset URL.
text = row.get("text") or ""
d = _RE_DOCKET.search(text[:2000])
if d:
docket = f"{d.group(1)}{d.group(2)}/{d.group(3)}"
link = HCOURT_URL_TEMPLATE.format(
letter=d.group(1).lower(), num=d.group(2), year=d.group(3)
)
else:
docket = ""
link = row.get("url")
return Case(
case_id=case_id,
link=link,
decision_date=decision_date,
jurisdiction="au",
language="en",
full_text=text or None,
metadata={"citation": citation, "docket": docket},
)
@staticmethod
def civil_filter(cases: list[Case]) -> list[Case]:
# HCA is mixed civil/criminal/constitutional. Exclude the explicit criminal
# pattern "v The King" / "v The Queen", everything else we treat as civil.
return [
c for c in cases
if " v The King" not in (c.metadata or {}).get("citation", "")
and " v The Queen" not in (c.metadata or {}).get("citation", "")
]