File size: 2,068 Bytes
fb12fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re

def extract_panData(data):
    unwanted_words = ["Name","/Name", "Date of Birth", "/Date of Birth", "Permanent Account Number", "Father's Name", "14 /Name", "/Father's Name"]
    
    # Clean the array by removing unwanted words and invalid entries
    cleaned_data = []
    combination_pattern = re.compile(r'(?=.*[A-Za-z])(?=.*[0-9])(?=.*[!@#$%^&*(),?":{}|<>])')

    for item in data:
        if item not in unwanted_words and not combination_pattern.search(item):
            cleaned_data.append(item)
    
    # Initialize result object
    result = {
        "statusCode": 200,
        "error": '',
        "data": {
            "panNo": '',
            "name": '',
            "fatherName": '',
            "dob": ''
        }
    }
    
    # Check and extract PAN number
    print(cleaned_data)
    pan_pattern = re.compile(r'^[A-Z]{5}[0-9]{4}[A-Z]$')
    if len(cleaned_data) > 0 and pan_pattern.match(cleaned_data[3]):
        result["data"]["panNo"] = cleaned_data[3]
    else:
        result["data"]["panNo"] = ''
        
    # Check and extract name
    name_pattern = re.compile(r'^[A-Za-z .]+$')
    if len(cleaned_data) > 1 and name_pattern.match(cleaned_data[2]):
        result["data"]["name"] = cleaned_data[2]
    else:
        result["data"]["name"] = ''
        
    # Check and extract father's name
    if len(cleaned_data) > 2 and name_pattern.match(cleaned_data[1]):
        result["data"]["fatherName"] = cleaned_data[1]
    else:
        result["data"]["fatherName"] = ''
        
    # Check and extract date of birth
    dob_pattern = re.compile(r'^\d{2}[-/]\d{2}[-/]\d{4}$')
    if len(cleaned_data) > 3 and dob_pattern.match(cleaned_data[0]):
        result["data"]["dob"] = cleaned_data[0]
    else:
        result["data"]["dob"] = ''
    
    # Check if any value is empty and set error message
    for key, value in result["data"].items():
        if value == '':
            result["statusCode"] = 400
            result["error"] = f"{key} value is not found due to bad image."
            break
    
    return result