Spaces:
Runtime error
Runtime error
Update src/extractor.py
Browse files- src/extractor.py +15 -21
src/extractor.py
CHANGED
|
@@ -4,9 +4,7 @@ GST_REGEX = r"\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}"
|
|
| 4 |
|
| 5 |
def extract_amount(line):
|
| 6 |
nums = re.findall(r"\d+\.\d+|\d+", line.replace(",", ""))
|
| 7 |
-
if nums
|
| 8 |
-
return float(nums[-1])
|
| 9 |
-
return 0.0
|
| 10 |
|
| 11 |
def extract_fields(text):
|
| 12 |
lines = text.split("\n")
|
|
@@ -28,36 +26,32 @@ def extract_fields(text):
|
|
| 28 |
data["GSTIN"] = gst_match.group()
|
| 29 |
|
| 30 |
for line in lines:
|
| 31 |
-
|
| 32 |
|
| 33 |
-
if "invoice" in
|
| 34 |
-
|
| 35 |
-
data["Invoice No"] = words[-1]
|
| 36 |
|
| 37 |
-
if "date" in
|
| 38 |
-
|
| 39 |
-
data["Date"] = words[-1]
|
| 40 |
|
| 41 |
-
if "cgst" in
|
| 42 |
data["CGST"] = extract_amount(line)
|
| 43 |
|
| 44 |
-
if "sgst" in
|
| 45 |
data["SGST"] = extract_amount(line)
|
| 46 |
|
| 47 |
-
if "igst" in
|
| 48 |
data["IGST"] = extract_amount(line)
|
| 49 |
|
| 50 |
-
if "taxable" in
|
| 51 |
data["Taxable"] = extract_amount(line)
|
| 52 |
|
| 53 |
-
if "total" in
|
| 54 |
data["Total"] = extract_amount(line)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
data["Vendor"] = l.strip()
|
| 61 |
-
break
|
| 62 |
|
| 63 |
return data
|
|
|
|
| 4 |
|
| 5 |
def extract_amount(line):
|
| 6 |
nums = re.findall(r"\d+\.\d+|\d+", line.replace(",", ""))
|
| 7 |
+
return float(nums[-1]) if nums else 0.0
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def extract_fields(text):
|
| 10 |
lines = text.split("\n")
|
|
|
|
| 26 |
data["GSTIN"] = gst_match.group()
|
| 27 |
|
| 28 |
for line in lines:
|
| 29 |
+
l = line.lower()
|
| 30 |
|
| 31 |
+
if "invoice" in l and not data["Invoice No"]:
|
| 32 |
+
data["Invoice No"] = line.split()[-1]
|
|
|
|
| 33 |
|
| 34 |
+
if "date" in l and not data["Date"]:
|
| 35 |
+
data["Date"] = line.split()[-1]
|
|
|
|
| 36 |
|
| 37 |
+
if "cgst" in l:
|
| 38 |
data["CGST"] = extract_amount(line)
|
| 39 |
|
| 40 |
+
if "sgst" in l:
|
| 41 |
data["SGST"] = extract_amount(line)
|
| 42 |
|
| 43 |
+
if "igst" in l:
|
| 44 |
data["IGST"] = extract_amount(line)
|
| 45 |
|
| 46 |
+
if "taxable" in l:
|
| 47 |
data["Taxable"] = extract_amount(line)
|
| 48 |
|
| 49 |
+
if "total" in l:
|
| 50 |
data["Total"] = extract_amount(line)
|
| 51 |
|
| 52 |
+
for line in lines:
|
| 53 |
+
if len(line.strip()) > 5:
|
| 54 |
+
data["Vendor"] = line.strip()
|
| 55 |
+
break
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return data
|