Text processing utilities for content manipulation and formatting operations.
Provides comprehensive text processing capabilities including cleaning, filtering, splitting, loading from various sources, and formatting utilities. Supports loading text from URLs, compressed files, package resources, and local files with automatic encoding detection. Includes specialized formatting for display values and text wrapping operations for consistent presentation across the system.
EndlessTextCreator
Infinite text generator for load testing and content creation operations.
Provides deterministic text generation by cycling through preprocessed word tokens from source content. Supports filtering and punctuation handling for realistic text patterns in benchmarking scenarios.
Example: :: creator = EndlessTextCreator("path/to/source.txt") generated = creator.create_text(start=0, length=100) more_text = creator.create_text(start=50, length=200)
Source code in src/guidellm/utils/text.py
| class EndlessTextCreator:
"""
Infinite text generator for load testing and content creation operations.
Provides deterministic text generation by cycling through preprocessed word
tokens from source content. Supports filtering and punctuation handling for
realistic text patterns in benchmarking scenarios.
Example:
::
creator = EndlessTextCreator("path/to/source.txt")
generated = creator.create_text(start=0, length=100)
more_text = creator.create_text(start=50, length=200)
"""
def __init__(
self,
data: str | Path,
filter_start: str | int | None = None,
filter_end: str | int | None = None,
):
"""
Initialize text creator with source content and optional filtering.
:param data: Source text location or content - file path, URL, or raw text
:param filter_start: Starting marker or index for content filtering
:param filter_end: Ending marker or index for content filtering
"""
self.data = data
self.text = load_text(data)
self.filtered_text = filter_text(self.text, filter_start, filter_end)
self.words = split_text(self.filtered_text, split_punctuation=True)
def create_text(self, start: int, length: int) -> str:
"""
Generate text by cycling through word tokens from the specified position.
Creates deterministic text sequences by selecting consecutive tokens from
the preprocessed word list, wrapping around when reaching the end.
Maintains proper spacing and punctuation formatting.
:param start: Starting position in the token sequence
:param length: Number of tokens to include in generated text
:return: Generated text string with proper spacing and punctuation
"""
text = ""
for counter in range(length):
index = (start + counter) % len(self.words)
add_word = self.words[index]
if counter != 0 and not is_punctuation(add_word):
text += " "
text += add_word
return text
|
__init__(data, filter_start=None, filter_end=None)
Initialize text creator with source content and optional filtering.
Parameters:
| Name | Type | Description | Default |
data | str | Path | Source text location or content - file path, URL, or raw text | required |
filter_start | str | int | None | Starting marker or index for content filtering | None |
filter_end | str | int | None | Ending marker or index for content filtering | None |
Source code in src/guidellm/utils/text.py
| def __init__(
self,
data: str | Path,
filter_start: str | int | None = None,
filter_end: str | int | None = None,
):
"""
Initialize text creator with source content and optional filtering.
:param data: Source text location or content - file path, URL, or raw text
:param filter_start: Starting marker or index for content filtering
:param filter_end: Ending marker or index for content filtering
"""
self.data = data
self.text = load_text(data)
self.filtered_text = filter_text(self.text, filter_start, filter_end)
self.words = split_text(self.filtered_text, split_punctuation=True)
|
create_text(start, length)
Generate text by cycling through word tokens from the specified position.
Creates deterministic text sequences by selecting consecutive tokens from the preprocessed word list, wrapping around when reaching the end. Maintains proper spacing and punctuation formatting.
Parameters:
| Name | Type | Description | Default |
start | int | Starting position in the token sequence | required |
length | int | Number of tokens to include in generated text | required |
Returns:
| Type | Description |
str | Generated text string with proper spacing and punctuation |
Source code in src/guidellm/utils/text.py
| def create_text(self, start: int, length: int) -> str:
"""
Generate text by cycling through word tokens from the specified position.
Creates deterministic text sequences by selecting consecutive tokens from
the preprocessed word list, wrapping around when reaching the end.
Maintains proper spacing and punctuation formatting.
:param start: Starting position in the token sequence
:param length: Number of tokens to include in generated text
:return: Generated text string with proper spacing and punctuation
"""
text = ""
for counter in range(length):
index = (start + counter) % len(self.words)
add_word = self.words[index]
if counter != 0 and not is_punctuation(add_word):
text += " "
text += add_word
return text
|
clean_text(text)
Normalize text by fixing encoding issues and standardizing whitespace.
Applies Unicode normalization and whitespace standardization for consistent text processing. Removes excessive whitespace and fixes common encoding problems.
Parameters:
| Name | Type | Description | Default |
text | str | Raw text string to clean and normalize | required |
Returns:
| Type | Description |
str | Cleaned text with normalized encoding and whitespace |
Source code in src/guidellm/utils/text.py
| def clean_text(text: str) -> str:
"""
Normalize text by fixing encoding issues and standardizing whitespace.
Applies Unicode normalization and whitespace standardization for consistent
text processing. Removes excessive whitespace and fixes common encoding problems.
:param text: Raw text string to clean and normalize
:return: Cleaned text with normalized encoding and whitespace
"""
return re.sub(r"\s+", " ", ftfy.fix_text(text)).strip()
|
filter_text(text, filter_start=None, filter_end=None)
Extract text substring using start and end markers or indices.
Filters text content by locating string markers or using numeric indices to extract specific portions. Supports flexible filtering for content extraction and preprocessing operations.
Parameters:
| Name | Type | Description | Default |
text | str | Source text to filter and extract from | required |
filter_start | str | int | None | Starting marker string or index position | None |
filter_end | str | int | None | Ending marker string or index position | None |
Returns:
| Type | Description |
str | Filtered text substring between specified boundaries |
Raises:
| Type | Description |
ValueError | If filter indices are invalid or markers not found |
Source code in src/guidellm/utils/text.py
| def filter_text(
text: str,
filter_start: str | int | None = None,
filter_end: str | int | None = None,
) -> str:
"""
Extract text substring using start and end markers or indices.
Filters text content by locating string markers or using numeric indices
to extract specific portions. Supports flexible filtering for content
extraction and preprocessing operations.
:param text: Source text to filter and extract from
:param filter_start: Starting marker string or index position
:param filter_end: Ending marker string or index position
:return: Filtered text substring between specified boundaries
:raises ValueError: If filter indices are invalid or markers not found
"""
filter_start_index = -1
filter_end_index = -1
if filter_start and isinstance(filter_start, str):
filter_start_index = text.index(filter_start)
elif filter_start:
if not isinstance(filter_start, int):
raise ValueError(f"Invalid filter start index: {filter_start}")
filter_start_index = filter_start
if filter_end and isinstance(filter_end, str):
filter_end_index = text.index(filter_end)
elif filter_end:
if not isinstance(filter_end, int):
raise ValueError(f"Invalid filter end index: {filter_end}")
filter_end_index = filter_end
if filter_start_index > -1:
text = text[filter_start_index:]
if filter_end_index > -1:
text = text[:filter_end_index]
return text
|
format_value_display(value, label, units='', total_characters=None, digits_places=None, decimal_places=None)
Format a numeric value with units and label for consistent display output.
Creates standardized display strings for metrics and measurements with configurable precision, width, and color formatting. Supports both fixed-width and variable-width output for tabular displays.
Parameters:
| Name | Type | Description | Default |
value | float | Numeric value to format and display | required |
label | str | Descriptive label for the value | required |
units | str | Units string to append after the value | '' |
total_characters | int | None | Total width for right-aligned output formatting | None |
digits_places | int | None | Total number of digits for numeric formatting | None |
decimal_places | int | None | Number of decimal places for numeric precision | None |
Returns:
| Type | Description |
str | Formatted string with value, units, and colored label |
Source code in src/guidellm/utils/text.py
| def format_value_display(
value: float,
label: str,
units: str = "",
total_characters: int | None = None,
digits_places: int | None = None,
decimal_places: int | None = None,
) -> str:
"""
Format a numeric value with units and label for consistent display output.
Creates standardized display strings for metrics and measurements with
configurable precision, width, and color formatting. Supports both
fixed-width and variable-width output for tabular displays.
:param value: Numeric value to format and display
:param label: Descriptive label for the value
:param units: Units string to append after the value
:param total_characters: Total width for right-aligned output formatting
:param digits_places: Total number of digits for numeric formatting
:param decimal_places: Number of decimal places for numeric precision
:return: Formatted string with value, units, and colored label
"""
if decimal_places is None and digits_places is None:
formatted_number = f"{value:.0f}"
elif digits_places is None:
formatted_number = f"{value:.{decimal_places}f}"
elif decimal_places is None:
formatted_number = f"{value:>{digits_places}f}"
else:
formatted_number = f"{value:>{digits_places}.{decimal_places}f}"
result = f"{formatted_number}{units} [{Colors.info}]{label}[/{Colors.info}]"
if total_characters is not None:
total_characters += len(Colors.info) * 2 + 5
if len(result) < total_characters:
result = result.rjust(total_characters)
return result
|
is_punctuation(text)
Check if a single character is a punctuation mark.
Identifies punctuation characters by excluding alphanumeric characters and whitespace from single-character strings.
Parameters:
| Name | Type | Description | Default |
text | str | Single character string to test | required |
Returns:
| Type | Description |
bool | True if the character is punctuation, False otherwise |
Source code in src/guidellm/utils/text.py
| def is_punctuation(text: str) -> bool:
"""
Check if a single character is a punctuation mark.
Identifies punctuation characters by excluding alphanumeric characters
and whitespace from single-character strings.
:param text: Single character string to test
:return: True if the character is punctuation, False otherwise
"""
return len(text) == 1 and not text.isalnum() and not text.isspace()
|
load_text(data, encoding=None)
Load text content from various sources including URLs, files, and package data.
Supports loading from HTTP/FTP URLs, local files, compressed archives, package resources, and raw text strings. Automatically detects source type and applies appropriate loading strategy with encoding support.
Parameters:
| Name | Type | Description | Default |
data | str | Path | Source location or raw text - URL, file path, package resource identifier, or text content | required |
encoding | str | None | Character encoding for file reading operations | None |
Returns:
| Type | Description |
str | Loaded text content as string |
Raises:
| Type | Description |
FileNotFoundError | If local file path does not exist |
httpx.HTTPStatusError | |
Source code in src/guidellm/utils/text.py
| def load_text(data: str | Path, encoding: str | None = None) -> str:
"""
Load text content from various sources including URLs, files, and package data.
Supports loading from HTTP/FTP URLs, local files, compressed archives, package
resources, and raw text strings. Automatically detects source type and applies
appropriate loading strategy with encoding support.
:param data: Source location or raw text - URL, file path, package resource
identifier, or text content
:param encoding: Character encoding for file reading operations
:return: Loaded text content as string
:raises FileNotFoundError: If local file path does not exist
:raises httpx.HTTPStatusError: If URL request fails
"""
logger.debug("Loading text: {}", data)
if not data:
return ""
# check URLs
if isinstance(data, str) and data.strip().startswith(("http", "ftp")):
with httpx.Client() as client:
response = client.get(data.strip())
response.raise_for_status()
return response.text
# check gzipped files
if isinstance(data, str) and data.endswith(".gz"):
with gzip.open(data, "rt", encoding=encoding) as file:
return file.read()
# check if it's raw text by not being a path
if isinstance(data, str) and (
len(data) > MAX_PATH_LENGTH or not Path(data).exists()
):
return data
# assume local file
if not isinstance(data, Path):
data = Path(data)
if not data.exists() or not data.is_file():
raise FileNotFoundError(f"File not found: {data}")
return data.read_text(encoding=encoding)
|
split_text(text, split_punctuation=False)
Split text into tokens with optional punctuation separation.
Tokenizes text into words and optionally separates punctuation marks for detailed text analysis and processing operations.
Parameters:
| Name | Type | Description | Default |
text | str | Text string to tokenize and split | required |
split_punctuation | bool | Separate punctuation marks as individual tokens | False |
Returns:
| Type | Description |
list[str] | |
Source code in src/guidellm/utils/text.py
| def split_text(text: str, split_punctuation: bool = False) -> list[str]:
"""
Split text into tokens with optional punctuation separation.
Tokenizes text into words and optionally separates punctuation marks
for detailed text analysis and processing operations.
:param text: Text string to tokenize and split
:param split_punctuation: Separate punctuation marks as individual tokens
:return: List of text tokens
"""
text = clean_text(text)
if split_punctuation:
return re.findall(r"[\w]+|[.,!?;]", text)
return text.split()
|
split_text_list_by_length(text_list, max_characters, pad_horizontal=True, pad_vertical=True)
Split text strings into wrapped lines with specified maximum character limits.
Processes each string in the input list by wrapping text to fit within character limits, with optional padding for consistent formatting in tabular displays. Supports different character limits per string and uniform padding across results.
Parameters:
| Name | Type | Description | Default |
text_list | list[Any] | List of strings to process and wrap | required |
max_characters | int | list[int] | Maximum characters per line, either single value or per-string limits | required |
pad_horizontal | bool | Right-align lines within their character limits | True |
pad_vertical | bool | Pad shorter results to match the longest wrapped result | True |
Returns:
| Type | Description |
list[list[str]] | List of wrapped line lists, one per input string |
Raises:
| Type | Description |
ValueError | If max_characters list length doesn't match text_list length |
Source code in src/guidellm/utils/text.py
| def split_text_list_by_length(
text_list: list[Any],
max_characters: int | list[int],
pad_horizontal: bool = True,
pad_vertical: bool = True,
) -> list[list[str]]:
"""
Split text strings into wrapped lines with specified maximum character limits.
Processes each string in the input list by wrapping text to fit within character
limits, with optional padding for consistent formatting in tabular displays.
Supports different character limits per string and uniform padding across results.
:param text_list: List of strings to process and wrap
:param max_characters: Maximum characters per line, either single value or
per-string limits
:param pad_horizontal: Right-align lines within their character limits
:param pad_vertical: Pad shorter results to match the longest wrapped result
:return: List of wrapped line lists, one per input string
:raises ValueError: If max_characters list length doesn't match text_list length
"""
if not isinstance(max_characters, list):
max_characters = [max_characters] * len(text_list)
if len(max_characters) != len(text_list):
raise ValueError(
f"max_characters must be a list of the same length as text_list, "
f"but got {len(max_characters)} and {len(text_list)}"
)
result: list[list[str]] = []
for index, text in enumerate(text_list):
lines = textwrap.wrap(text, max_characters[index])
result.append(lines)
if pad_vertical:
max_lines = max(len(lines) for lines in result)
for lines in result:
while len(lines) < max_lines:
lines.append(" ")
if pad_horizontal:
for index in range(len(result)):
lines = result[index]
max_chars = max_characters[index]
new_lines = []
for line in lines:
new_lines.append(line.rjust(max_chars))
result[index] = new_lines
return result
|