|
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 |
|
|