ontocast.tool.chunk.schema_detect¶
Detect which document-type schema a document belongs to.
Section labels are only meaningful relative to a schema: a 10-Q scored against
the academic schema recognises almost nothing and comes back unlabeled. When the
caller supplies neither section_schema_id nor a matching
document_type_hint, the schema has to be inferred from the document itself.
The schema catalog is a partition -- every document belongs to exactly one
cell, with general as the residual. Only schemas carrying a
document_profile are detection candidates; general deliberately has none,
because six of its seven labels are strict subsets of other schemas.
Three tiers, cheapest first, each seeing only what the previous one could not decide:
- Lexical (free, no model). Scores on exclusive evidence: a heading
recognised by exactly one candidate schema counts, a heading recognised by
several counts nothing. A shared heading such as
Referencesgenuinely says nothing about which cell the document is in, so weighting it -- even fractionally -- only adds noise. Measured over the corpus intest/data/schema_corpus.jsonthis classifies all nine cells correctly, with the tightest margin (a published trial protocol, which shares IMRaD headings with academic papers) at 2.0x. - Semantic (needs the chunker's embedding model). Each heading votes for its nearest schema by label-name similarity, damped by how close the runner-up is. Per-heading competition rather than per-schema mean similarity, because mean similarity is biased by how many label names a schema happens to have.
- Content (last resort, heading-poor documents only). Body paragraphs
against
document_profilesentences. Measured accuracy is far below the heading tiers -- chemistry prose reads like a technical specification, andnewsbehaves as a semantic attractor -- so it demands a much larger margin and is off unless explicitly enabled.
Every tier abstains rather than guessing. A wrong schema relabels an entire
document silently, whereas an abstention falls back to the manifest default and
leaves document_type_hint in charge.
SchemaDetection
dataclass
¶
An accepted detection, with the tier that made it and its evidence.
Source code in ontocast/tool/chunk/schema_detect.py
SchemaEvidence
dataclass
¶
Why one candidate schema scored what it did.
Source code in ontocast/tool/chunk/schema_detect.py
TextEmbedder
¶
Bases: Protocol
Embeds short texts, or returns None when no model is available.
Source code in ontocast/tool/chunk/schema_detect.py
candidate_schema_ids()
¶
Schemas eligible to be detected, in manifest order.
A schema is a candidate only if it declares a document_profile. That is
what keeps general -- the partition's residual cell -- from ever being a
positive detection.
Source code in ontocast/tool/chunk/schema_detect.py
detect_document_schema(headings, paragraphs=None, *, embed=None, allow_content_tier=False, min_score=2.0, min_margin=1.8, min_share=0.0, content_min_margin=4.0)
¶
Infer the document-type schema, or None when the evidence is thin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headings
|
list[str]
|
Raw heading lines in document order. |
required |
paragraphs
|
list[str] | None
|
Sampled body paragraphs, for the content tier. |
None
|
embed
|
TextEmbedder | None
|
Embedding callable; |
None
|
allow_content_tier
|
bool
|
Permit the weak content tier on heading-poor documents. |
False
|
min_score
|
float
|
Absolute evidence the winner must clear. |
2.0
|
min_margin
|
float
|
Factor by which the winner must beat the runner-up. |
1.8
|
min_share
|
float
|
Minimum fraction of voting items backing the winner. |
0.0
|
content_min_margin
|
float
|
Stricter margin for the content tier. |
4.0
|
Returns:
| Type | Description |
|---|---|
SchemaDetection | None
|
The accepted detection, or |
SchemaDetection | None
|
default. |
Source code in ontocast/tool/chunk/schema_detect.py
score_content(paragraphs, embed)
¶
Score candidate schemas by body prose against their document profiles.
Weak by construction -- see the module docstring -- and only meaningful for documents with essentially no headings.
Source code in ontocast/tool/chunk/schema_detect.py
score_headings_lexical(headings)
¶
Score candidate schemas on headings only one of them recognises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headings
|
list[str]
|
Raw heading lines in document order. |
required |
Returns:
| Type | Description |
|---|---|
list[SchemaEvidence]
|
Evidence per candidate schema, strongest first. |
Source code in ontocast/tool/chunk/schema_detect.py
score_headings_semantic(headings, embed)
¶
Score candidate schemas by heading-to-label-name similarity.
Each heading casts a single vote for its nearest schema, scaled by how far that schema beat the runner-up, so a heading that is ambiguous between two schemas contributes almost nothing.