jayanth-2005 commited on
Commit
44f6eb2
·
verified ·
1 Parent(s): 884071e

Update src/extractor.py

Browse files
Files changed (1) hide show
  1. 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
- lower = line.lower()
32
 
33
- if "invoice" in lower and not data["Invoice No"]:
34
- words = line.split()
35
- data["Invoice No"] = words[-1]
36
 
37
- if "date" in lower and not data["Date"]:
38
- words = line.split()
39
- data["Date"] = words[-1]
40
 
41
- if "cgst" in lower:
42
  data["CGST"] = extract_amount(line)
43
 
44
- if "sgst" in lower:
45
  data["SGST"] = extract_amount(line)
46
 
47
- if "igst" in lower:
48
  data["IGST"] = extract_amount(line)
49
 
50
- if "taxable" in lower:
51
  data["Taxable"] = extract_amount(line)
52
 
53
- if "total" in lower:
54
  data["Total"] = extract_amount(line)
55
 
56
- # Vendor fallback (first non-empty line)
57
- if not data["Vendor"]:
58
- for l in lines:
59
- if len(l.strip()) > 5:
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