medspacy.context.context_modifier
ConTextModifier
Represents a concept found by ConText in a document. An instance of this class is the result of ConTextRule matching text in a Doc.
Source code in medspacy/context/context_modifier.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 | |
allowed_types
property
Returns the associated allowed types.
category
property
Returns the associated category.
direction
property
Returns the associated direction.
excluded_types
property
Returns the associated excluded types.
max_scope
property
Returns the associated maximum scope.
max_targets
property
Returns the associated maximum number of targets.
modifier_span
property
The spaCy Span object, which is a view of self.doc, covered by this match.
num_targets
property
Returns the associated number of targets.
rule
property
Returns the associated context rule.
scope_span
property
Returns the associated scope.
__init__(context_rule, start, end, doc, scope_start=None, scope_end=None, max_scope=None)
Create a new ConTextModifier from a document span. Each modifier represents a span in the text and a surrounding window. Spans such as entities or other members of span groups that occur within this window can be modified by this ConTextModifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_rule
|
ConTextRule
|
The ConTextRule object which defines the modifier. |
required |
start
|
int
|
The start token index. |
required |
end
|
int
|
The end token index (non-inclusive). |
required |
doc
|
Doc
|
The spaCy Doc which contains this span. This is needed to initialize the modifier but is not maintained. |
required |
scope_start
|
Optional[int]
|
The start token index of the scope. |
None
|
scope_end
|
Optional[int]
|
The end index of the scope. |
None
|
max_scope
|
Optional[int]
|
Whether to use scope values rather than sentence boundaries for modifications. |
None
|
Source code in medspacy/context/context_modifier.py
18 19 20 21 22 23 24 25 26 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 | |
__set_scope(doc)
Applies the direction of the ConTextRule which generated this ConTextModifier to define a scope. If self._max_scope is None, then the default scope is the sentence which it occurs in whichever direction defined by self.direction. For example, if the direction is "forward", the scope will be [self.end: sentence.end]. If the direction is "backward", it will be [self.start: sentence.start].
If self.max_scope is not None and the length of the default scope is longer than self.max_scope, it will be reduced to self.max_scope.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Doc
|
The spaCy doc to use to set scope. |
required |
Source code in medspacy/context/context_modifier.py
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 | |
allows(target_label)
Returns whether if a modifier is able to modify a target type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_label
|
str
|
The target type to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the modifier is allowed to modify a target of the specified type. True if |
bool
|
|
Source code in medspacy/context/context_modifier.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
from_serialized_representation(serialized_representation)
classmethod
Instantiates the class from the serialized representation
Source code in medspacy/context/context_modifier.py
379 380 381 382 383 384 385 386 387 388 389 390 391 | |
limit_scope(other)
If self and other have the same category or if other has a directionality of 'terminate', use the span of other to update the scope of self. Limiting the scope of two modifiers of the same category reduces the number of modifiers. For example, in 'no evidence of CHF, no pneumonia', 'pneumonia' will only be modified by 'no', not 'no evidence of'. 'terminate' modifiers limit the scope of a modifier like 'no evidence of' in 'no evidence of CHF, but there is pneumonia'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
ConTextModifier
|
The modifier to check against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the other modifier modified the scope of self. |
Source code in medspacy/context/context_modifier.py
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 | |
modifies(target)
Checks whether the target is within the modifier scope and if self is allowed to modify target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Span
|
a spaCy span representing a target concept. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the target is within |
Source code in medspacy/context/context_modifier.py
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 | |
modify(target)
Add target to the list of self._targets and increment self._num_targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Span
|
The spaCy span to add. |
required |
Source code in medspacy/context/context_modifier.py
321 322 323 324 325 326 327 328 329 | |
on_modifies(target)
If the ConTextRule used to define a ConTextModifier has an on_modifies callback function, evaluate and return
either True or False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Span
|
The spaCy span to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The result of the |
Source code in medspacy/context/context_modifier.py
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 | |
reduce_targets()
Reduces the number of targets to the n-closest targets based on the value of self.max_targets. If
self.max_targets is None, no pruning is done.
Source code in medspacy/context/context_modifier.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
serialized_representation()
Serialized Representation of the modifier
Source code in medspacy/context/context_modifier.py
365 366 367 368 369 370 371 372 373 374 375 376 377 | |
update_scope(span)
Changes the scope of self to be the given spaCy span.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span
|
a spaCy Span which contains the scope which a modifier should cover. |
required |
Source code in medspacy/context/context_modifier.py
193 194 195 196 197 198 199 200 201 | |