Documentation

Law library

Summary

  • The app quotes an article only from its local copy of the official Arabic texts, never from a model’s memory: a 4-billion-parameter model cannot recall article numbers or wording reliably.
  • 1,361 articles are loaded today: the complete Family Code (227) and the complete CPCA (1,084), 33 articles of the Civil and Commercial Codes, and 17 labour-law articles.
  • Each text says how it was checked: read against the Official Journal page that published it, proved letter by letter against its official PDF, or transcribed from page images and read twice.
  • No article is marked verified until a lawyer has checked it against its page.
  • Nothing is scraped, searched online or read from PDFs while you work; the analysis starts only when you click.
  • Under each suggested article: the official reference, its place in the code, a link to the official document, and a “not verified by a lawyer” tag.
  • With no recognised claim type there is no candidate and nothing is proposed; a small model does not use this way out, so the lawyer judges the proposed claim type.
  • The app never downloads anything: importing a text is a maintainer’s task at development time, and the shipped app reads its texts offline.

What this means for a lawyer Every article the app proposes is quoted from an official text, with its reference and source link, but none is yet marked as checked by a lawyer.

Read the full document

The grounds suggestions rest on a local copy of the official Arabic texts. The app quotes an article only from this library and never from a model’s memory: a 4-billion-parameter model cannot recall article numbers or wording reliably.

Loaded today, 1,361 articles in three kinds:

Codes Articles How the text was taken Proof
Family Code (84-11 as amended by 05-02), complete; CPCA (08-09 as amended by 22-13), complete 227 + 1,084 Imported from the law corpus (branch codex/law-corpus, docs/LAW-CORPUS.md) by tools/law/import_corpus.py: for each article, the version in force today Every imported version is in the corpus state checked: read against the Journal Officiel page that published it, with the amendments applied. The source spelling is kept as printed. Repealed articles are left out. The corpus commit is recorded in each code’s notes
Civil Code, Commercial Code 33 Typed from official PDFs that have a text layer (data/law/curated-articles.json, tools/law/build_codes.py) Automatic: each text’s letters and digits are found, in order, in its PDF (sourceExtract)
Labour relations (90-11, 91-29, 96-21), labour disputes (90-04) 17 Transcribed from Journal Officiel page images (data/law/transcribed-articles.json, tools/law/build_transcribed.py) Read twice: transcribed (16 October 2026), then read again word by word against the same page images

To refresh the Family Code or the CPCA from a newer corpus commit, export its data/law/corpus folder and run:

python3 tools/law/import_corpus.py <corpus folder> <corpus commit> family-code cpca

The Civil Code will come the same way once the law agent delivers its checked package.

Each code file says which of the three it is in check (officialJournalPage, officialPdfText or pageImagesReadTwice; LawCode.Check). The import tools write it, and the loader refuses a code without it, or one whose check disagrees with its transcription note. The law library page shows it to the lawyer.

Every article records the file and page it is printed on (an article amended by a later law names that law’s Journal Officiel), and none is marked verified until a lawyer has checked it against its page.

How the grounds analysis works

Nothing is scraped, searched online or read from PDFs while the lawyer works. Everything runs on the lawyer’s machine. It starts only when the lawyer clicks «حلّل الوقائع واقترح الأسانيد» in the Legal grounds step.

