Classification System¶
Two interfaces. ClassificationSystem is a flat set of codes and is what the
retrieval-based classifiers are written against; HierarchicalClassificationSystem adds
the parent/child accessors and is what the hierarchy traversal agent and taxonomy
refinement require. See Classification Systems for how to
implement one for your own taxonomy.
Code¶
rac4os._shared.Code.Code
dataclass
¶
The code class works a the base datatype for storing, retrieving and working with
codes from the classification systems. More or less every hierarchical classification
system in official statistics, like SEA or COICOP haven a extensive documentation available,
what always contains
* code - Code in Form of digitis, e.g. 01111
* description - overall description of the content of the code, e.g. Cereals (ND)
* level - the level of the code inside the systems hierarchy, e.g. the corresponding level for the code 01111 is 4.
* detailled_description - A more detailled description of what should be classified inside of a certain category, can be found in e.g. the COICOP documentation under introductory_notes
* details - here will be all other details stored, that come on top of all the previous informations, e.g. explicit exclusion, inclusion, etc.
Source code in rac4os/_shared/Code.py
from_dict
classmethod
¶
Loads in a code form a dictionary and saves it as a Code object. All keys that don't exist in the dict but that exist in the Code object will be left blank. It is imprtant that the codes inserted here match the required datarypes defined, otherwise it will break here or deeper down the pipeline.
Source code in rac4os/_shared/Code.py
to_json
¶
ClassificationSystem¶
rac4os.classification_system.BaseClassificationSystem.ClassificationSystem
¶
Bases: ABC
A flat classification: a set of codes with no structure between them.
This is the smaller of the two interfaces and the one the retrieval-based classifiers are typed against, because they never navigate a taxonomy. They look a code up and ask whether a generated code is one of yours, which is all a flat system has to answer. If your categories genuinely have no hierarchy — a flat code list, a set of labels — this is the whole interface you need, and you can use it directly rather than subclassing it.
If your codes do nest, use HierarchicalClassificationSystem instead. It adds the
parent/child accessors on top of everything here, and the hierarchy traversal
agent and the taxonomy refinement module both require them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
ClassificationLoader
|
loader that yields the |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
loader |
ClassificationLoader
|
the loader that was passed in. |
codes |
list[Code]
|
every record the loader returned, in source order. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
__init__
¶
Loads the taxonomy through the loader and builds the code lookup.
This happens eagerly, so constructing a classification system reads and parses the source file.
Source code in rac4os/classification_system/BaseClassificationSystem.py
add_code
¶
Add a single code to the live system without rebuilding it.
Useful for patching in a category the source documentation omits. Only the lookup
is touched; self.codes stays as the loader returned it, so the addition does not
survive into a system built freshly from the same source.
A flat system has nowhere to file the new code, so it takes no parent. The hierarchical subclass overrides this and accepts one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
dict
|
a |
required |
Source code in rac4os/classification_system/BaseClassificationSystem.py
get_code
¶
Look up the full record for a code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
the code to look up, written the way the classification system writes it. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Code |
Code
|
the record for that code. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the code is not part of this classification system. This is far more often a formatting mismatch, such as a missing separator, than a genuinely absent category. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
is_valid_code
¶
Whether a code belongs to this classification system.
This is what the classifiers check a generated code against, so it decides what
counts as a usable answer. The comparison is exact: a code in the wrong notation,
with stray whitespace, or carrying a separator the system does not use is not
valid, even when the category it refers to exists. Codes added at runtime with
add_code are included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str | None
|
the code to check. None and the empty string are not valid. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the code is one of this system's codes. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
HierarchicalClassificationSystem¶
rac4os.classification_system.BaseClassificationSystem.HierarchicalClassificationSystem
¶
Bases: ClassificationSystem
A classification whose codes nest: the interface for a taxonomy with levels.
On top of the flat system's lookup it answers the structural questions — what the first level is, and what sits below or above a code. This is what the hierarchy traversal agent walks and what taxonomy refinement reads children from; the retrieval-based classifiers accept it too, since it is a classification system like any other, but they only use the flat part.
Subclasses implement get_prefixes and get_root_categories and nothing else. The
parent/child register, get_children, get_parent and get_code_trace are all
derived from those two, so describing your own taxonomy comes down to answering two
questions about how its codes are written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
ClassificationLoader
|
loader that yields the |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
loader |
ClassificationLoader
|
the loader that was passed in. |
codes |
list[Code]
|
every record the loader returned, in source order. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 271 272 273 274 275 276 277 278 279 280 281 | |
__init__
¶
Loads the taxonomy as the flat system does, then derives the hierarchy from it.
The parent/child register is built here, which means get_prefixes is called once
per code at construction: a mistake in it surfaces as a wrong hierarchy right
away rather than at the first navigation call.
Source code in rac4os/classification_system/BaseClassificationSystem.py
get_prefixes
abstractmethod
¶
Return every prefix of a code, from the broadest level down to the code itself.
This is the one method that encodes how your taxonomy writes its codes: separators, fixed-width levels, padding. The last element is the code itself and the second-to-last is its parent, which is what the parent/child register is built from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
a code of this classification system. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: the code's prefixes, broadest first, ending with the code itself. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
get_root_categories
abstractmethod
¶
Return the first level of the taxonomy.
The hierarchy traversal agent starts here and descends, so this is the entry point
into your classification. Each entry carries at least code and description.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: the categories at the first level of the hierarchy. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
add_code
¶
Add a single code to the live system without rebuilding it, optionally under a parent.
Useful for patching in a category the source documentation omits. Only the lookup and
the parent's child list are touched; self.codes stays as the loader returned it, so
the addition does not survive into a system built freshly from the same source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
dict
|
a |
required |
parent
|
str | None
|
code to file the new entry under. Left out, the code is reachable through |
None
|
Source code in rac4os/classification_system/BaseClassificationSystem.py
get_children
¶
Return the categories directly below a code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
the parent code. |
required |
Returns:
| Type | Description |
|---|---|
list[Code]
|
list[Code]: the direct children; an empty list for a leaf or for a code the system does not know. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
get_parent
¶
Return the category directly above a code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
str
|
the code whose parent is wanted. |
required |
Returns:
| Type | Description |
|---|---|
Code | None
|
Code|None: the parent record, or None if the code is already at the first level. |
Source code in rac4os/classification_system/BaseClassificationSystem.py
get_code_trace
¶
Return the path from the first level down to a code.
Each step is a (code, description) pair, so the trace reads as the chain of
categories a human coder would have walked through. Prefixes that are not themselves
categories of the system are skipped rather than raising, which keeps the trace usable
for taxonomies whose levels are not all populated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
the code whose path is wanted. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str]]
|
list[tuple[str, str]]: |
Source code in rac4os/classification_system/BaseClassificationSystem.py
Registry and dataset mapping¶
rac4os.classification_system.registry
¶
register
¶
Class decorator that registers a HierarchicalClassificationSystem implementation
under a name.
The name is the one a dataset refers to in mapping through its
classification_system key, which is how get_classification_system finds the class
to build.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
key to register the decorated class under. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
the decorator, which registers the class and returns it unchanged. |
Source code in rac4os/classification_system/registry.py
get_classification_system
¶
Build the classification system for a registered dataset name.
Looks the name up in mapping, builds the loader named there for the data path given
there, and hands that loader to the registered ClassificationSystem subclass. This
is purely a convenience: it exists so a dataset can be referred to by one name instead
of wiring a loader and a system class together at every call site. Constructing the
two by hand stays equally valid and is what you do for a taxonomy you have not
registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
a key of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
HierarchicalClassificationSystem |
HierarchicalClassificationSystem
|
a system built from that dataset, ready to query.
Every registered dataset is hierarchical; a flat classification is built by
constructing |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the name is not in |
Source code in rac4os/classification_system/registry.py
Bundled systems¶
ClassificationSystemICATUS
¶
Bases: HierarchicalClassificationSystem
Classification system for ICATUS 2016, registered under "ICATUS".
ICATUS codes are plain digit strings whose length is their level, so each additional
digit descends one level: 1 above 11 above 111.
Source code in rac4os/classification_system/systems/ClassificationSystems.py
get_root_categories
¶
Return the first level of the hierarchy.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one |
Source code in rac4os/classification_system/systems/ClassificationSystems.py
get_prefixes
¶
Return the prefixes of a ICATUS code, broadest first.
One prefix per leading digit, since a code's length is its level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
a ICATUS code. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: the code's prefixes, ending with the code itself. |
Source code in rac4os/classification_system/systems/ClassificationSystems.py
ClassificationSystemCOICOP
¶
Bases: HierarchicalClassificationSystem
Classification system for COICOP 2018, registered under "COICOP".
COICOP codes are dot-separated groups of digits and the number of groups is the level,
so 01 sits above 01.1, which sits above 01.1.1.
Source code in rac4os/classification_system/systems/ClassificationSystems.py
get_root_categories
¶
Return the first level of the hierarchy.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one |
Source code in rac4os/classification_system/systems/ClassificationSystems.py
get_prefixes
¶
Return the prefixes of a COICOP code, broadest first.
The code is split on the dot and rejoined one group at a time, so the separator is preserved in every prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
a COICOP code. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: the code's prefixes, ending with the code itself. |
Source code in rac4os/classification_system/systems/ClassificationSystems.py
ClassificationSystemSEA
¶
Bases: HierarchicalClassificationSystem
Classification system for the SEA 2021, registered under "SEA".
SEA codes follow COICOP down to four digits and then continue after a space, so 01
sits above 0111, which sits above 0111 100. Spaces are stripped before the
hierarchy is derived and re-inserted after the fourth digit.
Source code in rac4os/classification_system/systems/ClassificationSystems.py
get_root_categories
¶
Return the first level of the hierarchy.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: one |
Source code in rac4os/classification_system/systems/ClassificationSystems.py
get_prefixes
¶
Return the prefixes of a SEA code, broadest first.
Spaces are removed first; prefixes up to four digits are cut directly, and longer ones get a space re-inserted after the fourth digit, which is the notation the published SEA uses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
a SEA code. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: the code's prefixes, ending with the code itself. |