Namra-Satva commited on
Commit
b8d1763
·
verified ·
1 Parent(s): b4eb289

Update model_utils.py

Browse files
Files changed (1) hide show
  1. model_utils.py +17 -11
model_utils.py CHANGED
@@ -25,29 +25,35 @@ def parse_products(raw_text):
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_description_mixed.group(2)),
 
 
41
  "description": match_description_mixed.group(1).strip(" .:-"),
42
  "unit_price": float(match_description_mixed.group(3).replace(",", "")),
43
  "amount": float(match_description_mixed.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
 
25
  lines = raw_text.split('\n')
26
 
27
  for line in lines:
28
+ line = line.strip()
29
+ if not line:
30
+ continue
31
+
32
  match = re.match(r"(\d+)\s+(.*)\s+([\d,]+\.\d{2})\s+([\d,]+\.\d{2})", line)
33
  if match:
34
  qty, desc, unit_price, amount = match.groups()
35
  structured.append({
36
+ "qty": int(qty),
37
  "description": desc.strip(),
38
+ "unit_price": float(unit_price.replace(",", "")),
39
+ "amount": float(amount.replace(",", ""))
40
  })
41
+ continue
42
+
43
+ match_description_mixed = re.search(r"^(.*?)(\d+)\s+[^A-Za-z0-9\s]?([\d.,]+)\s+[^A-Za-z0-9\s]?([\d.,]+)$", line)
44
+ if match_description_mixed:
45
+ structured.append({
46
+ "qty": int(match_description_mixed.group(2)),
47
  "description": match_description_mixed.group(1).strip(" .:-"),
48
  "unit_price": float(match_description_mixed.group(3).replace(",", "")),
49
  "amount": float(match_description_mixed.group(4).replace(",", ""))
50
  })
51
+ else:
52
  structured.append({
53
  "qty": 0,
54
+ "description": line,
55
+ "unit_price": 0.0,
56
+ "amount": 0.0
57
  })
58
 
59
  return structured