API Documentation
This part of the project documentation focuses on
an information-oriented approach. Use it as a
reference for the technical implementation of the
acdh_tei_pyutils project code.
acdh_tei_pyutils.utils
a bunch of helper functions for either
- processing (TEI)/XML nodes like extracting information, or to
- query/parse (TEI)/XML nodes/trees
add_graphic_url_to_pb(doc)
writes url attributes into tei:pb elements fetched from matching tei:surface//tei:graphic[1] elements
Source code in src/acdh_tei_pyutils/utils.py
50 51 52 53 54 55 56 57 58 59 60 | |
any_xpath(node, any_xpath)
Runs any XPath expressions against the passed in node and provides common namespace prefixes like tei:, xml: or `skos
:param node: An lxml.etree Element
:param any_xpath: Any XPath expression, e.g. .//tei:rs
:return: The result of the xpath
Source code in src/acdh_tei_pyutils/utils.py
12 13 14 15 16 17 18 19 | |
check_for_hash(value)
checks if value starts with '#' and if so removes the '#' from the returned value
Source code in src/acdh_tei_pyutils/utils.py
42 43 44 45 46 47 | |
crate_tag_whitelist(element, tag_blacklist)
lists all unique elements from a given node and returns only those not in the given blacklist
Source code in src/acdh_tei_pyutils/utils.py
27 28 29 30 31 32 | |
extract_fulltext(root_node, tag_blacklist=[])
extracts all fulltext from given element and its children, except from blacklisted elements
Source code in src/acdh_tei_pyutils/utils.py
35 36 37 38 39 | |
extract_fulltext_with_spacing(root_node, tag_blacklist=None, block_elements=['p', 'salute', 'dateline', 'closer', 'seg', 'opener', 'div', 'head'])
Extract full text content from an XML element tree with proper spacing. This function recursively traverses an XML element tree and extracts all text content while preserving logical spacing around block-level elements. It handles XML namespaces and respects a blacklist of elements to exclude from extraction. Taken from https://github.com/arthur-schnitzler/schnitzler-briefe-static/blob/main/python/make_typesense_index.py Args: root_node: The root XML element from which to extract text. tag_blacklist (list, optional): A list of element tag names to exclude from text extraction. Elements with tags in this list will be skipped entirely. Defaults to None (empty list). block_elements (tuple, optional): A tuple of tag names that should have spaces added around them. Defaults to ('p', 'salute', 'dateline', 'closer', 'seg', 'opener', 'div', 'head'). Returns: str: The extracted text with normalized spacing. Multiple consecutive whitespace characters are collapsed into a single space, and the result is stripped of leading/trailing whitespace. Notes: - Handles XML namespaced tags by extracting the local name (part after '}'). - Special handling for 'space' elements with unit='chars' attribute. - Preserves tail text from child elements. - Automatically collapses multiple spaces into single spaces using regex.
Source code in src/acdh_tei_pyutils/utils.py
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 282 283 284 285 286 287 288 289 290 | |
get_birth_death_year(person_node, xpath_part='@when', birth=True)
tries to extract birth and death years from person nodes and returns either None or the year as Integer
Source code in src/acdh_tei_pyutils/utils.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
get_xmlid(element)
returns an @xml:id of the given node
Source code in src/acdh_tei_pyutils/utils.py
22 23 24 | |
make_bibl_label(node, no_author='o.A.', no_title='o.T.', year='o.J.', editor_abbr='(Hg.)', max_title_length=75)
creates a nice, bibliograhpically useful label from the passed in tei:biblStruct element
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src/acdh_tei_pyutils/utils.py
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 | |
make_entity_label(name_node, default_msg='no label provided', default_lang='en')
Extracts a label and a lang tag from the past in name-node
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src/acdh_tei_pyutils/utils.py
95 96 97 98 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 | |
normalize_string(string)
removese any superfluos whitespace from a given string
Source code in src/acdh_tei_pyutils/utils.py
90 91 92 | |
previous_and_next(some_iterable)
taken from https://stackoverflow.com/a/1012089
Source code in src/acdh_tei_pyutils/utils.py
82 83 84 85 86 87 | |
acdh_tei_pyutils.tei
TeiEnricher
Bases: TeiReader
a class to enrich tei-documents
Source code in src/acdh_tei_pyutils/tei.py
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 | |
add_base_and_id(base_value, id_value, prev_value, next_value)
adds @xml:base and @xml:id and next and prev to root element
:param base_value: The value of the @xml:base :type base_value: str
:return: the updated tree
Source code in src/acdh_tei_pyutils/tei.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
add_handle(handle, handle_xpath='.//tei:idno[@type="handle"]', insert_xpath='.//tei:publicationStmt/tei:p')
adds an idno @type=handle element into tei:publicationStmt
:param handle: the handle :type handle: str
:param handle_xpath: an xpath expression where to look for an handle :type handle_xpath: str
:raises: HandleAlreadyExist Error
:returns: the indo node
Source code in src/acdh_tei_pyutils/tei.py
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 | |
create_mention_list(mentions, event_title='')
creates a tei element with notes of mentions
:param mentions: a list of dicts with keys doc_id and doc_title
:type mentions: noteGrp
:return: a etree.element
Source code in src/acdh_tei_pyutils/tei.py
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 | |
get_full_id()
returns the combination of @xml:base and @xml:id
:return: combination of @xml:base and @xml:id :rtype: str
Source code in src/acdh_tei_pyutils/tei.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
handle_exist(handle_xpath='.//tei:idno[@type="handle"]')
checks if a handle is already assigned
:return: the registered handle or empty string :rtype: str, None
Source code in src/acdh_tei_pyutils/tei.py
198 199 200 201 202 203 204 205 206 207 | |
TeiReader
Bases: XMLReader
a class to read an process tei-documents
Source code in src/acdh_tei_pyutils/tei.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 | |
any_xpath(any_xpath='//tei:rs')
Runs any xpath expressions against the parsed document :param any_xpath: Any XPath expression. :return: The result of the xpath
Source code in src/acdh_tei_pyutils/tei.py
30 31 32 33 34 35 | |
create_plain_text(node)
extracts all text nodes from given element :param start_node: An XPath expressione pointing to an element which text nodes should be extracted :return: A normalized, cleaned plain text
Source code in src/acdh_tei_pyutils/tei.py
71 72 73 74 75 76 77 78 79 | |
extract_ne_dicts(parent_node, ne_xpath='//tei:rs', NER_TAG_MAP=NER_TAG_MAP)
extract strings tagged as named entities :param ne_xpath: An XPath expression pointing to elements used to tagged NEs. :param NER_TAG_MAP: A dictionary providing mapping from TEI tags used to tag NEs to spacy-tags :return: A list of NE-dicts containing the 'text' and the 'ne_type'
Source code in src/acdh_tei_pyutils/tei.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
extract_ne_elements(parent_node, ne_xpath='//tei:rs')
extract elements tagged as named entities :param ne_xpath: An XPath expression pointing to elements used to tagged NEs. :return: A list of elements
Source code in src/acdh_tei_pyutils/tei.py
37 38 39 40 41 42 43 44 | |
extract_ne_offsets(parent_nodes='.//tei:body//tei:p', ne_xpath='.//tei:rs', NER_TAG_MAP=NER_TAG_MAP)
extracts offsets of NEs and the NE-type :param parent_nodes: An XPath expressione pointing to those element which text nodes should be extracted :param ne_xpath: An XPath expression pointing to elements used to tagged NEs. Takes the parent node(s) as context :param NER_TAG_MAP: A dictionary providing mapping from TEI tags used to tag NEs to spacy-tags :return: A list of spacy-like NER Tuples [('some text'), {'entities': [(15, 19, 'place')]}]
Source code in src/acdh_tei_pyutils/tei.py
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 | |
get_text_nes_list(parent_nodes='.//tei:body//tei:p', ne_xpath='.//tei:rs', NER_TAG_MAP=NER_TAG_MAP)
extracts all text nodes from given elements and their NE :param parent_nodes: An XPath expressione pointing to those elements which text nodes should be extracted :param ne_xpath: An XPath expression pointing to elements used to tagged NEs. Takes the parent node(s) as context :param NER_TAG_MAP: A dictionary providing mapping from TEI tags used to tag NEs to spacy-tags :return: A list of dicts like [{"text": "Wien ist schön", "ner_dicts": [{"text": "Wien", "ne_type": "LOC"}]}]
Source code in src/acdh_tei_pyutils/tei.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
command line interface
Console script for acdh_collatex_utils.
add_base_id_next_prev(glob_pattern, base_value)
Console script add @xml:base, @xml:id and @prev @next attributes to root element
Source code in src/acdh_tei_pyutils/cli.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
denormalize_indices(files, indices, mention_xpath, title_xpath, title_sec_xpath, date_xpath, standoff, blacklist_ids=[])
Write pointers to mentions in index-docs and copy index entries into docs
Source code in src/acdh_tei_pyutils/cli.py
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 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
mentions_to_indices(files, indices, mention_xpath, event_title, title_xpath)
Console script write pointers to mentions in index-docs
Source code in src/acdh_tei_pyutils/cli.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 | |
schnitzler(files, indices, doc_person, doc_work)
Console script write pointers to mentions in index-docs
Source code in src/acdh_tei_pyutils/cli.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |