Token Milestones¤
Class for handling token milestones.
TokenMilestones
pydantic-model
¤
Bases: BaseModel
Milestones class.
- Referencing the Milestones instance yields an iterator of the spans in the Doc.
- Referencing Milestones.spans returns an indexed list of spans in the Doc.
- Referencing milestones.doc.spans["milestones"] returns a SpanGroup.
Config:
default:validation_config
Fields:
-
doc(Doclike) -
patterns(Optional[Any]) -
case_sensitive(Optional[bool]) -
character_map(Optional[dict]) -
attr(Optional[str]) -
flags(Optional[Enum]) -
mode(Optional[str]) -
nlp(Optional[str]) -
type(Optional[str])
Source code in lexos/milestones/token_milestones.py
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 | |
spans: list[Span]
property
¤
Return the Spans.
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of spaCy Spans. |
__init__(**data) -> None
¤
Set regex flags and milestone IOB extensions after initialization.
Source code in lexos/milestones/token_milestones.py
__iter__() -> Iterator
¤
Make the class iterable.
Returns:
| Name | Type | Description |
|---|---|---|
Iterator |
Iterator
|
A generator containing the object's spans. |
get_matches(patterns: Optional[Any] = None, mode: Optional[str] = None, case_sensitive: Optional[bool] = None) -> list[Span]
¤
Get matches to milestone patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Optional[Any]
|
The pattern(s) to match. |
None
|
mode
|
Optional[str]
|
The mode to use for matching ('string', 'phrase', 'rule'). |
None
|
case_sensitive
|
Optional[bool]
|
Whether to use case sensitive matching. Defaults to True. |
None
|
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of spaCy Spans matching the pattern. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If patterns is None or empty. |
Source code in lexos/milestones/token_milestones.py
remove(patterns: Any, *, mode: Optional[str] = 'string') -> None
¤
Remove patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Any
|
The pattern(s) to match. |
required |
mode
|
Optional[str]
|
The mode to use for matching. |
'string'
|
Source code in lexos/milestones/token_milestones.py
reset()
¤
Reset all milestone values to defaults.
Note: Does not modify patterns or any other settings.
Source code in lexos/milestones/token_milestones.py
set_milestones(spans: list[Span], *, start: Optional[str | None] = None, remove: Optional[bool] = False, max_label_length: Optional[int] = 20) -> None
¤
Commit milestones to the object instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
list[Span]
|
The span(s) to use for identifying token attributes. |
required |
start
|
Optional[str | None]
|
Set milestone start to the token before or after the milestone span. May be "before" or "after". |
None
|
remove
|
Optional[bool]
|
Set milestone start to the token following the milestone span and remove the milestone span tokens from the Doc. |
False
|
max_label_length
|
Optional[int]
|
The maximum number of characters to include in the label. |
20
|
Source code in lexos/milestones/token_milestones.py
to_list(*, strip_punct: Optional[bool] = True) -> list[dict]
¤
Get a list of milestone dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strip_punct
|
Optional[bool]
|
Strip single punctation mark at the end of the character string. |
True
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: A list of milestone dicts. |
Note
Some language models include a final punctuation mark in the token string, particularly at the end of a sentence. The strip_punct argument is a somewhat hacky convenience method to remove it. However, the user may wish instead to do some post-processing in order to use the output for their own purposes.
Source code in lexos/milestones/token_milestones.py
__init__(**data) -> None
¤
Set regex flags and milestone IOB extensions after initialization.
Source code in lexos/milestones/token_milestones.py
__iter__() -> Iterator
¤
Make the class iterable.
Returns:
| Name | Type | Description |
|---|---|---|
Iterator |
Iterator
|
A generator containing the object's spans. |
spans: list[Span]
property
¤
Return the Spans.
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of spaCy Spans. |
_assign_token_attributes(spans: list[Span], max_label_length: int = 20) -> None
¤
Assign token attributes in the doc based on spans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
list[Span]
|
A list of spaCy Spans. |
required |
max_label_length
|
int
|
The maximum number of characters to include in the label. |
20
|
Source code in lexos/milestones/token_milestones.py
_autodetect_mode(patterns: str | list) -> str
¤
Autodetect mode for matching milestones if not supplied (experimental).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
str | list
|
A pattern to match. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string to supply to the get_matches() mode argument. |
Source code in lexos/milestones/token_milestones.py
_get_string_matches(patterns: Any, flags: Enum) -> list[Span]
¤
Get matches to milestone patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Any
|
A pattern to match. |
required |
flags
|
Enum
|
An enum of regex flags. |
required |
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of Spans matching the pattern. |
Source code in lexos/milestones/token_milestones.py
_get_phrase_matches(patterns: Any, attr: str = 'ORTH') -> list[Span]
¤
Get matches to milestone patterns in phrases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Any
|
A pattern to match. |
required |
attr
|
str
|
A spaCy Token attribute to search. |
'ORTH'
|
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of Spans matching the pattern. |
Source code in lexos/milestones/token_milestones.py
_get_rule_matches(patterns: Any) -> list[Span]
¤
Get matches to milestone patterns with spaCy rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Any
|
A pattern to match. |
required |
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of Spans matching the pattern. |
Source code in lexos/milestones/token_milestones.py
_remove_duplicate_spans(spans: list[Span]) -> list[Span]
¤
Remove duplicate spans, generally created when a pattern is added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
list[Span]
|
A list of Spans. |
required |
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of de-duplicated Spans. |
Source code in lexos/milestones/token_milestones.py
_set_case_sensitivity(case_sensitive: Optional[bool] = None) -> None
¤
Set the object's case sensitivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
case_sensitive
|
(optional, bool)
|
Whether or not to use case-sensitive searching. |
None
|
Source code in lexos/milestones/token_milestones.py
_to_spacy_span(match: Match) -> Span
¤
Convert a re.match object to a Span.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
match
|
Match
|
A re.match object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Span |
Span
|
A spaCy Span. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If match is None or span cannot be created. |
Source code in lexos/milestones/token_milestones.py
get_matches(patterns: Optional[Any] = None, mode: Optional[str] = None, case_sensitive: Optional[bool] = None) -> list[Span]
¤
Get matches to milestone patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Optional[Any]
|
The pattern(s) to match. |
None
|
mode
|
Optional[str]
|
The mode to use for matching ('string', 'phrase', 'rule'). |
None
|
case_sensitive
|
Optional[bool]
|
Whether to use case sensitive matching. Defaults to True. |
None
|
Returns:
| Type | Description |
|---|---|
list[Span]
|
list[Span]: A list of spaCy Spans matching the pattern. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If patterns is None or empty. |
Source code in lexos/milestones/token_milestones.py
remove(patterns: Any, *, mode: Optional[str] = 'string') -> None
¤
Remove patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
Any
|
The pattern(s) to match. |
required |
mode
|
Optional[str]
|
The mode to use for matching. |
'string'
|
Source code in lexos/milestones/token_milestones.py
reset()
¤
Reset all milestone values to defaults.
Note: Does not modify patterns or any other settings.
Source code in lexos/milestones/token_milestones.py
set_milestones(spans: list[Span], *, start: Optional[str | None] = None, remove: Optional[bool] = False, max_label_length: Optional[int] = 20) -> None
¤
Commit milestones to the object instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
list[Span]
|
The span(s) to use for identifying token attributes. |
required |
start
|
Optional[str | None]
|
Set milestone start to the token before or after the milestone span. May be "before" or "after". |
None
|
remove
|
Optional[bool]
|
Set milestone start to the token following the milestone span and remove the milestone span tokens from the Doc. |
False
|
max_label_length
|
Optional[int]
|
The maximum number of characters to include in the label. |
20
|
Source code in lexos/milestones/token_milestones.py
to_list(*, strip_punct: Optional[bool] = True) -> list[dict]
¤
Get a list of milestone dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strip_punct
|
Optional[bool]
|
Strip single punctation mark at the end of the character string. |
True
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: A list of milestone dicts. |
Note
Some language models include a final punctuation mark in the token string, particularly at the end of a sentence. The strip_punct argument is a somewhat hacky convenience method to remove it. However, the user may wish instead to do some post-processing in order to use the output for their own purposes.