Tokenization¶
Text detokenization, token alignment and sentence splitting.
Detokenizer
¶
Detokenizes tokenized text into a string with aligned entity spans.
Handles language-specific detokenization rules including character-level languages (Chinese, Japanese, etc.) and various special modes like WikiANN hash replacement. Currently text in most language is detokenized by simply joining tokens by whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
The language of the tokens for selecting the appropriate detokenization strategy. |
required |
detokenizer_type
|
DetokenizerType
|
The type of detokenization to perform. Options:
- |
'whitespace'
|
Source code in meld/tokenization.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
detokenize(tokens)
¶
Detokenize a list of tokens into text with aligned spans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[str]
|
List of tokens to detokenize. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, list[Span]]
|
The detokenized text and spans for each token within it. |
Source code in meld/tokenization.py
detokenize_bio(document, original_text=None)
¶
Detokenize a BIO-formatted document. If an original_text is given, the detokenizer will simply align tokens with the given source text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
list[LabeledTokens]
|
The |
required |
original_text
|
list[str] | None
|
Optional original text for alignment. |
None
|
Returns:
| Type | Description |
|---|---|
list[LabeledText]
|
A list of |
list[LabeledText]
|
spans. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tokens cannot be aligned and fallback to whitespace tokenization fails. |
Source code in meld/tokenization.py
tokens_to_document(labeled_tokens, original_text=None)
¶
Convert labeled tokens to a NERDocument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled_tokens
|
list[LabeledTokens]
|
The list of LabeledTokens to convert. |
required |
original_text
|
list[str] | None
|
Optional original text for alignment. |
None
|
Returns:
| Type | Description |
|---|---|
NERDocument
|
A |
NERDocument
|
detokenized text with aligned entity spans. |
Source code in meld/tokenization.py
SentenceSplitter
¶
Sentence tokenizer that splits documents into sentences using either a Segment Any Text (SAT) [1] model or pre-computed sentence boundaries from Parquet files.
Requires the optional wtpsplit dependency to be installed unless read_spans is True (e.g. by installing with the "sentence-segmentation" extra enabled).
References¶
[1] Markus Frohmann, Igor Sterner, Ivan Vulić, Benjamin Minixhofer, and Markus Schedl. 2024. Segment Any Text: A Universal Approach for Robust, Efficient and Adaptable Sentence Segmentation. In Yaser Al-Onaizan, Mohit Bansal, and Yun-Nung Chen, editors, Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing, pages 11908–11941, Miami, Florida, USA, November. Association for Computational Linguistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence_boundaries
|
SentenceBoundaryType
|
Type of sentence boundaries. |
required |
sentence_span_file
|
Path | Traversable | None
|
Optional path to a Parquet file with
pre-computed sentence spans used if |
None
|
sat_model
|
str
|
Model name for |
'sat-12l-sm'
|
read_spans
|
bool
|
Whether to read from pre-computed span file instead of splitting. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When attempting to tokenize when full sentence
boundaries already exist ( |
ValueError
|
When |
ImportError
|
If the optional |
Source code in meld/tokenization.py
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 | |
sentence_tokenize(document)
¶
Tokenize the document into sentences and update its spans accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
NERDocument
|
The document to sentence-tokenize. |
required |
Returns:
| Type | Description |
|---|---|
NERDocument
|
The document with spans split into sentences. |
Source code in meld/tokenization.py
align_tokens_with_text(tokens, text)
¶
Align token spans with the original text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[str]
|
List of tokens to align. |
required |
text
|
str
|
The original text to align tokens against. |
required |
Returns:
| Type | Description |
|---|---|
list[Span]
|
List of spans of each token's position in the text. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tokens cannot be aligned with the text. |
Source code in meld/tokenization.py
bio_to_spans(bio_labels, token_spans)
¶
Convert BIO-formatted labels to annotated spans based on their position in the untokenized document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bio_labels
|
list[BIO]
|
List of BIO tags. |
required |
token_spans
|
list[Span]
|
List of token spans corresponding to the BIO labels. |
required |
Returns:
| Type | Description |
|---|---|
list[Annotation]
|
List of annotations with labels and their spans. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a BIO tag with position "B" or "I" is encountered without an entity type. |