Metadata-Version: 2.5
Name: folio-resolve
Version: 0.5.0
Summary: Shared FOLIO source-text-to-concept resolution engine: scorer, span decomposition, gates, alias blocklist, domain-prior judge, and annotate primitives.
Project-URL: Homepage, https://github.com/damienriehl/folio-resolve
Project-URL: Repository, https://github.com/damienriehl/folio-resolve
Project-URL: Issues, https://github.com/damienriehl/folio-resolve/issues
Author: Damien Riehl
License: MIT
License-File: LICENSE
Keywords: entity-matching,entity-resolution,folio,legal,nlp,ontology,taxonomy
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Legal Industry
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: pydantic<3.0,>=2.7
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pyarrow<27,>=21; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: embedding
Requires-Dist: faiss-cpu<2.0,>=1.8; extra == 'embedding'
Requires-Dist: numpy<3.0,>=1.26; extra == 'embedding'
Requires-Dist: sentence-transformers<4.0,>=3.0; extra == 'embedding'
Provides-Extra: folio
Requires-Dist: folio-python[search]>=0.1.5; extra == 'folio'
Provides-Extra: opengloss
Requires-Dist: pyarrow<27,>=21; extra == 'opengloss'
Provides-Extra: spacy
Requires-Dist: spacy<4.0,>=3.7; extra == 'spacy'
Description-Content-Type: text/markdown

# folio-resolve

