File size: 1,487 Bytes
03b6d75
e651999
03b6d75
 
e651999
03b6d75
 
 
 
 
 
 
 
 
e651999
 
03b6d75
 
 
 
e651999
 
 
 
03b6d75
 
 
 
 
 
e651999
 
 
 
 
 
03b6d75
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
import re


def parse_id_card(text, validation_type, entities):
    if validation_type == "身分證正面":
        # 正則表達式
        birthdate_pattern = r"民國\s*\d+\s*年\s*\d+\s*月\s*\d+\s*日"
        issue_date_pattern = r"民國\s*\d+\s*年\s*\d+\s*月\s*\d+\s*日(\S+)(?:補發|換發)"
        unified_id_pattern = r"[A-Za-z]\d{9}"

        birthdate = re.search(birthdate_pattern, text)
        issue_date = re.search(issue_date_pattern, text)
        unified_id = re.search(unified_id_pattern, text)

        result = {
            "解析全文內容": text,
            "姓名": entities.get('B-PER', '無法解析'),
            "出生年月日": birthdate.group() if birthdate else '無法解析',
            "發證日期": issue_date.group() if issue_date else '無法解析',
            "統一編號": unified_id.group() if unified_id else '無法解析'
        }
    elif validation_type == "身分證反面":
        result = {
            "解析全文內容": text,
            "父": entities.get('B-FATHER', '無法解析'),
            "母": entities.get('B-MOTHER', '無法解析'),
            "配偶": entities.get('B-SPOUSE', '無法解析'),
            "出生地": entities.get('B-LOC', '無法解析'),
            "住址": entities.get('I-LOC', '無法解析'),
            "編號": entities.get('B-ID', '無法解析')
        }
    else:
        result = {
            "解析全文內容": text,
        }

    return result