Spaces:
Runtime error
Runtime error
Merge pull request 'Added parsers for Kaufland and Spar #1'
Browse files
receipt_processor/parsers/kaufland_parser.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from .base import BaseParser
|
| 4 |
+
|
| 5 |
+
class KauflandParser(BaseParser):
|
| 6 |
+
def parse(self, lines):
|
| 7 |
+
receipt = {
|
| 8 |
+
"store": "Kaufland",
|
| 9 |
+
"date": None,
|
| 10 |
+
"address": None,
|
| 11 |
+
"items": [],
|
| 12 |
+
"parser_used": "KauflandParser"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# === Address detection ===
|
| 16 |
+
for line in lines:
|
| 17 |
+
if re.search(r'\b\d{1,4}[a-zA-Z]?,?\s*\b.*Zagreb\b', line):
|
| 18 |
+
receipt["address"] = line.strip()
|
| 19 |
+
break
|
| 20 |
+
|
| 21 |
+
# === Date detection (store raw format from "Datum")
|
| 22 |
+
for line in lines:
|
| 23 |
+
match = re.search(r'datum[:\s]*([0-9]{2}[./][0-9]{2}[./][0-9]{2})', line, re.IGNORECASE)
|
| 24 |
+
if match:
|
| 25 |
+
receipt["date"] = match.group(1).replace('.', '/')
|
| 26 |
+
break
|
| 27 |
+
|
| 28 |
+
# === Product name and price pairing ===
|
| 29 |
+
last_valid_name = None
|
| 30 |
+
|
| 31 |
+
for i in range(len(lines)):
|
| 32 |
+
line = lines[i].strip()
|
| 33 |
+
|
| 34 |
+
# Save potential product name
|
| 35 |
+
if re.match(r'^[A-Za-zČĆŽŠĐčćžšđ\s\d]+$', line) and len(line) > 4 and not any(x in line.lower() for x in ["račun", "cijena", "kartica", "za platiti", "datum", "mid", "pan", "tid", "apn"]):
|
| 36 |
+
last_valid_name = line.title()
|
| 37 |
+
continue
|
| 38 |
+
|
| 39 |
+
# Match a clean price line
|
| 40 |
+
match = re.match(r'^(\d+[.,]\d{2})$', line)
|
| 41 |
+
if match and last_valid_name:
|
| 42 |
+
price = float(match.group(1).replace(",", "."))
|
| 43 |
+
receipt["items"].append({
|
| 44 |
+
"name": last_valid_name,
|
| 45 |
+
"quantity": 1.0,
|
| 46 |
+
"price": price
|
| 47 |
+
})
|
| 48 |
+
break # stop after first match for now
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
return receipt
|
receipt_processor/parsers/parser_selector.py
CHANGED
|
@@ -7,7 +7,9 @@ class ParserSelector:
|
|
| 7 |
'lidl': ['lidl'],
|
| 8 |
'konzum': ['konzum'],
|
| 9 |
'plodine': ['plodine'],
|
| 10 |
-
'studenac': ['studenac']
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
def get_store_parser(self, text: str):
|
|
|
|
| 7 |
'lidl': ['lidl'],
|
| 8 |
'konzum': ['konzum'],
|
| 9 |
'plodine': ['plodine'],
|
| 10 |
+
'studenac': ['studenac'],
|
| 11 |
+
'spar': ['spar'],
|
| 12 |
+
'kaufland': ['kaufland']
|
| 13 |
}
|
| 14 |
|
| 15 |
def get_store_parser(self, text: str):
|
receipt_processor/parsers/spar_parser.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from .base import BaseParser
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class SparParser(BaseParser):
|
| 7 |
+
def parse(self, lines):
|
| 8 |
+
receipt = {
|
| 9 |
+
"store": "Spar",
|
| 10 |
+
"date": None,
|
| 11 |
+
"total": None,
|
| 12 |
+
"address": None,
|
| 13 |
+
"items": [],
|
| 14 |
+
"parser_used": "SparParser"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# === Address detection ===
|
| 18 |
+
for i in range(len(lines)):
|
| 19 |
+
line = lines[i].strip()
|
| 20 |
+
if re.search(r'\bSPAR SUPERMARKET\b', line, re.IGNORECASE):
|
| 21 |
+
if i + 1 < len(lines):
|
| 22 |
+
potential_address = lines[i + 1].strip()
|
| 23 |
+
if re.search(r'\d{4,5}\s+\w+', potential_address):
|
| 24 |
+
receipt["address"] = potential_address
|
| 25 |
+
break
|
| 26 |
+
|
| 27 |
+
# === Date detection (convert to ISO format) ===
|
| 28 |
+
for line in lines:
|
| 29 |
+
match = re.search(r'(\d{2}\.\d{2}\.\d{4})', line)
|
| 30 |
+
if match:
|
| 31 |
+
try:
|
| 32 |
+
parsed_date = datetime.strptime(match.group(1), "%d.%m.%Y")
|
| 33 |
+
receipt["date"] = parsed_date.isoformat()
|
| 34 |
+
except:
|
| 35 |
+
receipt["date"] = match.group(1)
|
| 36 |
+
break
|
| 37 |
+
|
| 38 |
+
# === Total amount (optional, not in output spec) ===
|
| 39 |
+
for line in reversed(lines):
|
| 40 |
+
if any(x in line.lower() for x in ["ukupno", "plaćanje", "total"]):
|
| 41 |
+
match = re.search(r'(\d+[.,]\d{2})', line)
|
| 42 |
+
if match:
|
| 43 |
+
receipt["total"] = float(match.group(1).replace(",", "."))
|
| 44 |
+
break
|
| 45 |
+
|
| 46 |
+
# === 3-line item block parsing ===
|
| 47 |
+
i = 0
|
| 48 |
+
while i < len(lines) - 2:
|
| 49 |
+
name_line = lines[i].strip()
|
| 50 |
+
qty_price_line = lines[i + 1].strip()
|
| 51 |
+
total_line = lines[i + 2].strip()
|
| 52 |
+
|
| 53 |
+
match = re.search(r'(\d+)\s*[x×]\s*(\d+[.,]\d{2})', qty_price_line, re.IGNORECASE)
|
| 54 |
+
total_match = re.search(r'(\d+[.,]\d{2})', total_line)
|
| 55 |
+
|
| 56 |
+
if match and total_match:
|
| 57 |
+
name = name_line.title()
|
| 58 |
+
quantity = float(match.group(1))
|
| 59 |
+
total_price = float(total_match.group(1).replace(",", "."))
|
| 60 |
+
|
| 61 |
+
receipt["items"].append({
|
| 62 |
+
"name": name,
|
| 63 |
+
"quantity": quantity,
|
| 64 |
+
"price": total_price
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
i += 3
|
| 68 |
+
else:
|
| 69 |
+
i += 1
|
| 70 |
+
|
| 71 |
+
return receipt
|