**The shared FOLIO source-text→concept matching engine.** One pinned, MIT-licensed Python library
that maps arbitrary source text (book headings, deposition transcripts, intake narratives, task
titles) to concepts in [FOLIO](https://folio.openlegalstandard.org) — the open legal ontology of
18,000+ concepts.

> This is a **lift-and-improve** extraction. The matching intelligence already existed across
> `folio-mapper`, `folio-enrich`, and `folio-insights` — but it had **diverged** (enrich literally
> forked mapper's scorer) and was informally shared through a fragile `sys.path` hack. `folio-resolve`
> is the single source of truth those repos now pin, plus the capabilities the recorded failures demanded.

---

## Why this exists

Three repos independently built the same engine and drifted apart:

- **folio-mapper** wrote the canonical, word-order-invariant scorer + a 4-stage pipeline + a FAISS embedding index.
- **folio-enrich** *copied* mapper's scorer (its `search.py` says "ported from folio-mapper" in the code) and added an Aho-Corasick entity ruler, a reconciler, a domain-prior judge, and a feature-rich annotate/feedback UI.
- **folio-insights** `sys.path`-imports **both** siblings at runtime — documented as fragile.

A book-annotation review (Ch02) then surfaced failures that no amount of local patching would fix:
place-names over-scoring (Slovenia → 99 units), homonyms (Action ≠ Auction), conjoined compound
headings that match nothing, metadata tagged as substance, and pure-semantic maps with no shared
label token (Presumptions → Litigation Burdens of Proof). Those became this library's new capabilities.

## What it does

| Capability | Module | Solves |
|---|---|---|
| Word-order-invariant relevance scoring | `scoring` | "arbitration rules" and "rules of arbitration" both reach *Arbitration Rules* |
| Multi-strategy search-term generation + legal expansions | `scoring` | recall on paraphrases (a helper the consumer drives — see the note below) |
| Pure-Python Aho-Corasick entity ruler | `entity_ruler`, `matching` | trusted exact-label spans, no spaCy/C deps |
| Candidate reconciliation (ruler + LLM + semantic) | `reconciler` | one clean candidate set with provenance |
| **Span decomposition** (conjunction split + shared head/tail) | `decompose` | "**Proposed** Findings of Fact **and** Conclusions of Law" → both siblings |
| **Place-name / short-label gates** | `gates` | kills Slovenia→99 and Presumptions→Northern Mariana Islands@90 |
| **Alias/homonym blocklist** | `blocklist` | deterministic Action ≠ Auction guard |
| **Metadata/front-matter exclusion** | `sources` | never tag the copyright page |
| **Multi-tag domain prior** (auto-suggest + validate/add) | `domain_prior` | Defenses → *Litigation* Defenses; corpus carries many subjects |
| **Shared label→IRI resolution** (decompose-first, calibrated 0–100 bar, branch-carrying) | `resolve` | every consumer resolves identically; `law`→*Delaware*@90 no longer accepted |
| **Score calibration** (weak-band recalibration) | `calibration` | verdict-labeled score→P(correct) fit |
| **Lemma-key index augmentation** (build-time spaCy, cached JSON) | `lemma` | singular *agreement* reaches plural-labelled *Agreements* |
| LLM judge interface + domain-prior prompts | `judge` | context-aware disambiguation, verdict enforcement |
| **Annotate primitives** (confidence, per-tag verdicts, notes, reject/restore, insights) | `annotate` | the self-improving feedback loop, as a library |
| 4-stage pipeline: filter → expand → rank → judge | `pipeline` | the build-once-use-many entry point |

### Reading that table precisely

Four rows mean slightly less than a quick read suggests. Consumers pin this library, so the
exact shape matters more than the headline:

- **"Word-order invariant" is a property of the *overlap*, not of the final number.**
  `content_words` reduces both phrasings to the same set, so both reach the concept — but the
  scorer also has exact-string and substring fast paths that word order does reach.
  `"arbitration rules"` scores **99.0** against *Arbitration Rules* (exact string), while
  `"rules of arbitration"` scores **88.0** (pure overlap). Same concept, same rank in practice;
  not the same score. Consumers that threshold on an absolute value should know which path fired.
- **The folio-python provider is a recall source, not a scoring authority.** Every label-search
  candidate it returns is re-scored by this library's own scorer, so consumers see the same
  0–100 relevance scale whether they use `FolioPythonProvider` or `InMemoryOntology`. This can
  change scores, ranks, result membership, and thresholded `MatchPipeline` output. When migrating,
  revalidate `MatchPipeline.score_floor`, score calibration, and committed ranking snapshots.
- **`full_text` now affects non-judge tie ordering, not primary eligibility.** It supplies the
  `rank_tiebreak_score` used for deterministic `(-score, -rank_tiebreak_score, iri)` ordering,
  even when `run_judge=False`. It can reorder equal-primary candidates, including membership at
  an equal-score top-k boundary, but score floors, calibration, thresholds, and precedence across
  unequal primary scores still use `score` alone.
- **`generate_search_terms` is a helper you call, not a pipeline stage.** It produces the
  sub-phrases, content words, and `LEGAL_TERM_EXPANSIONS` suffixes ("litigation" → "litigation
  practice", "litigation service"), and it is exported for exactly that use — but
  `MatchPipeline` does **not** call it. The pipeline's `filter` stage runs one
  `search_by_label(surface_term)`; its `expand` stage runs `decompose` plus the optional semantic
  index. Drive the multi-strategy search yourself if you want that recall:
  `[pipe.match(t) for t in generate_search_terms(heading)]`.
- **The shared-*tail* rule is a heuristic, and it over-fires on prepositional tails.** It is
  right when the tail is a genuine elided head noun — `"Antitrust and Securities Law"` →
  `["Antitrust Law", "Securities Law"]` — and wrong when the tail word is the object of a
  preposition: `"Findings of Fact and Conclusions of Law"` emits `"Findings of Fact Law"`
  alongside the correct `"Conclusions of Law"`. That extra string is harmless noise, not a wrong
  tag: anything it reaches is a Findings-of-Fact concept the first sibling already produced, and
  against most ontologies it reaches nothing. A leading shared head suppresses the tail rule
  entirely — which is why the **Proposed** variant in the table decomposes cleanly.

### Determinism is a guarantee, not a coincidence

Identical input produces byte-identical output in any process, and several consumers commit
golden/snapshot baselines that depend on it. That is enforced, not assumed: `tests/test_determinism.py`
re-runs the public entry points in subprocesses under different `PYTHONHASHSEED` values and
compares them byte-for-byte. In-process assertions cannot catch this class — within one process a
set's iteration order is fixed, so the bug is invisible. Anything that reaches a caller-visible
result is therefore totally ordered: ties break on a stable field (IRI, label, filename), never on
a hash.

## Personas

- **Book-extraction pipelines** (`folio-insights`, `books`) — offline, CLI-first tagging of treatise text.
- **Intake / matter classifiers** (`alea-intake`, `mootloop`) — narrative → FOLIO concepts for routing.
- **Interactive mappers** (`folio-mapper`, `clio-skills`) — a UI over the library's ranked candidates.
- **The FOLIO ontology team** — a reference implementation of "good" source-text→concept matching.

## Use cases

- Tag a chapter of a litigation treatise with FOLIO concepts, with a litigation domain prior so
  "Defenses" resolves to *Litigation Defenses*, not the generic sense.
- Resolve a compound heading like *"Proposed Findings of Fact and Conclusions of Law"* to the two
  sibling concepts it actually names.
- Suggest the subject tags for a corpus ("Personal Injury Depositions" → *Personal Injury* +
  *Deposition*) and let a human validate/add via a FOLIO taxonomy-tree picker.
- Feed a reviewer's `wrong` verdict on a homonym straight into the alias blocklist so the mistake
  never recurs — the self-improving loop.

## Install

```bash
uv add folio-resolve                 # core (pure-Python, only pydantic)
uv add "folio-resolve[folio]"        # + folio-python live ontology adapter
uv add "folio-resolve[embedding]"    # + sentence-transformers / faiss for the semantic path
uv add "folio-resolve[spacy]"        # + lemma-key index augmentation (build-time only)
```

The **core is pure-Python** — the scorer, decomposition, gates, blocklist, domain-prior, reconciler,
calibration, and annotate primitives depend on nothing heavier than `pydantic`. FAISS,
sentence-transformers, spaCy, and folio-python live behind `Protocol` seams with working pure-Python
defaults, so the whole test suite runs with no model downloads and no network.

### Lemma-key augmentation (`[spacy]` extra, v0.2.0)

The 2026-07 ruler shootout ([bench/RESULTS.md](bench/RESULTS.md)) showed the recall edge folio-enrich's
spaCy ruler appeared to have lives in its **label index**, not its engine: lemma keys let the singular
surface *agreement* reach the plural-labelled concept *Agreements* (+200/200 lemma-gold hits, +698
corpus matches). v0.2.0 promotes that indexing here, engine-agnostic:

```python
from folio_resolve import Concept, FOLIOEntityRuler, InMemoryOntology, augment_labels

provider = InMemoryOntology([Concept(iri="R-agreements", label="Agreements")])
labels = provider.all_labels()                      # any OntologyProvider
labels = augment_labels(                            # adds lemma_preferred / lemma_alternative keys
    labels,
    lemma_map={"agreements": "agreement"},          # precomputed map; works with the core install
)
ruler = FOLIOEntityRuler()
ruler.load_patterns(labels)                         # pure-Python matching, zero heavy deps
assert ruler.find_matches("The agreement was signed.")[0].entity_id == "R-agreements"
```

This core-only example supplies a small precomputed lemma map. To compute one with spaCy,
omit `lemma_map`; pass `cache_dir` and an `ontology_hash` derived from your ontology content
to cache it by that hash plus `LEMMA_VERSION`. Set `on_missing_spacy="skip"` to keep the
original labels when spaCy or its model is unavailable.

**spaCy is needed only at index-build time** (computing what each label's lemma is; requires the
`[spacy]` extra plus `python -m spacy download en_core_web_sm`). Steady-state consumers load the
cached JSON lemma map and never import spaCy. Without the extra: the default raises a clear
`SpacyNotInstalledError`; `on_missing_spacy="skip"` degrades to the un-augmented index. Use it
whenever documents refer to concepts in the singular while the ontology labels them in the plural
(FOLIO does, pervasively); skip it for exact-vocabulary corpora. Legal pluralia tantum (*damages*,
*proceedings*, *wills*, …) are denylisted per-ontology via `OntologySpec.behavior.lemma_denylist`.

### Judge transport hardening (v0.2.1)

`parse_judge_json` now survives everything models actually emit — ```` ```json ```` fences (a new
exported `strip_markdown_fences` does the stripping), a non-list `judged`, non-dict rows, a
non-object payload, `None` input, non-string `reasoning`, and an `adjusted_score` that is
non-numeric (`"high"` → the row is dropped, not an exception) or out of range (clamped to 0-100
*before* verdict enforcement, so a `"penalized"` verdict cannot escape the scale). Surfaced by the
folio-mapper migration: mapper is the donor of these verdict rules and had kept a local parse loop
precisely because the library's was weaker than the code it was lifted from.

### Consumer-driven API additions (v0.3.0)

Three gaps the **alea-intake** migration recorded (SCHEDULE.md row 4) are closed. All are
**additive** — default behavior is bit-identical, pinned by a golden no-drift table in
`tests/test_scoring.py` so folio-mapper's and folio-enrich's committed captures stay valid.

- **`compute_relevance_score` is type-defensive.** Every text argument is coerced: a `None`
  `preferred_label` (folio-python returns one for concepts with no preferred label), a test
  double, a number, a non-`str` synonym entry — all read as *absent* instead of raising
  `TypeError` out of `re.findall`. A non-`str` `label` scores `0.0`. Consumers can drop their
  boundary coercion shims. Same spirit as the v0.2.1 `parse_judge_json` hardening.
- **`PlaceNameGate` takes a consumer vocabulary.** `extra_tokens` (place names, matched against
  label tokens *and* the whole label so multi-word names work), `extra_markers` (substring
  phrasings — `("city of", "republic of")` catches *City of Exampleton* whatever it's called),
  and `extra_branch_markers` (extra governed branches). A `place_tokens` property exposes the
  merged vocabulary. This retires the parallel local backstops consumers had to carry:

  ```python
  from folio_resolve import Concept, InMemoryOntology, PlaceNameGate

  provider = InMemoryOntology([
      Concept(iri="R-rize", label="Rize", branch="Location"),
  ])
  gate = PlaceNameGate(
      min_signals=2,
      extra_tokens={"macedonia", "rize", "europe", "north america"},
      extra_markers=("city of", "republic of", "province of"),
  )
  concept = provider.get_concept("R-rize")
  assert concept is not None
  decision = gate.evaluate(
      query="law", label=concept.label, branch=concept.branch, score=90.0,
  )
  assert decision.demoted
  ```

- **The specificity penalty is weightable.** `compute_relevance_score(..., specificity_penalty=w)`
  scales the "candidate is more specific than the query" haircut: `1.0` is the historical
  default, `0.0` disables it. Consumers whose queries are *shorter than their targets by
  construction* (a 1-2 word claim name against a 3-5 word FOLIO label — *Habitability* →
  *Breach of Warranty of Habitability*, scored 67.5 at full penalty) can damp it rather than
  fork the scorer. Consumers that map taxonomy nodes, where an over-specific target IS an
  error, leave it at `1.0`.

Adopting `PlaceNameGate` is a **per-path** decision, not a per-repo one — see
[docs/migration/SCHEDULE.md](docs/migration/SCHEDULE.md#adopting-placenamegate-is-a-per-path-decision).

## Bring Your Own Key (BYOK)

`folio-resolve` is **key-agnostic** — it never reads an env var, instantiates a provider SDK, or makes
a network call on its own. The **zero-key deterministic core** (ruler, scoring, decomposition, gates,
blocklist, metadata exclusion, calibration, annotate) runs fully offline and free. Three optional stages
accept a provider through a `typing.Protocol` seam you fill with an object you construct:

| Stage | Protocol | Buys you | Absent → |
|---|---|---|---|
| **Judge** | `Judge.complete(system, user) -> str` | context-aware disambiguation + verdict enforcement | items pass through **unjudged** |
| **Embeddings** | `EmbeddingProvider` (`embed`/`embed_batch`/`dimension`) | semantic recall for no-shared-token maps | local `all-MiniLM-L6-v2` default (no key), or skipped |
| **Domain-prior suggestions** | `DomainPriorSuggester(ontology)` | auto-suggest corpus subject tags | supply tags manually |

You own the key, the vendor (OpenAI / Gemini / Anthropic / local), and the spend. The library ships the
judge **prompt builders** and **deterministic verdict enforcement**; you supply only the raw model call.
Graceful degradation is the default — no key means deterministic-only output, never a crash;
mark the pass-through candidates `unjudged` yourself so reviewers can tell "no model ran" from
"the model declined" (the BYOK guide shows the one-line `extraction_path` tag). Reference cost: **≈ $0.12 / chapter on `gemini-2.5-flash-lite`** (~1,875
calls, ~652K tokens, 464 units). Full guide, env-var conventions, and a minimal wiring example per
vendor: **[docs/BYOK.md](docs/BYOK.md)**.

## Quick start

```python
from folio_resolve import InMemoryOntology, Concept, MatchPipeline, DomainPrior

ontology = InMemoryOntology([
    Concept(iri="R-defenses", label="Litigation Defenses", branch="Objectives"),
    Concept(iri="R-arb", label="Arbitration Rules", branch="Service"),
])
pipe = MatchPipeline(ontology=ontology)

# word-order-invariant
pipe.match("rules of arbitration")        # -> Arbitration Rules

# domain prior threads a subject into the (optional) judge
prior = DomainPrior.from_manifest_subjects("treatise", [("R-lit", "Litigation")])
pipe.match("Defenses", domain_prior=prior)
```

## Span tagging (OpenGloss + FOLIO)

The opt-in `folio_resolve.spans` API tags every matching phrase and word in two
separate layers. Phrases rank `primary`; contained word anchors remain as
`fallback` and name the span that outranks them. It returns all matching senses,
without choosing a meaning. Existing matching APIs and outputs remain unchanged.
Loading and tagging need only the core install; install the build extra only
when compiling parquet:

```bash
pip install "folio-resolve[opengloss]"
python -m folio_resolve.spans.lexicon build \
  "$OPENGLOSS_PARQUET_DIR"/train-00000.parquet \
  "$OPENGLOSS_PARQUET_DIR"/train-00001.parquet \
  "$OPENGLOSS_PARQUET_DIR"/train-00002.parquet \
  --artifact lexicon.jsonl.gz --manifest lexicon.manifest.json
python -m folio_resolve.spans.lexicon verify lexicon.jsonl.gz lexicon.manifest.json
```

Set `OPENGLOSS_PARQUET_DIR` to the acquired parquet directory from
[`mjbommar/opengloss-v2.4-senses`](https://huggingface.co/datasets/mjbommar/opengloss-v2.4-senses),
revision `c64e480015b8c84f9d08469d4dfd64a7a8fb89bd`. The builder verifies all
three source hashes before reading rows. Keep the artifact and manifest together;
the manifest records source hashes, row count, and uncompressed content SHA-256
for reproducible rebuild comparisons.

```python
from folio_resolve import Concept, InMemoryOntology
from folio_resolve.spans import SpanTagger, load_lexicon

lexicon = load_lexicon("lexicon.jsonl.gz", "lexicon.manifest.json")
ontology = InMemoryOntology([
    Concept(iri="https://example.org/concepts/tenant", label="Tenant"),
])  # Supply concepts from your pinned FOLIO release in production.
tagger = SpanTagger(ontology, lexicon, folio_release="your-FOLIO-release-or-OWL-sha256")
result = tagger.tag("The tenant acted in good faith.")
print(result.to_json())
```

Either layer can run alone. For typed links between the layers, pass
`crosswalk=load_crosswalk("folio-opengloss.sssom.tsv")` using
`load_crosswalk` from `folio_resolve.spans`. Links retain the predicate,
confidence, justification, and mapping set identity; anchors are match evidence,
not factual resolutions. Character offsets refer to the original input. The
optional `regions=[(start, end)]` restricts tagging to selected text ranges.

Result provenance names the OpenGloss dataset and revision, lexicon content
digest, caller-supplied FOLIO release and actual label-index digest, label source
(`all_concepts` or the provider's `all_labels` fallback), crosswalk set id/version
when supplied, normalization scheme, and library/tagger versions. Each anchor
records its match method (`exact` or OpenGloss noun `plural`) and confidence;
OpenGloss anchors also retain `source` and `tier`. `to_json()` sorts keys and
adds no clock time; `tagged_on` appears only when supplied by the caller.

OpenGloss v2.4 senses by Michael J. Bommarito II are used under
[CC BY 4.0](https://creativecommons.org/licenses/by/4.0/). Compilation projects,
sorts, and compresses the dataset; matching normalizes headwords. Some senses
derive from Princeton WordNet 3.0 and retain `source: wordnet-3.0`; WordNet's
separate license and attribution also apply. See [THIRD-PARTY.md](THIRD-PARTY.md)
and the [real-data receipt](docs/benchmarks/span-tagging-real-data.md).

## Development

```bash
uv sync --frozen --extra dev
uv run --no-sync pytest -ra --junitxml=test-results/core.xml
uv run --no-sync mypy   # strict library scope from pyproject.toml
uv run --no-sync ruff check .
```

Pull requests run these checks on Python 3.11 and 3.12 (the type check runs on
3.11). Core CI installs only the development extra; it needs no private gold data,
provider credentials, or model downloads. Pytest's `-ra` output explains skips:
the private audit packet is absent from a clean checkout, the spreadsheet test
requires optional `openpyxl`, and real-ontology UAT requires the `folio` extra and
`FOLIO_RESOLVE_UAT_REAL_ONTOLOGY=1`. The skip count depends on the local environment.
JUnit reports remain available as workflow artifacts after test failures.

CI also builds a wheel and installs it into a separate virtual environment outside
the checkout. Its isolated Python process imports the installed package and checks
that the bundled alias-blocklist JSON is present and loads through the public
loader. This catches packaging omissions that editable-install tests can miss.
The [core verification workflow](.github/workflows/ci.yml) contains the exact smoke
commands and supports manual dispatch; release publishing remains separate.

### Optional real-model tests

The default pytest command excludes `embedding_integration` tests and never loads
sentence-transformers. The manual [embedding workflow](.github/workflows/embedding.yml)
installs a separate Python 3.12 CPU environment, downloads the immutable revision
in [the model pin](tests/embedding_model.json), then tests offline. Its artifacts
record the model revision, installed package versions, commit, and JUnit results.
This is an adapter and pipeline smoke test, not a retrieval-quality benchmark.

For a local run, use the workflow's separate environment and acquisition commands,
then point to the downloaded snapshot directory (whose basename is the pinned
revision):

```bash
HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \
FOLIO_RESOLVE_EMBEDDING_MODEL_PATH=/path/to/snapshots/1110a243fdf4706b3f48f1d95db1a4f5529b4d41 \
/path/to/embedding-venv/bin/python -m pytest tests/test_embedding_integration.py \
  -m embedding_integration -ra
```

The explicit `-m` overrides the default marker exclusion. A missing snapshot,
missing optional dependency, or disabled offline mode fails this explicit run;
none is silently skipped. Model acquisition remains a separate network step.

## License & attribution

MIT — see [LICENSE](LICENSE). Every extracted component and dependency is logged in
[THIRD-PARTY.md](THIRD-PARTY.md). The migration schedule for consumer repos is in
[docs/migration/SCHEDULE.md](docs/migration/SCHEDULE.md).
