medspacy.section_detection.sectionizer
Sectionizer
The Sectionizer will search for spans in the text which match section header rules, such as 'Past Medical History:'. Sections will be represented in custom attributes as: category: A normalized title of the section. Example: 'past_medical_history' section_title: The Span of the doc which was matched as a section header. Example: 'Past Medical History:' section_span: The entire section of the note, starting with section_header and up until the end of the section, which will be either the start of the next section header of some pre-specified scope. Example: 'Past Medical History: Type II DM'
Section attributes will be registered for each Doc, Span, and Token in the following attributes: Doc..sections: A list of namedtuples of type Section with 4 elements: - section_title - section_header - section_parent - section_span. A Doc will also have attributes corresponding to lists of each (ie., Doc..section_titles, Doc..section_headers, Doc..section_parents, Doc..section_list) (Span|Token)..section_title (Span|Token)..section_header (Span|Token)..section_parent (Span|Token)._.section_span
Source code in medspacy/section_detection/sectionizer.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 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 348 349 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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
input_span_type
property
writable
The input source of entities for the component. Must be either "ents" corresponding to doc.ents or "group" for a spaCy span group.
Returns:
| Type | Description |
|---|---|
|
The input type, "ents" or "group". |
rules
property
Gets list of rules associated with the Sectionizer.
Returns:
| Type | Description |
|---|---|
List[SectionRule]
|
The list of SectionRules associated with the Sectionizer. |
section_categories
property
Gets a list of categories used in the Sectionizer.
Returns:
| Type | Description |
|---|---|
Set[str]
|
The list of all section categories available to the Sectionizer. |
span_group_name
property
writable
The name of the span group used by this component. If input_type is "group", calling this component will
use spans in the span group with this name.
Returns:
| Type | Description |
|---|---|
str
|
The span group name. |
__call__(doc)
Call the Sectionizer on a spaCy doc. Sectionizer will identify sections using provided rules, then evaluate any section hierarchy as needed, create section spans, and modify attributes on existing spans based on the sections the entities spans in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Doc
|
The Doc to process. |
required |
Returns:
| Type | Description |
|---|---|
Doc
|
The processed spaCy Doc. |
Source code in medspacy/section_detection/sectionizer.py
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
__init__(nlp, name='medspacy_sectionizer', rules='default', language_code='en', max_section_length=None, phrase_matcher_attr='LOWER', require_start_line=False, require_end_line=False, newline_pattern='[\\n\\r]+[\\s]*$', input_span_type='ents', span_group_name='medspacy_spans', span_attrs='default', apply_sentence_boundary=False)
Create a new Sectionizer component.
Args:
nlp: A SpaCy Language object.
name: The name of the component.
rules: The rules to load. Default is "default", loads rules packaged with medspaCy that are derived from
SecTag, MIMIC-III, and practical refinement at the US Department of Veterans Affairs. If None, no rules
are loaded. Otherwise, must be a path to a json file containing rules. Add SectionRules directly through
`Sectionizer.add`.
language_code: Language code to use (ISO code) as a default for loading resources. See documentation
and also the /resources directory to see which resources might be available in each language.
Default is "en" for English.
max_section_length: Optional argument specifying the maximum number of tokens following a section header
which can be included in a section body. This can be useful if you think your section rules are
incomplete and want to prevent sections from running too long in the note. Default is None, meaning that
the scope of a section will be until either the next section header or the end of the document.
phrase_matcher_attr: The token attribute to use for PhraseMatcher for rules where `pattern` is None. Default
is 'LOWER'.
require_start_line: Optionally require a section header to start on a new line. Default False.
require_end_line: Optionally require a section header to end with a new line. Default False.
newline_pattern: Regular expression to match the new line either preceding or following a header
if either require_start_line or require_end_line are True. Default is r"[
]+[\s]*$"
span_attrs: The optional span attributes to modify. Default option "default" uses attributes in
DEFAULT_ATTRIBUTES. If a dictionary of custom attributes, format is a dictionary mapping section
categories to a dictionary containing the attribute name and the value to set the attribute to when a
span is contained in a section of that category. Custom attributes must be assigned with
Span.set_extension before creating the Sectionizer. If None, sectionizer will not modify span
attributes.
input_span_type: "ents" or "group". Where to look for spans when modifying attributes of spans
contained in a section if span_attrs is not None. "ents" will modify attributes of spans in doc.ents.
"group" will modify attributes of spans in the span group specified by span_group_name.
span_group_name: The name of the span group used when input_span_type is "group". Default is
"medspacy_spans".
apply_sentence_boundary: Optionally end sentence before and after section header boundary. This ensures
the section header is considered its own sentence.
Source code in medspacy/section_detection/sectionizer.py
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 152 | |
add(rules)
Adds SectionRules to the Sectionizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
A single SectionRule or a collection of SectionRules to add to the Sectionizer. |
required |
Source code in medspacy/section_detection/sectionizer.py
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 | |
filter_end_lines(doc, matches)
Filter a list of matches to only contain spans where the start token is followed by a new line.
Returns:
| Type | Description |
|---|---|
List[Tuple[int, int, int]]
|
A list of match tuples (match_id, start, end) that meet the filter criteria. |
Source code in medspacy/section_detection/sectionizer.py
503 504 505 506 507 508 509 510 511 512 513 514 | |
filter_start_lines(doc, matches)
Filter a list of matches to only contain spans where the start token is the beginning of a new line.
Returns:
| Type | Description |
|---|---|
List[Tuple[int, int, int]]
|
A list of match tuples (match_id, start, end) that meet the filter criteria. |
Source code in medspacy/section_detection/sectionizer.py
490 491 492 493 494 495 496 497 498 499 500 501 | |
register_default_attributes()
classmethod
Register the default values for the Span attributes defined in DEFAULT_ATTRIBUTES.
Source code in medspacy/section_detection/sectionizer.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
set_assertion_attributes(spans)
Add Span-level attributes to entities based on which section they occur in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
Iterable[Span]
|
the spans to modify. |
required |
Source code in medspacy/section_detection/sectionizer.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
set_parent_sections(sections)
Determine the legal parent-child section relationships from the list of in-order sections of a document and the possible parents of each section as specified during direction creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sections
|
List[Tuple[int, int, int]]
|
a list of spacy match tuples found in the doc |
required |
Returns:
| Type | Description |
|---|---|
List[Tuple[int, int, int, int]]
|
A list of tuples (match_id, start, end, parent_idx) where the first three indices are the same as the input |
List[Tuple[int, int, int, int]]
|
and the added parent_idx represents the index in the list that corresponds to the parent section. Might be a |
List[Tuple[int, int, int, int]]
|
smaller list than the input due to pruning with |
Source code in medspacy/section_detection/sectionizer.py
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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |