Spaces:
Sleeping
Sleeping
Download rules/spaces.py from frznkshn/TextNormalizer: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/frznkshn/TextNormalizer/resolve/main/rules/spaces.py
- Command line
-
hf download hf://spaces/frznkshn/TextNormalizer/rules/spaces.py
-
curl -L -o spaces.py https://huggingface.co/spaces/frznkshn/TextNormalizer/resolve/main/rules/spaces.py
1.13 kB
| import random | |
| import re | |
| from rules.base import BaseProcessor | |
| from rules.currency import SYMBOLS | |
| # A number, whitespace, then a standalone currency symbol | |
| # (e.g. `1 $`, `۳ €`, `2.5 £`). A symbol followed by a digit or letter | |
| # (e.g. `1 $5`) is not a currency suffix. | |
| NUMBER_SPACE_SYMBOL_PATTERN = re.compile( | |
| rf""" | |
| (?<![\w.,٫٬]) | |
| (?P<amount>\d+(?:[.,٫٬]\d+)*) | |
| [^\S\n]+ | |
| (?P<symbol>[{SYMBOLS}]) | |
| (?![\w{SYMBOLS}]) | |
| """, | |
| re.VERBOSE, | |
| ) | |
| class CurrencySpaceProcessor(BaseProcessor): | |
| """Glues a currency symbol to the number before it: `1 $` -> `1$`. | |
| Supported symbols: `$`, `€`, `£`. | |
| This works on the whole text (not word by word) because the number and | |
| the symbol are separate words. Run it before `CurrencyProcessor`. | |
| """ | |
| def __init__(self, epsilon: float = 1.0): | |
| super(CurrencySpaceProcessor, self).__init__(epsilon=epsilon) | |
| def process(self, text: str, language: str = "en", *args, **kwargs) -> str: | |
| if random.random() > self.epsilon: | |
| return text | |
| return NUMBER_SPACE_SYMBOL_PATTERN.sub(r"\g<amount>\g<symbol>", text) |