SPB Git

Repository Public

test_extract_normalize.py 7.0 KB 169 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : SPIDER — Universal Web Intelligence Engine7# File             : tests/unit/extract/test_extract_normalize.py8# Module           : Tests9# Repository       : https://git.spboucher.ai/spider10# Project Website  : Not specified11# Created          : 2026-12-0412# Last Modified    : 2026-12-0413#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development      : dev@spboucher.ai23# Support          : support@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""FR/EN value normalisation."""42import pytest4344from spider.extract.normalize import (45    Money,46    UnitValue,47    canonical_unit,48    parse_bool,49    parse_date,50    parse_money,51    parse_number,52    parse_unit_value,53)545556@pytest.mark.parametrize(("text", "expected"), [57    ("1 234,56", 1234.56), ("1,234.56", 1234.56), ("1 234", 1234), ("1 234,5", 1234.5),58    ("1 234", 1234), ("12k", 12000), ("3.5M", 3_500_000), ("0,5", 0.5), ("-3", -3),59    ("−5,5", -5.5), ("1,234,567", 1234567), ("1.250,00", 1250), ("(1 200)", -1200), ("42", 42),60])61def test_parse_number_values(text, expected):62    result = parse_number(text)63    assert result is not None64    assert result[0] == pytest.approx(expected)656667def test_parse_number_integer_type_and_percent():68    assert parse_number("1 234") == (1234, "integer+thousands_space")69    value, detail = parse_number("12,5 %")70    assert value == 12.5 and "percent" in detail717273@pytest.mark.parametrize("text", ["", "abc", "1,2,3", "12 34", "1.2.3,4,5", "12 kg", "$5", "1 234 56", None])74def test_parse_number_rejects(text):75    assert parse_number(text) is None767778def test_parse_number_ambiguous_comma_needs_hint():79    assert parse_number("1,234") is None  # English thousands or French decimal: never guessed80    assert parse_number("1,234", decimal=".") == (1234, "integer+thousands_comma")81    assert parse_number("1,234", decimal=",")[0] == pytest.approx(1.234)82    assert parse_number("1.234", decimal=",") == (1234, "integer+thousands_dot")838485@pytest.mark.parametrize(("text", "amount", "currency"), [86    ("1 250 $", 1250, None), ("$1,250.00", 1250, None), ("1.250,00 €", 1250, "EUR"), ("CA$ 12", 12, "CAD"),87    ("12 $ CA", 12, "CAD"), ("US$5", 5, "USD"), ("1250 CAD", 1250, "CAD"), ("£3.50", 3.5, "GBP"),88    ("1 250,50 $ CAD", 1250.5, "CAD"), ("EUR 5", 5, "EUR"),89])90def test_parse_money(text, amount, currency):91    money, _detail = parse_money(text)92    assert money == Money(pytest.approx(amount), currency)939495def test_parse_money_ambiguous_dollar_and_hint():96    money, detail = parse_money("1 250 $")97    assert money.currency is None and detail.startswith("ambiguous_dollar")98    money, detail = parse_money("1 250 $", currency_hint="CAD")99    assert money == Money(1250, "CAD") and detail.startswith("currency_from_hint")100    assert parse_money("1250") is None  # no marker, no hint: not money101    assert parse_money("1250", currency_hint="cad")[0] == Money(1250, "CAD")102103104def test_parse_money_period_and_rejections():105    money, detail = parse_money("1 250 $/mois")106    assert money.amount == 1250 and "period=mois" in detail107    assert parse_money("$1,250 per month")[0].amount == 1250108    assert parse_money("12 %") is None109    assert parse_money("$5 €") is None110    assert parse_money("1,250 $") is None  # suffix symbol + lone comma group: undecidable111    assert parse_money("free") is None112113114@pytest.mark.parametrize(("text", "iso"), [115    ("2026-12-03", "2026-12-03"), ("3 décembre 2026", "2026-12-03"), ("Dec 3, 2026", "2026-12-03"),116    ("December 3rd, 2026", "2026-12-03"), ("1er janvier 2026", "2026-01-01"), ("le 3 déc. 2026", "2026-12-03"),117    ("jeudi 3 décembre 2026", "2026-12-03"), ("Thursday, December 3, 2026", "2026-12-03"),118    ("3 December 2026", "2026-12-03"), ("13/04/2026", "2026-04-13"), ("04/13/2026", "2026-04-13"),119    ("2026/12/03", "2026-12-03"), ("2026-12-03T10:00:00-05:00", "2026-12-03T10:00:00-05:00"),120    ("2026-12-03T10:00:00Z", "2026-12-03T10:00:00+00:00"), ("15 août 2026", "2026-08-15"),121])122def test_parse_date(text, iso):123    assert parse_date(text)[0] == iso124125126def test_parse_date_ambiguity():127    assert parse_date("03/04/2026") == (None, "ambiguous")128    assert parse_date("03/04/2026", dayfirst=True)[0] == "2026-04-03"129    assert parse_date("03/04/2026", dayfirst=False)[0] == "2026-03-04"130    assert parse_date("03/03/2026")[0] == "2026-03-03"131132133@pytest.mark.parametrize("text", ["hello", "31/02/2026", "03/04/26", "2026-13-01", "", "32 janvier 2026", None])134def test_parse_date_rejects(text):135    assert parse_date(text) is None136137138@pytest.mark.parametrize(("text", "value", "unit"), [139    ("85 m²", 85, "m²"), ("85 m2", 85, "m²"), ("1 200 pi²", 1200, "ft²"), ("950 sq ft", 950, "ft²"),140    ("950 sq. ft.", 950, "ft²"), ("12,5 kg", 12.5, "kg"), ("3 lb", 3, "lb"), ("-5 °C", -5, "°C"),141    ("10km", 10, "km"), ("5 pieds carrés", 5, "ft²"), ("72 °F", 72, "°F"), ("1,5 L", 1.5, "L"),142])143def test_parse_unit_value(text, value, unit):144    parsed, detail = parse_unit_value(text)145    assert parsed == UnitValue(pytest.approx(value), unit)146    assert detail  # unit as written147148149def test_parse_unit_value_no_conversion_and_rejects():150    assert parse_unit_value("1 200 pi²")[0].value == 1200  # square feet stay square feet151    assert parse_unit_value("12 xyz") is None152    assert parse_unit_value("kg") is None153    assert canonical_unit("pi²") == "ft²" and canonical_unit("Mètres carrés") == "m²"154    assert canonical_unit("M") is None  # case-sensitive symbols are not folded155156157@pytest.mark.parametrize(("text", "expected"), [158    ("Oui", True), ("non", False), ("YES", True), ("No", False), ("Disponible", True), ("available", True),159    ("Sold out", False), ("Épuisé", False), ("En stock.", True), ("Rupture de stock", False),160    ("https://schema.org/InStock", True), ("http://schema.org/OutOfStock", False),161])162def test_parse_bool(text, expected):163    assert parse_bool(text)[0] is expected164165166@pytest.mark.parametrize("text", ["maybe", "", "oui et non", "1", None])167def test_parse_bool_rejects(text):168    assert parse_bool(text) is None169
Files main
Menu

Theme