Lexicon — UK Legal Compliance Checker
Lexicon is a fine-tuned Legal-BERT model that reads contract clauses and detects potential violations of UK law. It is designed to run entirely on local hardware — no data leaves your machine — making it suitable for law firms handling confidential documents.
Lexicon is not a replacement for a solicitor. It is a first-pass screening tool that flags clauses worth a closer look, citing the specific UK legislation that may apply.
What problem does it solve?
Reviewing contracts for legal compliance is time-consuming, especially for small legal teams. A standard commercial agreement can contain dozens of clauses, and problematic terms are often buried inside legitimate-sounding language. Lexicon automates the initial scan, surfacing issues a junior associate or non-specialist might miss.
How it works
Lexicon runs a four-step pipeline on any contract you provide:
Step 1 — Clause splitting
The contract (PDF or plain text) is split into individual clauses using regex pattern matching. The splitter detects numbered sections (1., (a)), bullet points, and paragraph boundaries, filtering out headers and fragments shorter than 8 words so only substantive clauses reach the classifier.
Step 2 — Tokenisation
Each clause is tokenised using Legal-BERT's wordpiece tokeniser — a vocabulary trained on millions of legal documents. Terms like "indemnification", "tortfeasor", and "subrogation" are represented as single tokens rather than broken into fragments. Every clause is padded or truncated to 512 tokens. An attention mask tells the model which tokens are real content and which are padding.
Step 3 — Classification
The tokenised clause passes through nlpaueb/legal-bert-base-uncased, a 110-million parameter transformer pretrained on case law, contracts, and legislation. Self-attention allows the model to read every token in relation to every other token simultaneously — this is what allows it to understand that "limitation" means something different in "limitation of liability" versus "limitation period". The [CLS] token's 768-dimensional output vector summarises the entire clause, passes through dropout (0.3) and a linear head, producing 11 scores — one per violation category. Softmax converts these to probabilities and the highest is the confidence score.
Step 4 — Confidence filtering and explanation
Predictions below 0.75 confidence are suppressed. Violations above 0.80 confidence are passed to a locally-running Qwen2.5:7b instance (via Ollama) which generates a plain English explanation of why the clause is problematic and what a compliant version should say. The final output is a structured compliance report saved to disk, with issue numbers, severity ratings, law citations, explanations, and a human review flag for borderline predictions.
Violation categories
| Category | Key legislation |
|---|
| unfair_contract_terms | Unfair Contract Terms Act 1977 |
| limitation_of_liability | UCTA 1977, s.2(1) |
| limitation_period | Limitation Act 1980 |
| data_protection | UK GDPR, Data Protection Act 2018 |
| employment_rights | Employment Rights Act 1996 |
| consumer_rights | Consumer Rights Act 2015 |
| negligence | UCTA 1977, Occupiers Liability Act 1957 |
| misrepresentation | Misrepresentation Act 1967 |
| restraint_of_trade | Common law (Nordenfelt v Maxim Nordenfelt) |
| intellectual_property | Patents Act 1977, CDPA 1988 |
| no_violation | — |
Performance
- 97.1% accuracy on 34 completely unseen independent test clauses
- Caught 9 violations (including 7 hidden ones) in a £2.4M AI services contract with zero false positives
- Catches obvious and moderately hidden violations reliably at high confidence
- Flags ambiguous predictions for manual lawyer review rather than guessing
Model details
- Base model:
nlpaueb/legal-bert-base-uncased
- Fine-tuned on: 1,400 synthetic UK contract clauses across 11 categories
- Classification head:
Linear(768 → 11) with dropout 0.3
- Training: 10 epochs, AdamW lr=2e-5, linear decay, batch size 8
- Hardware: Apple M-series MPS
Quick start
1from transformers import AutoTokenizer, AutoModel
2import torch
3
4tokeniser = AutoTokenizer.from_pretrained("nlpaueb/legal-bert-base-uncased")
5
6clause = "The supplier accepts no liability for any loss whatsoever."
7
8tokens = tokeniser(
9 clause,
10 max_length=512,
11 padding="max_length",
12 truncation=True,
13 return_tensors="pt"
14)
15
16# load classifier weights from this repo and run inference
17# see GitHub repo for full working pipeline including
18# PDF ingestion, clause splitting, explainer, and report generation
Full pipeline
The complete Lexicon system beyond this classifier includes:
- PDF and plain text contract ingestion via pymupdf
- Regex-based clause splitter with header filtering
- This Legal-BERT classifier for violation detection
- Qwen2.5:7b via Ollama for plain English explanation generation
- Structured compliance report with severity ratings and law citations
- Confidence-based human review flagging
Limitations
- Trained on synthetic data — may miss highly novel or sophisticated phrasings
- UK law only — not applicable to other jurisdictions
- Deeply disguised multi-sentence violations may fall below the confidence threshold
- Not a substitute for qualified legal advice under any circumstances
- Best used as a first-pass screening tool before solicitor review
Intended use
Designed for small law firms, legal teams, and businesses doing initial contract review. Lexicon flags clauses that warrant closer attention from a qualified solicitor, reducing the time spent on manual first-pass review.
Licence
Apache 2.0
Citation
1@misc{lexicon2026,
2 author = {4tu1},
3 title = {Lexicon: UK Legal Compliance Checker},
4 year = {2026},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/4tu1/lexicon}
7}