TextNormalizer / rules /spaces.py
frznkshn's picture
Upload 13 files
ed76d6e verified
Raw History Blame Contribute Delete
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)