Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions semantica/normalize/number_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,11 @@ def __init__(self, **config):
"SEK",
"NOK",
"DKK",
"RUB",
"KRW",
"ILS",
"NGN",
Comment thread
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
"PKR",
]

self.logger.debug("Currency normalizer initialized")
Expand Down Expand Up @@ -606,13 +611,15 @@ def normalize_currency(
# Check for currency code
if not currency_code:
for code in self.currency_codes:
if code in currency_input.upper():
match = re.search(
rf"(?<![A-Z]){re.escape(code)}(?![A-Z])",
currency_input.upper(),
)
if match:
currency_code = code
amount_str = (
currency_input.replace(code, "")
.replace(code.lower(), "")
.strip()
)
currency_input[: match.start()] + currency_input[match.end() :]
).strip()
amount_str = amount_str.replace(",", "").replace(" ", "")
try:
amount = float(amount_str)
Expand Down
18 changes: 18 additions & 0 deletions tests/normalize/test_number_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def test_parse_currency(self):
self.assertEqual(result["amount"], 100.0)
self.assertEqual(result["currency"], "EUR")

def test_symbol_currencies_are_validated_as_supported_codes(self):
for symbol, expected_code in self.normalizer.currency_symbols.items():
result = self.normalizer.normalize_currency(f"{symbol}100")
self.assertEqual(result["currency"], expected_code)
self.assertTrue(self.normalizer.validate_currency_code(result["currency"]))

def test_currency_codes_match_boundaries_without_matching_words(self):
for value in ("RUB100", "100 RUB", "rub 100"):
result = self.normalizer.normalize_currency(value)
self.assertEqual(result["amount"], 100.0)
self.assertEqual(result["currency"], "RUB")

for value in ("ruby 100", "wilson 100"):
result = self.normalizer.normalize_currency(value)
self.assertEqual(result["amount"], 100.0)
self.assertEqual(result["currency"], "USD")


class TestScientificNotationHandler(unittest.TestCase):
def setUp(self):
self.handler = ScientificNotationHandler()
Expand Down
Loading