frknuzn/ocr-poc
0
1import re2 3# Tolerant label patterns (Turkish variations)4LABEL_DATE = re.compile(r"\bta?r[ıi]h\b", re.IGNORECASE)5LABEL_TIME = re.compile(r"\bsa?at\b", re.IGNORECASE)6LABEL_RECEIPT = re.compile(r"\bfi[şs]|fis\b", re.IGNORECASE)7LABEL_VAT_TOTAL = re.compile(r"k?dv\s*(toplam[ıi]|top|tutar[ıi])|top\.?\s*k\s*d\s*v", re.IGNORECASE)8LABEL_GRAND_TOTAL = re.compile(r"(genel\s*)?toplam|toplam\s*tutar", re.IGNORECASE)9 10# Money pattern (tolerant for thousand separators and , or . decimals)11MONEY = re.compile(r"(?:\d{1,3}(?:[\.,]\d{3})*|\d+)[,\.]\d{2}")12 13# Date and time extraction (flexible separators)14DATE = re.compile(r"\b(\d{1,2}[\/\.\-]\d{1,2}[\/\.\-]\d{2,4})\b")15TIME = re.compile(r"\b(\d{1,2}:\d{2}(?::\d{2})?)\b")16 17def last_money_on_line(line: str) -> str | None:18 """Return last money-like token on a line, if any. Strips currency symbols."""19 m = None20 for m in MONEY.finditer(line.replace("₺", "").replace("TL", " ").replace("TRY", " ")):21 pass22 return m.group(0) if m else None23 24 25_TRANS = str.maketrans({26 "İ": "I", "I": "I", "Ş": "S", "Ğ": "G", "Ü": "U", "Ö": "O", "Ç": "C",27 "ı": "I", "ş": "S", "ğ": "G", "ü": "U", "ö": "O", "ç": "C",28 "ß": "S", "€": "E", "£": "L",29})30 31 32def normalize_label(s: str) -> str:33 s = (s or "").upper().translate(_TRANS)34 s = s.replace("0", "O").replace("1", "I").replace("2", "Z")35 s = re.sub(r"[^A-Z0-9]", "", s)36 return s37 38 39def label_contains(line: str, key: str) -> bool:40 return key in normalize_label(line)41 