Spaces:
Sleeping
Sleeping
Update model_utils.py
Browse files- model_utils.py +18 -13
model_utils.py
CHANGED
|
@@ -25,24 +25,29 @@ def parse_products(raw_text):
|
|
| 25 |
lines = raw_text.split('\n')
|
| 26 |
|
| 27 |
for line in lines:
|
| 28 |
-
|
| 29 |
-
if not line:
|
| 30 |
-
continue
|
| 31 |
-
match = re.search(r"^(.*?)(\d+)\s+[^A-Za-z0-9\s]?([\d.,]+)\s+[^A-Za-z0-9\s]?([\d.,]+)$", line)
|
| 32 |
if match:
|
| 33 |
-
|
| 34 |
structured.append({
|
| 35 |
-
"qty":
|
| 36 |
-
"description": desc.strip(
|
| 37 |
-
"unit_price":
|
| 38 |
-
"amount":
|
| 39 |
})
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
structured.append({
|
| 42 |
"qty": 0,
|
| 43 |
-
"description": line,
|
| 44 |
-
"unit_price": 0
|
| 45 |
-
"amount": 0
|
| 46 |
})
|
| 47 |
|
| 48 |
return structured
|
|
|
|
| 25 |
lines = raw_text.split('\n')
|
| 26 |
|
| 27 |
for line in lines:
|
| 28 |
+
match = re.match(r"(\d+)\s+(.*)\s+([\d,]+\.\d{2})\s+([\d,]+\.\d{2})", line)
|
|
|
|
|
|
|
|
|
|
| 29 |
if match:
|
| 30 |
+
qty, desc, unit_price, amount = match.groups()
|
| 31 |
structured.append({
|
| 32 |
+
"qty": qty,
|
| 33 |
+
"description": desc.strip(),
|
| 34 |
+
"unit_price": unit_price,
|
| 35 |
+
"amount": amount
|
| 36 |
})
|
| 37 |
+
match_description_mixed=re.search(r"^(.*?)(\d+)\s+[^A-Za-z0-9\s]?([\d.,]+)\s+[^A-Za-z0-9\s]?([\d.,]+)$", desc.strip())
|
| 38 |
+
elif match_description_mixed:
|
| 39 |
+
structured.append({
|
| 40 |
+
"qty":int(match.group(2)),
|
| 41 |
+
"description": match_description_mixed.group(1).strip(" .:-"),
|
| 42 |
+
"unit_price": float(match.group(3).replace(",", "")),
|
| 43 |
+
"amount": float(match.group(4).replace(",", ""))
|
| 44 |
+
})
|
| 45 |
+
elif line.strip():
|
| 46 |
structured.append({
|
| 47 |
"qty": 0,
|
| 48 |
+
"description": line.strip(),
|
| 49 |
+
"unit_price": 0,
|
| 50 |
+
"amount": 0
|
| 51 |
})
|
| 52 |
|
| 53 |
return structured
|