Prepared in advance:

  • The official texts are imported once, at development time, from the official PDFs into data/law/codes/*.json, and shipped with the app. A script checks each text against its PDF page; a lawyer’s review is still to come (none is marked verified).
  • The claim catalog (data/law/claims/*.json) is curated, lawyer-verifiable knowledge. For each claim type it records its usual articles, requests, documents and prerequisites.

At the lawyer’s click (GroundsAnalyzer):

  1. Reading the facts (model call 1, prompts/read-facts.ar.txt). The model writes five labelled lines:

    • the legal relationship;
    • the obligation breached;
    • what is sought;
    • one claim type (a second only for an independent demand), as an id from the catalog list for the court section’s area, each listed with a one-line description; the list ends with other, the named way out when nothing applies;
    • 5–12 search terms in the codes’ vocabulary.

    The parser accepts ids or Arabic names and formatting variants, keeps catalog claim types only (other means none), and removes any article number.

  2. Gathering the candidates (no model). The candidates are the catalog articles of the recognised claim types only, at most 12. With no recognised claim type there is no candidate and the analysis proposes nothing (it abstains). ArticleIndex, a BM25 full-text index over every in-force imported article, now serves only the lawyer’s manual search: mixed into the candidates it added noise and no useful article (October 2026 audit).

  3. Choosing articles (model call 2, prompts/select-articles.ar.txt). The model sees the candidates as key — citation — first 180 characters of the official text and answers key | role | reason. The parser:

    • keeps candidate keys only, at most 6;
    • maps the roles (أساس/إثبات/تعويض/إجراء);
    • truncates a reason to 30 words;
    • drops a reason that names another article.

The result is the model’s reading plus the proposed articles, each with its role, reason, official text and provenance. It is saved with the draft. Nothing is ticked until the lawyer confirms the claim type; confirming ticks the catalog’s required articles for it, and everything else waits for the lawyer’s tick.

Why there are no hand-written keywords: there is no official mapping from facts to articles, so a keyword list can never be complete. Here the model translates the facts into legal vocabulary, and the full-text index covers every imported article without curation.

Before any code is imported, stage 2 can only offer the catalog articles of the recognised claim types, described by their catalog reason instead of their text.

Measuring it: run this on the lawyer’s kind of machine:

dotnet run --project src/DzLegal.Harness -- grounds --models gemma3:4b --out bench-grounds

It runs the fact patterns in tests/grounds-cases/ and writes bench-grounds/summary.md, plus one file per case with the model’s raw answers at each stage. It reports:

  • claim exact: the expected claim is ranked first, or nothing is proposed when nothing applies;
  • claim found: the expected claim is among those proposed;
  • core recall: the expected articles proposed;
  • off-claim share: proposed articles outside the expected claim’s catalog list, i.e. noise;
  • articles per case, empty answers, and seconds per case.

This is reported, not gated, because the quality depends on the model and on the imported texts.

First measurement (gemma3:4b, 8 GB laptop, no official text imported, catalog-only, cases written and tuned by us):

Measure Result
Claim exact 64%
Claim found 75%
Core recall 73%
Time per case 91 s

Three weaknesses showed up:

  • Empty answers: five cases came back empty, probably from the answer format.
  • Over-inclusion: the model nearly always added a second, generic claim type and accepted 6–8 articles.
  • Confusions between neighbouring claim types: liability for things vs tort, taṭlīq vs divorce, commercial lease vs commercial debt.

What we changed in response:

  • The parser now accepts the claim type’s Arabic name and formatting variants.
  • Each claim type has a one-line description that tells it apart from its neighbours.
  • The prompts ask for one claim type and 2–5 articles.
  • The bench now saves the raw answers and measures the noise.

Second measurement, after those changes:

Measure Run 1 Run 2 Run 3
Claim exact 64% 80% 84%
Claim found 75% 80% 84%
Core recall 73% 61% 67%
Off-claim share not measured 13% 10%
Articles per case about 7 2.4 2.5
Empty answers 5 0 0
Seconds per case 91 84 82

The lists are now short and focused. Core recall fell mostly because fewer articles are proposed, and because our “core” articles are a guess. For example, the model picks 107 rather than 106 for a loan, which is defensible.

Remaining errors:

  • liability for things still read as tort (3 of 3);
  • 3 of the 4 cases outside the catalog were still given a claim type;
  • one taṭlīq read as divorce by the husband.

Two last generic changes followed:

  • a named way out (other) at the end of the claim list;
  • descriptions that say what each claim type is not.

Run 3 came after those changes. Liability for things improved (0/3 → 2/3), and the commercial supplies case became right. The model never chose other, so all four out-of-catalog cases still got the closest claim type: inheritance was read as divorce, the administrative case as rescission, company dissolution as commercial debt, and civil status as a sale. Three single confusions remain: a dog bite read as tort, one taṭlīq read as divorce by the husband, and one loan read as a sale.

Conclusion and decision:

  • A 4B model does not use an abstain option, so the lawyer judges the proposed claim type. This is acceptable because the model’s reading and claim type are shown, and nothing is used unless the lawyer ticks it.
  • other stays: it costs nothing and may work with other models.
  • Tuning on our own 44 cases stops here, to avoid overfitting. The next measurements are:
    • the legal reviewer’s hold-out cases, with her own core articles;
    • a run after the official texts are imported;
    • optionally, a larger local model if the hardware allows.

Why there is no live scraping or web search:

  • The case facts would leave the machine.
  • The sources are mostly scanned Journal Officiel PDFs, which a small local model cannot read reliably.
  • It would take minutes per case.
  • Nobody could verify or reproduce a suggestion afterwards.

Provenance shown under each suggested article:

  • the official reference of the text, plus the Journal Officiel issue once a lawyer fills joReference in sources.json;
  • the article’s place in the code (book › title › chapter) once imported;
  • a link the lawyer can click: the exact official document the text was imported from, or the official site (officialSite) before import;
  • a tag: “Catalog proposal — not verified by a lawyer” until both the claim and the imported code are marked verified.

Rules

  1. Article text only comes from an official publication. Every code file records the URL it was imported from, the SHA-256 of the downloaded file, and the date. Nobody types article text by hand, and no model writes it.
  2. Every import is reviewed. The importer writes a report listing gaps in the numbering, duplicates, and articles that are suspiciously short or long. A lawyer compares a sample of articles with the Journal Officiel before the code is marked "verified": true.
  3. Amendments show up as a diff. Re-importing a newer consolidated text reports added, removed, changed and newly repealed articles.
  4. The app never downloads anything. Importing is a maintainer task done at development time. The shipped app reads data/law/codes/*.json offline.

Files

Path What it is
data/law/sources.json The texts to import: id, titles, the citation name used in requêtes, and the official reference (law number and date).
data/law/codes/<id>.json One imported code: metadata, source, and its articles (number, book/title/chapter path, text, repealed flag).
data/law/reports/<id>.md The review report from the last import.

Article numbers are canonical, for example 106, 124 مكرر and 124 مكرر 1. «المادة الأولى» is stored as 1.

Importing a text

You need the .NET SDK, plus pdftotext (poppler-utils) for PDF sources. Run from the repository root:

# from the official URL
dotnet run --project src/DzLegal.Harness -- law import civil-code --url "<official PDF or HTML URL>" --as-of 2026-01-01
# or from a file downloaded by hand
dotnet run --project src/DzLegal.Harness -- law import civil-code --file ~/Downloads/code-civil.pdf --as-of 2026-01-01

--as-of records the date up to which the consolidated text includes amendments.

What the importer does:

  • It extracts the text: PDF via pdftotext, HTML with its tags removed, or plain UTF-8 text.
  • It turns PDF presentation forms into ordinary letters.
  • It detects text extracted in reversed visual order and flips it.
  • It splits the text on «المادة N» headings and keeps each article’s text exactly as published.
  • It re-joins lines that the PDF layout wrapped.

The code file is written only when it validates: canonical and unique numbers, and no empty text unless the article is marked repealed.

Sources: use the official publications, which are the Journal Officiel (joradp.dz) and the consolidated codes published by the Ministry of Justice (mjustice.dz). Algerian copyright law excludes official legal texts from protection. That should be confirmed as part of the legal review.

Verifying an import (for the lawyer)

  1. Read data/law/reports/<id>.md. Every gap must be explained, usually by a repealed article or an extraction problem. Explanations go in the code’s notes.
  2. Compare at least 20 articles chosen across the whole code, plus every article the claim catalog cites, with the official text.
  3. If everything matches, set "verified": true in data/law/codes/<id>.json and commit.

Claim catalog and suggestions

data/law/claims/<area>.json (areas: civil, commercial, family, social) lists claim types the way a lawyer thinks of them (with no keywords: the model recognises them from their label and a one-line description of when the claim applies and how it differs from its neighbours), for example «استرداد مبلغ قرض», «الخلع» or «التسريح التعسفي». Each claim type has:

  • articles: code, number, role (basis, proof, damages, procedure) and a short “why”.
  • prerequisites: for example, prior conciliation for labour claims.
  • suggestedRequests, requiredDocuments and factsToState, offered to the lawyer as one-click inserts and a checklist.

Loader checks

  • Every claim cites a code listed in sources.json.
  • Once a code is imported, every cited article must exist and be in force. A wrong or repealed number fails at load time and in CI.
  • Until the code is imported, the citation is shown with “text not yet available”.

Status

1,361 articles are loaded, as the table at the top of this page shows:

  • Family Code (84-11 as amended by 05-02) and CPCA (08-09 as amended by 22-13): complete, 227 + 1,084 articles, imported from the law corpus.
  • Civil Code and Commercial Code: 33 articles, those the claim types rely on, typed from the SGG 2007 consolidated edition.
  • Labour relations and labour disputes: 17 articles, transcribed from Journal Officiel page images.

How the typed articles (Civil and Commercial codes) were loaded:

  • Typed. data/law/curated-articles.json holds the clean text of each article. Only spacing, punctuation and the letter ك (which these PDFs encode as آ) were corrected.
  • Proved. python3 tools/law/build_codes.py checks every text against its PDF: the same letters and digits in the same order, from the same page.
  • Written. The builder writes data/law/codes/*.json with the page, the edition and the source extract. The loader rejects any later edit that changes a letter.
  • Verified by a lawyer. Each article is verified: false until a lawyer compares it with the PDF page. The review sheet records her verdicts, which then set verified, verifiedBy and verifiedOn.

The codes are partial (partial: true). Every article the claim catalog cites in those codes must be loaded, and the loader enforces this. The full-text importer (law import) remains for loading whole codes later.