medspacy.context.context
The ConText definiton.
ConText
The ConText for spaCy processing.
This component matches modifiers in a Doc, defines their scope, and identifies edges between targets and modifiers. Sets two spaCy extensions: - Span..modifiers: a list of ConTextModifier objects which modify a target Span - Doc..context_graph: a ConText graph object which contains the targets, modifiers, and edges between them.
Source code in medspacy/context/context.py
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 | |
categories
property
Returns list of categories available that Context might produce.
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
Returns list of ConTextRules available to context.
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, targets=None)
Applies the ConText algorithm to a Doc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
The spaCy Doc to process. |
required | |
targets
|
str
|
The optional custom attribute extension on doc to run over. Must contain an iterable of Span objects |
None
|
Returns:
| Type | Description |
|---|---|
Doc
|
The processed spaCy Doc. |
Source code in medspacy/context/context.py
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 | |
__init__(nlp, name='medspacy_context', rules='default', language_code='en', phrase_matcher_attr='LOWER', allowed_types=None, excluded_types=None, terminating_types=None, max_scope=None, max_targets=None, prune_on_modifier_overlap=True, prune_on_target_overlap=False, span_attrs='default', input_span_type='ents', span_group_name='medspacy_spans')
Creates a new ConText object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nlp
|
Language
|
A SpaCy Language object. |
required |
name
|
str
|
The name of the component. |
'medspacy_context'
|
rules
|
Optional[str]
|
The rules to load. Default is "default", loads rules packaged with medspaCy that are derived from
original ConText rules and years of practical applications 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 ConTextRules
directly through |
'default'
|
language_code
|
str
|
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. |
'en'
|
phrase_matcher_attr
|
str
|
The token attribute to use for PhraseMatcher for rules where |
'LOWER'
|
allowed_types
|
Optional[Set[str]]
|
A global list of types included by context. Rules will operate on only spans with these labels. |
None
|
excluded_types
|
Optional[Set[str]]
|
A global list of types excluded by context. Rules will not operate on spans with these labels. |
None
|
terminating_types
|
Optional[Dict[str, Iterable[str]]]
|
A global map of types to the types that can terminate them. This can be used to apply terminations to all rules of a particular type rather than adding to every rule individually in the ContextRule object. |
None
|
max_scope
|
Optional[int]
|
The number of tokens around a modifier in a target can be modified. Default value is None, Context will use the sentence boundaries. If a value greater than zero, applies the window globally. Both options will be overridden by a more specific value in a ContextRule. |
None
|
max_targets
|
Optional[int]
|
The maximum number of targets a modifier can modify. Default value is None, context will modify all targets in its scope. If a value greater than zero, applies this value globally. Both options will be overridden by a more specific value in a ContextRule. |
None
|
prune_on_modifier_overlap
|
bool
|
Whether to prune modifiers which are substrings of another modifier. If True, will drop substrings completely. For example, if "no history of" and "history of" are both ConTextRules,both will match the text "no history of afib", but only "no history of" should modify afib. Default True. |
True
|
prune_on_target_overlap
|
bool
|
Whether to remove any matched modifiers which overlap with target entities. If False, any overlapping modifiers will not modify the overlapping entity but will still modify any other targets in its scope. Default False. |
False
|
span_attrs
|
Union[Literal['default'], Dict[str, Dict[str, Any]], None]
|
The optional span attributes to modify. Default option "default" uses attributes in
|
'default'
|
input_span_type
|
Union[Literal['ents', 'group']]
|
"ents" or "group". Where to look for targets. "ents" will modify attributes of spans
in doc.ents. "group" will modify attributes of spans in the span group specified by |
'ents'
|
span_group_name
|
str
|
The name of the span group used when |
'medspacy_spans'
|
Source code in medspacy/context/context.py
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 | |
add(rules)
Adds ConTextRules to Context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
A single ConTextRule or a collection of ConTextRules to add to the Sectionizer. |
required |
Source code in medspacy/context/context.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 239 240 241 242 243 | |
register_default_attributes()
classmethod
Registers the default values for the Span attributes defined in DEFAULT_ATTRIBUTES.
Source code in medspacy/context/context.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
register_graph_attributes()
classmethod
Registers spaCy attribute extensions: Span..modifiers and Doc..context_graph.
Source code in medspacy/context/context.py
245 246 247 248 249 250 251 252 253 254 | |
set_context_attributes(edges)
Adds Span-level attributes to targets with modifiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
The edges of the ContextGraph to modify. |
required |
Source code in medspacy/context/context.py
273 274 275 276 277 278 279 280 281 282 283 284 | |