Spaces:
Sleeping
Sleeping
feat: option "retirer les symboles" (garde lettres et chiffres)
Nouvelle option opt-in remove_symbols : supprime tous les symboles (EUR, %, #, *,
(R), etc.) de toutes les cellules en gardant lettres (accents compris), chiffres,
espaces et separateurs decimaux. Ex. "12,50 EUR" -> "12,50", "Ref#12" -> "Ref12".
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
- app.py +2 -0
- core/cleaner.py +28 -0
- static/index.html +9 -1
- tests/test_cleaner.py +8 -1
app.py
CHANGED
|
@@ -53,6 +53,7 @@ async def clean(
|
|
| 53 |
drop_duplicates: bool = Form(True),
|
| 54 |
trim_whitespace: bool = Form(True),
|
| 55 |
normalize_headers: bool = Form(True),
|
|
|
|
| 56 |
keep_digits_only: bool = Form(False),
|
| 57 |
decimal_comma: bool = Form(False),
|
| 58 |
ebp_format: bool = Form(False),
|
|
@@ -82,6 +83,7 @@ async def clean(
|
|
| 82 |
"drop_duplicates": drop_duplicates,
|
| 83 |
"trim_whitespace": trim_whitespace,
|
| 84 |
"normalize_headers": normalize_headers,
|
|
|
|
| 85 |
"keep_digits_only": keep_digits_only,
|
| 86 |
"decimal_comma": decimal_comma,
|
| 87 |
"ebp_format": ebp_format,
|
|
|
|
| 53 |
drop_duplicates: bool = Form(True),
|
| 54 |
trim_whitespace: bool = Form(True),
|
| 55 |
normalize_headers: bool = Form(True),
|
| 56 |
+
remove_symbols: bool = Form(False),
|
| 57 |
keep_digits_only: bool = Form(False),
|
| 58 |
decimal_comma: bool = Form(False),
|
| 59 |
ebp_format: bool = Form(False),
|
|
|
|
| 83 |
"drop_duplicates": drop_duplicates,
|
| 84 |
"trim_whitespace": trim_whitespace,
|
| 85 |
"normalize_headers": normalize_headers,
|
| 86 |
+
"remove_symbols": remove_symbols,
|
| 87 |
"keep_digits_only": keep_digits_only,
|
| 88 |
"decimal_comma": decimal_comma,
|
| 89 |
"ebp_format": ebp_format,
|
core/cleaner.py
CHANGED
|
@@ -82,6 +82,17 @@ def _looks_numeric(value: str) -> bool:
|
|
| 82 |
return bool(_NUM_RE.match(value)) and any(c.isdigit() for c in value)
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def _strip_to_digits(value: str) -> str:
|
| 86 |
"""
|
| 87 |
Retire lettres, symboles et espaces, ne garde que les chiffres et les
|
|
@@ -199,6 +210,23 @@ def clean_csv(raw: bytes, options: dict) -> tuple[bytes, dict]:
|
|
| 199 |
if removed:
|
| 200 |
log(f"{removed} ligne(s) en double supprimée(s).")
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
# 4b. Garder uniquement les chiffres (retire lettres/symboles des montants)
|
| 203 |
if options.get("keep_digits_only"):
|
| 204 |
stripped = 0
|
|
|
|
| 82 |
return bool(_NUM_RE.match(value)) and any(c.isdigit() for c in value)
|
| 83 |
|
| 84 |
|
| 85 |
+
# Tout sauf lettres (accents compris), chiffres, espaces et séparateurs , . -
|
| 86 |
+
_SYMBOL_RE = re.compile(r"[^\w\s.,\-]", re.UNICODE)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def _remove_symbols(value: str) -> str:
|
| 90 |
+
"""Retire les symboles (€, %, #, *, ®, etc.) en gardant lettres, chiffres,
|
| 91 |
+
espaces et séparateurs décimaux. Ex. : "12,50 €" → "12,50", "Réf#12" → "Réf12"."""
|
| 92 |
+
new = _SYMBOL_RE.sub("", value)
|
| 93 |
+
return re.sub(r"\s+", " ", new).strip()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
def _strip_to_digits(value: str) -> str:
|
| 97 |
"""
|
| 98 |
Retire lettres, symboles et espaces, ne garde que les chiffres et les
|
|
|
|
| 210 |
if removed:
|
| 211 |
log(f"{removed} ligne(s) en double supprimée(s).")
|
| 212 |
|
| 213 |
+
# 4a. Retirer tous les symboles (€, %, #, *…) en gardant lettres et chiffres
|
| 214 |
+
if options.get("remove_symbols"):
|
| 215 |
+
cleaned_cells = 0
|
| 216 |
+
|
| 217 |
+
def desymbol(x):
|
| 218 |
+
nonlocal cleaned_cells
|
| 219 |
+
if isinstance(x, str):
|
| 220 |
+
new = _remove_symbols(x)
|
| 221 |
+
if new != x:
|
| 222 |
+
cleaned_cells += 1
|
| 223 |
+
return new
|
| 224 |
+
return x
|
| 225 |
+
|
| 226 |
+
df = df.apply(lambda col: col.map(desymbol))
|
| 227 |
+
if cleaned_cells:
|
| 228 |
+
log(f"{cleaned_cells} cellule(s) : symboles retirés.")
|
| 229 |
+
|
| 230 |
# 4b. Garder uniquement les chiffres (retire lettres/symboles des montants)
|
| 231 |
if options.get("keep_digits_only"):
|
| 232 |
stripped = 0
|
static/index.html
CHANGED
|
@@ -177,6 +177,14 @@
|
|
| 177 |
</span>
|
| 178 |
</label>
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
<label class="opt">
|
| 181 |
<input type="checkbox" name="keep_digits_only" />
|
| 182 |
<span class="opt-text">
|
|
@@ -268,7 +276,7 @@
|
|
| 268 |
const base = window.location.origin;
|
| 269 |
const fd = new FormData();
|
| 270 |
fd.append('csv_file', csvInput.files[0]);
|
| 271 |
-
['drop_empty','drop_duplicates','trim_whitespace','normalize_headers','keep_digits_only','decimal_comma','ebp_format']
|
| 272 |
.forEach(name => {
|
| 273 |
const el = form.querySelector(`[name="${name}"]`);
|
| 274 |
fd.append(name, el.checked ? 'true' : 'false');
|
|
|
|
| 177 |
</span>
|
| 178 |
</label>
|
| 179 |
|
| 180 |
+
<label class="opt">
|
| 181 |
+
<input type="checkbox" name="remove_symbols" />
|
| 182 |
+
<span class="opt-text">
|
| 183 |
+
<span class="opt-title">Retirer les symboles</span>
|
| 184 |
+
<span class="opt-desc">Supprime €, %, #, *, ®… en gardant lettres et chiffres (12,50 € → 12,50, Réf#12 → Réf12).</span>
|
| 185 |
+
</span>
|
| 186 |
+
</label>
|
| 187 |
+
|
| 188 |
<label class="opt">
|
| 189 |
<input type="checkbox" name="keep_digits_only" />
|
| 190 |
<span class="opt-text">
|
|
|
|
| 276 |
const base = window.location.origin;
|
| 277 |
const fd = new FormData();
|
| 278 |
fd.append('csv_file', csvInput.files[0]);
|
| 279 |
+
['drop_empty','drop_duplicates','trim_whitespace','normalize_headers','remove_symbols','keep_digits_only','decimal_comma','ebp_format']
|
| 280 |
.forEach(name => {
|
| 281 |
const el = form.querySelector(`[name="${name}"]`);
|
| 282 |
fd.append(name, el.checked ? 'true' : 'false');
|
tests/test_cleaner.py
CHANGED
|
@@ -4,7 +4,7 @@ from core.cleaner import clean_csv, detect_delimiter, detect_encoding
|
|
| 4 |
|
| 5 |
def _opts(**over):
|
| 6 |
base = dict(drop_empty=False, drop_duplicates=False, trim_whitespace=False,
|
| 7 |
-
normalize_headers=False, keep_digits_only=False,
|
| 8 |
decimal_comma=False, ebp_format=False)
|
| 9 |
base.update(over)
|
| 10 |
return base
|
|
@@ -76,6 +76,13 @@ def test_ebp_format_output():
|
|
| 76 |
assert b"\r\n" in out
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def test_keep_digits_only_strips_letters_symbols():
|
| 80 |
raw = "p\n12,50€\nA123\n10 ml\n€99.90\n".encode("utf-8")
|
| 81 |
out, _ = clean_csv(raw, _opts(keep_digits_only=True))
|
|
|
|
| 4 |
|
| 5 |
def _opts(**over):
|
| 6 |
base = dict(drop_empty=False, drop_duplicates=False, trim_whitespace=False,
|
| 7 |
+
normalize_headers=False, remove_symbols=False, keep_digits_only=False,
|
| 8 |
decimal_comma=False, ebp_format=False)
|
| 9 |
base.update(over)
|
| 10 |
return base
|
|
|
|
| 76 |
assert b"\r\n" in out
|
| 77 |
|
| 78 |
|
| 79 |
+
def test_remove_symbols_keeps_letters_and_digits():
|
| 80 |
+
raw = "ref;prix\nRéf#12;12,50 €\nA*B;99 %\n".encode("utf-8")
|
| 81 |
+
out, _ = clean_csv(raw, _opts(remove_symbols=True))
|
| 82 |
+
body = out.decode("utf-8-sig").splitlines()[1:]
|
| 83 |
+
assert body == ["Réf12;12,50", "AB;99"]
|
| 84 |
+
|
| 85 |
+
|
| 86 |
def test_keep_digits_only_strips_letters_symbols():
|
| 87 |
raw = "p\n12,50€\nA123\n10 ml\n€99.90\n".encode("utf-8")
|
| 88 |
out, _ = clean_csv(raw, _opts(keep_digits_only=True))
|