File size: 1,130 Bytes
ed76d6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)