ontocast.util.numeric_inventory¶
Domain-agnostic numeric-mention inventory for coverage checking.
Compares numbers stated in a source text against numeric literals present in the extracted graph. The comparison is deliberately verbatim-oriented: extraction is expected to transcribe source values exactly (units are normalized downstream in code, never by the LLM), so a text number missing from the graph is a candidate extraction gap.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
NumericInventory
dataclass
¶
Numbers stated in a text, split by whether a unit stands next to them.
measurements are unit-adjacent mentions in text order, one per
distinct (value, unit), each carrying the unit token and the phrase
it occurs in; a stated measurement is a fact the graph is expected to
hold, and the context is what lets a later pass place it.
unclassified are the bare numbers, shortest-first: a value whose
unit sits elsewhere in the sentence, or typography, and nothing in the
text alone says which.
Source code in ontocast/util/numeric_inventory.py
Attributes¶
is_empty
property
¶
measurements = field(default_factory=list)
class-attribute
instance-attribute
¶
unclassified = field(default_factory=list)
class-attribute
instance-attribute
¶
Methods:¶
__init__(measurements=list(), unclassified=list())
¶
measurement_values()
¶
Canonical values of the measurements, in text order.
Functions:¶
canonical_number(text)
¶
Return the canonical decimal form of a numeric string, or None.
Source code in ontocast/util/numeric_inventory.py
extract_numeric_tokens(text, *, ignore_year_like=True, ignore_identifier_fragments=False)
¶
Extract canonical numeric tokens from free text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text. |
required |
ignore_year_like
|
bool
|
Drop bare integers in the 1900-2100 range (years, citation artifacts). Values that also occur with a decimal point are kept. |
True
|
ignore_identifier_fragments
|
bool
|
Drop digit groups sitting against an
identifier separator -- see :func: |
False
|
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of canonical decimal strings. |
Source code in ontocast/util/numeric_inventory.py
inventory_numeric_mentions(text, *, unit_surfaces=frozenset(), ignore_year_like=True, ignore_identifier_fragments=False)
¶
Split the numbers of text into measurements and bare numbers.
A number is a measurement when a unit surface stands next to it -- from
the built-in lexicon or from unit_surfaces, typically the labels and
symbols of the unit individuals in the unit's ontology context. The
year-like and identifier guards apply to the bare numbers only: a number
written with its unit is a measurement whatever its magnitude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text of the unit. |
required |
unit_surfaces
|
Collection[str]
|
Extra unit surfaces beyond the built-in lexicon. |
frozenset()
|
ignore_year_like
|
bool
|
Drop bare integers in the publication-year span. |
True
|
ignore_identifier_fragments
|
bool
|
Drop bare digit groups that are parts of an identifier. |
False
|
Returns:
| Type | Description |
|---|---|
NumericInventory
|
The inventory; measurements in text order, bare numbers shortest-first. |
Source code in ontocast/util/numeric_inventory.py
measurement_pairs_in_graph(graph, ontology_graph=None, *, numeric_value_properties=(), unit_properties=())
¶
(canonical_number, unit_surface_key) pairs structured in graph.
A subject contributes pairs only when it carries both a configured
numeric-value literal and a configured unit object IRI -- a bare number
elsewhere in the graph does not cover a unit-adjacent mention. Unit IRIs
map to surfaces through :func:unit_surface_index; each surface is
expanded with :func:~ontocast.util.measurement_lexicon._lookup_keys so
the same case and plural rules that recognise a mention also recognise
its extracted counterpart. When the ontology has no surface for an IRI,
the IRI's local name is tried as a last resort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
The extracted facts graph. |
required |
ontology_graph
|
Graph | None
|
The unit's ontology context (for unit surfaces). |
None
|
numeric_value_properties
|
Collection[str]
|
IRIs of the numeric-value role properties. |
()
|
unit_properties
|
Collection[str]
|
IRIs of the unit-role properties. |
()
|
Returns:
| Type | Description |
|---|---|
set[tuple[str, str]]
|
Pairs keyed for membership tests against mention lookup keys. |
Source code in ontocast/util/numeric_inventory.py
missing_numeric_inventory(text, graph, *, unit_surfaces=frozenset(), ontology_graph=None, numeric_value_properties=(), unit_properties=(), ignore_year_like=True, ignore_identifier_fragments=False, limit=30)
¶
The inventory of text restricted to values absent from the graph.
Measurements are judged against structured (number, unit) pairs in
the graph: a bare numeric literal does not clear a unit-adjacent mention.
Bare numbers still use the number-only presence set. Capped at limit
over both lists, measurements first: they are the numbers a later pass
can act on, so when the cap bites it is the bare numbers that are
dropped. A warning records how many were.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text for the unit. |
required |
graph
|
RDFGraph
|
Graph extracted from that text. |
required |
unit_surfaces
|
Collection[str]
|
Extra unit surfaces beyond the built-in lexicon. |
frozenset()
|
ontology_graph
|
Graph | None
|
Ontology context used to map unit IRIs to surfaces. |
None
|
numeric_value_properties
|
Collection[str]
|
IRIs of the numeric-value role properties. |
()
|
unit_properties
|
Collection[str]
|
IRIs of the unit-role properties. |
()
|
ignore_year_like
|
bool
|
Drop bare integers in the publication-year span. |
True
|
ignore_identifier_fragments
|
bool
|
Drop bare digit groups that are parts of an identifier. Offering them invites the critic to structure a file number or a citation into numeric properties, which the downstream multi-value check then flags. |
False
|
limit
|
int
|
Maximum mentions across both lists. |
30
|
Returns:
| Type | Description |
|---|---|
NumericInventory
|
The missing measurements in text order and the missing bare numbers |
NumericInventory
|
shortest-first. |
Source code in ontocast/util/numeric_inventory.py
missing_numeric_mentions(text, graph, *, ignore_year_like=True, ignore_identifier_fragments=False, limit=30, unit_surfaces=frozenset(), ontology_graph=None, numeric_value_properties=(), unit_properties=())
¶
Return canonical numbers stated in text but absent from the graph.
Measurements come first in text order, then bare numbers shortest-first;
see :func:missing_numeric_inventory for the split and the cap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text for the unit. |
required |
graph
|
RDFGraph
|
Graph extracted from that text. |
required |
ignore_year_like
|
bool
|
Drop bare integers in the publication-year span. |
True
|
ignore_identifier_fragments
|
bool
|
Drop bare digit groups that are parts of an identifier. |
False
|
limit
|
int
|
Maximum mentions returned. |
30
|
unit_surfaces
|
Collection[str]
|
Extra unit surfaces beyond the built-in lexicon. |
frozenset()
|
ontology_graph
|
Graph | None
|
Ontology context used to map unit IRIs to surfaces. |
None
|
numeric_value_properties
|
Collection[str]
|
IRIs of the numeric-value role properties. |
()
|
unit_properties
|
Collection[str]
|
IRIs of the unit-role properties. |
()
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Canonical decimal strings, capped at |
Source code in ontocast/util/numeric_inventory.py
numeric_literals_in_graph(graph, *, include_annotations=False)
¶
Collect canonical numeric values appearing in graph literals.
By default numbers inside labels, comments, SKOS notes and descriptions do not count as present. Counting them let a placeholder node labelled with the missing number silence the coverage finding that asked for it, so the lane measured whether a number had been mentioned rather than whether it had been extracted; a value that exists only inside a label is invisible to every query and to SHACL alike.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
The graph to inventory. |
required |
include_annotations
|
bool
|
Count numbers inside annotation literals too. |
False
|
Source code in ontocast/util/numeric_inventory.py
unit_surface_index(ontology_graph, unit_properties=())
¶
Surface form -> unit individuals declaring it, for one ontology graph.
Unit individuals are found through the ranges of the configured unit-role
properties and through classes named *Unit, with their subclasses.
Surfaces are labels, notations and code/symbol literals short enough to
stand next to a number. Memoised per graph object (validated by size, so
a graph mutated in place is re-indexed), because the snapshot is shared
by reference across a whole fan-out and this walks it whole.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_graph
|
Graph | None
|
The unit's ontology context; |
required |
unit_properties
|
Collection[str]
|
IRIs of the unit-role properties ( |
()
|
Returns:
| Type | Description |
|---|---|
_SurfaceIndex
|
Surface -> sorted unit IRIs. Treat as read-only. |
Source code in ontocast/util/numeric_inventory.py
unit_surfaces_in_ontology(ontology_graph, unit_properties=())
¶
The unit surfaces of an ontology graph; see :func:unit_surface_index.
Source code in ontocast/util/numeric_inventory.py
unit_symbol_index(ontology_graph, unit_properties=())
¶
Unit individual -> the symbol surfaces it declares, case preserved.
The subset of :func:unit_surface_index that comes from code, symbol
and notation predicates rather than labels. Symbols are case-significant
by definition -- two units can differ by letter case alone -- while a
label is prose and is not. Kept separate so a check on symbol case never
fires on a label spelt with a capital.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_graph
|
Graph | None
|
The unit's ontology context; |
required |
unit_properties
|
Collection[str]
|
IRIs of the unit-role properties ( |
()
|
Returns:
| Type | Description |
|---|---|
dict[str, frozenset[str]]
|
Unit IRI -> its declared symbol surfaces. Empty when the graph |
dict[str, frozenset[str]]
|
declares no symbols. |