Tokenizers
Tokenization utilities for text.
Provides methods to split text into regex-based or Unicode-aware tokens.
Tokenization is used for alignment in resolver.py and for determining
sentence boundaries for smaller context use cases. This module is not used
for tokenization within the language model during inference.
BaseTokenizerError
Bases: BaseException
Base class for all tokenizer-related errors.
InvalidTokenIntervalError
Bases: BaseTokenizerError
Error raised when a token interval is invalid or out of range.
SentenceRangeError
Bases: BaseTokenizerError
Error raised when the start token index for a sentence is out of range.
CharInterval(start_pos, end_pos)
dataclass
Represents a range of character positions in the original text.
Attributes:
| Name | Type | Description |
|---|---|---|
start_pos |
int
|
The starting character index (inclusive). |
end_pos |
int
|
The ending character index (exclusive). |
TokenInterval(start_index=0, end_index=0)
dataclass
Represents an interval over tokens in tokenized text.
The interval is defined by a start index (inclusive) and an end index (exclusive).
Attributes:
| Name | Type | Description |
|---|---|---|
start_index |
int
|
The index of the first token in the interval. |
end_index |
int
|
The index one past the last token in the interval. |
TokenType
Bases: IntEnum
Enumeration of token types produced during tokenization.
Attributes:
| Name | Type | Description |
|---|---|---|
WORD |
Represents an alphabetical word token. |
|
NUMBER |
Represents a numeric token. |
|
PUNCTUATION |
Represents punctuation characters. |
Token(index, token_type, char_interval=(lambda: CharInterval(0, 0))(), first_token_after_newline=False)
dataclass
Represents a token extracted from text.
Each token is assigned an index and classified into a type (word, number, punctuation, or acronym). The token also records the range of characters (its CharInterval) that correspond to the substring from the original text. Additionally, it tracks whether it follows a newline.
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
The position of the token in the sequence of tokens. |
token_type |
TokenType
|
The type of the token, as defined by TokenType. |
char_interval |
CharInterval
|
The character interval within the original text that this token spans. |
first_token_after_newline |
bool
|
True if the token immediately follows a newline or carriage return. |
TokenizedText(text, tokens=list())
dataclass
Holds the result of tokenizing a text string.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The text that was tokenized. For UnicodeTokenizer, this is NOT normalized to NFC (to preserve indices). |
tokens |
list[Token]
|
A list of Token objects extracted from the text. |
Tokenizer
Bases: ABC
Abstract base class for tokenizers.
tokenize(text)
abstractmethod
Splits text into tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to tokenize. |
required |
Returns:
| Type | Description |
|---|---|
TokenizedText
|
A TokenizedText object. |
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
163 164 165 166 167 168 169 170 171 172 | |
RegexTokenizer
Bases: Tokenizer
Regex-based tokenizer (default).
The RegexTokenizer is faster than UnicodeTokenizer for English text because it skips involved Unicode handling.
tokenize(text)
Splits text into tokens (words, digits, or punctuation).
Each token is annotated with its character position and type. Tokens
following a newline or carriage return have first_token_after_newline
set to True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to tokenize. |
required |
Returns:
| Type | Description |
|---|---|
TokenizedText
|
A TokenizedText object containing all extracted tokens. |
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
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 | |
Sentinel(name)
Sentinel class for unique object identification.
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
249 250 | |
UnicodeTokenizer
Bases: Tokenizer
Unicode-aware tokenizer for better non-English support.
This tokenizer uses Unicode character properties (Unicode Standard Annex #29)
via the regex library's \X pattern to correctly handle grapheme clusters
like Emojis and Hangul.
Unlike some Unicode tokenizers, this class does NOT normalize text to NFC. This ensures that token indices exactly match the original input string.
Note: Grapheme clustering makes this tokenizer slower than RegexTokenizer.
tokenize(text)
Splits text into tokens using Unicode properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to tokenize. |
required |
Returns:
| Type | Description |
|---|---|
TokenizedText
|
A TokenizedText object. |
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
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 | |
tokenize(text, tokenizer=_DEFAULT_TOKENIZER)
Splits text into tokens using the provided tokenizer (default: RegexTokenizer).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to tokenize. |
required |
tokenizer
|
Tokenizer
|
The tokenizer instance to use. |
_DEFAULT_TOKENIZER
|
Returns:
| Type | Description |
|---|---|
TokenizedText
|
A TokenizedText object. |
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
229 230 231 232 233 234 235 236 237 238 239 | |
tokens_text(tokenized_text, token_interval)
Reconstructs the substring of the original text spanning a given token interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenized_text
|
TokenizedText
|
A TokenizedText object containing token data. |
required |
token_interval
|
TokenInterval
|
The interval specifying the range [start_index, end_index) of tokens. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The exact substring of the original text corresponding to the token |
str
|
interval. |
Raises:
| Type | Description |
|---|---|
InvalidTokenIntervalError
|
If the token_interval is invalid or out of range. |
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
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 | |
find_sentence_range(text, tokens, start_token_index, known_abbreviations=_KNOWN_ABBREVIATIONS)
Finds a 'sentence' interval from a given start index.
Sentence boundaries are defined by
- punctuation tokens in _END_OF_SENTENCE_PATTERN
- newline breaks followed by an uppercase letter
- not abbreviations in _KNOWN_ABBREVIATIONS (e.g., "Dr.")
This favors terminating a sentence prematurely over missing a sentence boundary, and will terminate a sentence early if the first line ends with new line and the second line begins with a capital letter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to analyze. |
required |
tokens
|
Sequence[Token]
|
The tokens that make up |
required |
start_token_index
|
int
|
The index of the token to start the sentence from. |
required |
known_abbreviations
|
Set[str]
|
A set of strings that are known abbreviations and should not be treated as sentence boundaries. |
_KNOWN_ABBREVIATIONS
|
Returns:
| Type | Description |
|---|---|
TokenInterval
|
A TokenInterval representing the sentence range [start_token_index, end). If |
TokenInterval
|
no sentence boundary is found, the end index will be the length of |
TokenInterval
|
|
Raises:
| Type | Description |
|---|---|
SentenceRangeError
|
If |
Source code in src/kibad_llm/extractors/chunking_utils/tokenizers.py
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 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |