Spaces:
Sleeping
Sleeping
Download utils/text_normalizer.py from ibrahim7004/audiobook-generator: direct link, hf CLI and curl.
- Browser
- Download file 824 Bytes
-
https://huggingface.co/spaces/ibrahim7004/audiobook-generator/resolve/main/utils/text_normalizer.py
- Command line
-
hf download hf://spaces/ibrahim7004/audiobook-generator/utils/text_normalizer.py
-
curl -L -o text_normalizer.py https://huggingface.co/spaces/ibrahim7004/audiobook-generator/resolve/main/utils/text_normalizer.py
824 Bytes
| import os | |
| _QUOTE_NORMALIZER = [ | |
| ("\u201c", '"'), ("\u201d", '"'), ("\u201e", '"'), ("\u201f", '"'), | |
| ("\u2033", '"'), ("\u2036", '"'), | |
| ("\u2018", "'"), ("\u2019", "'"), ("\u201a", "'"), ("\u201b", "'"), | |
| ("\u2013", "-"), ("\u2014", "-"), | |
| ("\u2026", "..."), | |
| ] | |
| DEBUG = os.getenv("DEBUG_NORMALIZER", "false").lower() == "true" | |
| def normalize_text(text: str) -> str: | |
| if DEBUG: | |
| print("[DEBUG] normalize_text called") | |
| # Replace curly quotes, dashes, ellipses | |
| for bad, good in _QUOTE_NORMALIZER: | |
| text = text.replace(bad, good) | |
| # Final safeguard: strip unsupported characters | |
| if DEBUG: | |
| print("[DEBUG] Final Normalized Text: ", | |
| text.encode("utf-8", "ignore").decode("utf-8", "ignore")) | |
| return text.encode("utf-8", "ignore").decode("utf-8", "ignore") | |