文档智能处理
High Contrast
Dark Mode
Light Mode
Sepia
Forest
1 min read238 words

文档智能处理

每天处理上百份合同、发票、简历?让 AI 来读文档,你来做决策。

文档处理架构

graph TD INPUT[文档输入] --> CLASSIFY[分类] CLASSIFY --> OCR[OCR 识别] OCR --> EXTRACT[信息抽取] EXTRACT --> VALIDATE[校验] VALIDATE --> OUTPUT[结构化输出] INPUT --> PDF[PDF] INPUT --> IMAGE[图片] INPUT --> SCAN[扫描件] EXTRACT --> NER[实体识别] EXTRACT --> TABLE[表格抽取] EXTRACT --> KV[键值对] style OCR fill:#e3f2fd,stroke:#1565c0,stroke-width:2px style EXTRACT fill:#c8e6c9,stroke:#388e3c,stroke-width:2px

文档处理工具

工具 类型 价格 强项
Tesseract 开源 OCR 免费 通用文字识别
PaddleOCR 开源 OCR 免费 中文、表格
Azure Document Intelligence 云服务 $1.5/1000页 结构化抽取
AWS Textract 云服务 $1.5/1000页 表格、表单
百度 OCR 云服务 免费(限量) 中文场景

文档智能抽取

"""
文档信息抽取 (规则 + 模式匹配)
"""
import re
from dataclasses import dataclass
@dataclass
class ExtractedInvoice:
invoice_no: str
date: str
amount: float
vendor: str
items: list[dict]
class DocumentExtractor:
"""文档信息抽取器"""
PATTERNS = {
"发票号": r"发票号[码]?\s*[::]\s*(\d{8,20})",
"日期": r"(\d{4}[-/年]\d{1,2}[-/月]\d{1,2}[日]?)",
"金额": r"[合计总]?\s*金额\s*[::]?\s*[¥¥]?\s*([\d,]+\.?\d*)",
"税额": r"税\s*额\s*[::]?\s*[¥¥]?\s*([\d,]+\.?\d*)",
"电话": r"(1[3-9]\d{9})",
"邮箱": r"([\w.-]+@[\w.-]+\.\w+)",
"身份证": r"(\d{17}[\dXx])",
}
@classmethod
def extract_invoice(cls, text: str) -> dict:
"""从文本中抽取发票信息"""
result = {}
for field, pattern in cls.PATTERNS.items():
match = re.search(pattern, text)
result[field] = match.group(1) if match else "未识别"
return result
@staticmethod
def extract_table(text: str) -> list[dict]:
"""简易表格抽取"""
lines = text.strip().split("\n")
if len(lines) < 2:
return []
# 假设第一行是表头
headers = [h.strip() for h in lines[0].split("|") if h.strip()]
rows = []
for line in lines[1:]:
if "---" in line:
continue
cells = [c.strip() for c in line.split("|") if c.strip()]
if len(cells) == len(headers):
rows.append(dict(zip(headers, cells)))
return rows
@staticmethod
def classify_document(text: str) -> str:
"""文档分类"""
keywords = {
"发票": ["发票", "税额", "纳税人"],
"合同": ["甲方", "乙方", "合同期限", "违约"],
"简历": ["教育背景", "工作经验", "技能"],
"报告": ["分析", "总结", "建议", "结论"],
}
scores = {}
text_lower = text.lower()
for doc_type, words in keywords.items():
score = sum(1 for w in words if w in text_lower)
scores[doc_type] = score
best = max(scores, key=scores.get)
return best if scores[best] > 0 else "未知"
# 演示
extractor = DocumentExtractor()
sample_invoice = """
增值税普通发票
发票号码:12345678901234567890
开票日期:2024-03-15
购方:深圳科技有限公司
合计金额:¥12,580.00
税额:¥1,635.40
联系电话:13800138000
邮箱:finance@example.com
"""
print("=== 发票信息抽取 ===")
for field, value in extractor.extract_invoice(sample_invoice).items():
print(f"  {field}: {value}")
print(f"\n文档类型: {extractor.classify_document(sample_invoice)}")

合同审查自动化

"""
合同关键条款抽取
"""
CONTRACT_CHECKS = {
"合同期限": {
"模式": r"合同期限\s*[::]\s*(.+?)(?:\n|。)",
"风险": "注意自动续期条款",
},
"违约金": {
"模式": r"违约金\s*[::]?\s*(.+?)(?:\n|。)",
"风险": "违约金是否对等",
},
"付款条件": {
"模式": r"付款\s*[::]?\s*(.+?)(?:\n|。)",
"风险": "确认付款节点和比例",
},
"知识产权": {
"模式": r"知识产权\s*[::]?\s*(.+?)(?:\n|。)",
"风险": "成果归属是否明确",
},
"争议解决": {
"模式": r"(?:争议|纠纷).*?(?:仲裁|法院)",
"风险": "管辖地是否对自己有利",
},
}
print("=== 合同审查清单 ===")
for clause, info in CONTRACT_CHECKS.items():
print(f"  {clause}: {info['风险']}")

LLM 增强文档处理

场景 传统方式 LLM 方式 准确率提升
发票识别 OCR + 正则 OCR + LLM 理解 +15-20%
合同审查 人工逐条 LLM 摘要 + 风险提示 效率提升 5x
简历筛选 关键词匹配 LLM 语义理解 +25-30%
邮件分类 规则过滤 LLM 意图识别 +20%

下一章:AI 办公助手——让 AI 成为你的全天候助理。