Rubrics
A rubric declares the node types of a graph built from a document's own text,
the form each type carries and what each type must rest on. load_rubric reads
one from TOML; check_graph(graph, rubric, level) returns every violation as a
Finding whose diagnostic is its code; coverage reports how far
classification has got; split_node and annotate_node return a new graph with
the edit applied. The contract they implement is
Verbatim ingestion.
Rubric-driven classification of graphs built from a document's own text.
A rubric declares the node types, their forms and their edge rules. check_graph checks a graph
against it at the anchored, typed or assessed level, coverage reports how far the
classification has got, and split_node and annotate_node make the edits that classify it.
FormField
dataclass
¶
A field of a type's form.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The field's name. |
type |
str
|
One of |
required |
bool
|
Whether a typed node must have it. |
values |
tuple[str, ...]
|
The allowed strings of an |
Source code in src/credencegraph/rubric/model.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
NodeType
dataclass
¶
A node type declared by a rubric.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The type's name, stored as a node's |
description |
str
|
What the type is for, shown in reports. |
origins |
tuple[str, ...]
|
The origins the type may be used with. |
assess |
bool
|
Whether a node of the type needs an assessment at the |
base |
Credence | None
|
The base |
form |
Mapping[str, FormField]
|
The form's fields by name, in declaration order. |
rests_on |
RestsOn | None
|
The edge rule, if any. |
Source code in src/credencegraph/rubric/model.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
Parameter
dataclass
¶
A parameter that scope fields may range over.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The parameter's name. |
unit |
str
|
Its unit, for readers. |
domain |
tuple[float | None, float | None]
|
|
Source code in src/credencegraph/rubric/model.py
49 50 51 52 53 54 55 56 57 58 59 60 61 | |
RestsOn
dataclass
¶
A type's edge rule: what a node of the type must rest on.
Attributes:
| Name | Type | Description |
|---|---|---|
types |
tuple[str, ...]
|
The types a supporting node may have. |
min |
int
|
How many supporting relations the node needs at least. |
relation |
str
|
|
strength |
Credence
|
The strength |
Source code in src/credencegraph/rubric/model.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
Rubric
dataclass
¶
A rubric: node types, their forms and edge rules, and the parameters scopes range over.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Identifies the rubric in reports. |
version |
str
|
The rubric's own version, chosen by its author. |
types |
Mapping[str, NodeType]
|
The declared types by name, in declaration order. |
parameters |
Mapping[str, Parameter]
|
The parameters by name. |
initial |
Mapping[str, str]
|
The type each chunk unit starts with, by unit. |
Source code in src/credencegraph/rubric/model.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
identity()
¶
Return the rubric's name and version, as echoed in every report.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
|
Source code in src/credencegraph/rubric/model.py
139 140 141 142 143 144 145 | |
RubricError
¶
Bases: CredenceGraphError
A rubric file is malformed, of an unknown format, or breaks a rule of the rubric format.
Source code in src/credencegraph/rubric/model.py
45 46 | |
annotate_node(graph, rubric, node_id, *, node_type=None, settings=None, rests_on=(), verdict=None, reason=None)
¶
Type, fill in, link and assess a node, validating each write against the rubric.
The steps run in that order, so a type given here is the one the form and links are checked
against. Required form fields may be left out: they are filled one at a time, and
check_graph at typed reports the ones still missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph, left unchanged. |
required |
rubric
|
Rubric
|
The rubric. |
required |
node_id
|
str
|
The node. |
required |
node_type
|
str | None
|
A declared type allowed for the node's origin, or |
None
|
settings
|
Mapping[str, Any] | None
|
Form fields to set, by name; each must be declared by the node's type and its value must have the field type and be found in the node's text. |
None
|
rests_on
|
Sequence[str]
|
Nodes to add the type's edge-rule relation from, with the rule's strength. |
()
|
verdict
|
str | None
|
|
None
|
reason
|
str | None
|
Why the node was not assessed; sets the verdict |
None
|
Returns:
| Type | Description |
|---|---|
Graph
|
The new graph. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If any write breaks the rubric, or a relation would close a cycle. |
Source code in src/credencegraph/rubric/edit.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
check_graph(graph, rubric, level=TYPED)
¶
Check a graph against a rubric at a validation level and every level below it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph. |
required |
rubric
|
Rubric
|
The rubric. |
required |
level
|
str
|
|
TYPED
|
Returns:
| Type | Description |
|---|---|
list[Finding]
|
Every violation: the |
list[Finding]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the level is not one of |
Source code in src/credencegraph/rubric/validate.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | |
coverage(graph, rubric)
¶
Report how far classification has got. Never fails: it is a report, not a gate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph. |
required |
rubric
|
Rubric
|
The rubric. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
|
dict[str, object]
|
|
dict[str, object]
|
first appear, each with its |
dict[str, object]
|
|
dict[str, object]
|
assessment, in graph order. A node without a valid origin is counted in |
Source code in src/credencegraph/rubric/validate.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
has_eqref(text, label)
¶
Tell whether a text references an equation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text. |
required |
label
|
str
|
An equation label, matched by |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the text references it. |
Source code in src/credencegraph/rubric/faithfulness.py
106 107 108 109 110 111 112 113 114 115 116 117 118 | |
has_numeral(text, number)
¶
Tell whether a number appears as a numeral in a text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text. |
required |
number
|
float
|
The number. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether some numeral of the text parses to the same float. |
Source code in src/credencegraph/rubric/faithfulness.py
70 71 72 73 74 75 76 77 78 79 80 | |
has_unit(text, unit)
¶
Tell whether a unit appears in a text as a token, delimited by whitespace or punctuation.
The argument of \mathrm{…}, \text{…} and \si{…} is delimited by its braces, so it
counts as a token too. A boundary is required only where the unit begins or ends with a letter
or digit: it keeps Hz from matching inside kHz or 100Hz, while a unit that starts
with punctuation, such as the % of 1.1%, is itself delimited from the numeral before it.
The operators /, ^, ·, ⋅, * and the multiplication sign join symbols into one
compound unit, and the subscript operator _ joins a symbol to its subscript, rather than
delimit them, so m is not a token of m/s, m^2 or m_e; the compound itself is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text. |
required |
unit
|
str
|
The unit, such as |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the unit is a token of the text. |
Source code in src/credencegraph/rubric/faithfulness.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
load_rubric(path)
¶
Read a rubric file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The TOML file. |
required |
Returns:
| Type | Description |
|---|---|
Rubric
|
The rubric. |
Raises:
| Type | Description |
|---|---|
RubricError
|
If the file is not TOML or breaks a rule of the rubric format. |
OSError
|
If the file cannot be read. |
Source code in src/credencegraph/rubric/model.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
loads_rubric(text)
¶
Read a rubric from TOML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The TOML document. |
required |
Returns:
| Type | Description |
|---|---|
Rubric
|
The rubric. |
Raises:
| Type | Description |
|---|---|
RubricError
|
If the text is not TOML or breaks a rule of the rubric format. |
Source code in src/credencegraph/rubric/model.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
normalise_text(text)
¶
Normalise a text for storage: Unicode NFC, whitespace runs collapsed to one space, ends trimmed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text as read from the source. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The normalised text. |
Source code in src/credencegraph/rubric/text.py
18 19 20 21 22 23 24 25 26 27 | |
numerals(text)
¶
Return the values of every numeral in a text.
A numeral followed by % contributes both its value and its value divided by 100. Values are
computed in decimal arithmetic and rounded once, so 1.1% gives exactly the float 0.011.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text. |
required |
Returns:
| Type | Description |
|---|---|
set[float]
|
The values. |
Source code in src/credencegraph/rubric/faithfulness.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
parse_value(text)
¶
Read a form value from the command line: as JSON if it parses, else as the string itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The value as typed. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The value. |
Source code in src/credencegraph/rubric/edit.py
201 202 203 204 205 206 207 208 209 210 211 212 213 | |
rubric_from_dict(data)
¶
Build a rubric from the data of a rubric file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The parsed TOML document. |
required |
Returns:
| Type | Description |
|---|---|
Rubric
|
The rubric. |
Raises:
| Type | Description |
|---|---|
RubricError
|
If the data breaks a rule of the rubric format, or its format is unknown. |
Source code in src/credencegraph/rubric/model.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
split_node(graph, node_id, clauses=(), *, fields=False)
¶
Make a document chunk a compound, with one span child per clause or one field child.
A span child's text is a clause of the chunk, its locator the chunk's plus ;chars=<start>-<end>.
A field child has no text, a copy of the chunk's anchor, and gets its content from a form later.
Children are <node id>a, <node id>b, … in the order created, joined to the chunk by a
part-of relation, and start unassigned. Splitting a compound adds children to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph, left unchanged. |
required |
node_id
|
str
|
The chunk to split. |
required |
clauses
|
Sequence[str]
|
The clauses, each occurring exactly once in the chunk's text. |
()
|
fields
|
bool
|
Add one field child instead of span children. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Graph, list[str]]
|
The new graph, and the ids of the children added. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the node is not a top-level document chunk with a locator, carries a form,
an assessment or a base, takes part in a relation a compound may not, or a clause does not
occur exactly once in its text; or if both or neither of |
Source code in src/credencegraph/rubric/edit.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
text_digest(text)
¶
Return the digest of a text: sha256: and the hex SHA-256 of its normalised UTF-8 bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The digest, such as |
Source code in src/credencegraph/rubric/text.py
30 31 32 33 34 35 36 37 38 39 | |