The normalize_text function is the primary entry point for preparing English text for TTS. It performs a comprehensive suite of substitutions including URLs, emails, dates, times, currency, percentages, ordinals, abbreviations, and model versions.
Arguments:
text (str): The input text.locale (str): Currently only "en-US" is supported.return_spans (bool): If True, returns a NormalizedTextResult object containing metadata about what was changed.
Returns:
- If
return_spans=False: A str of the normalized text. - If
return_spans=True: A NormalizedTextResult object.
Example of NormalizedTextResult:
When return_spans=True, you receive an object with:
text: The normalized string.spans: A list of NormalizedSpan objects mapping original character indices to normalized indices, including the reason for the change (e.g., "url", "date", "currency").
from kittentts.preprocess import normalize_text
# Simple usage
text = "Contact me at test@example.com or visit https://google.com"
print(normalize_text(text))
# "Contact me at test at example dot com or visit www dot google dot com"
# Usage with span metadata
result = normalize_text(text, return_spans=True)
for span in result.spans:
print(f"Reason: {span.reason}, Original: {span.originalStartChar}-{span.originalEndChar}")