文本个人信息脱敏
更新时间:2026-11-23
简介
用正则把文本中的手机号、身份证、银行卡、邮箱、密钥、IP 等个人信息替换成占位符,并按类型统计命中次数。不依赖模型和 GPU。
功能描述
- 支持类型:PEM、JWT、URL(默认关)、IP、MAC、文件路径、邮箱、密钥键值、会话 ID、渠道标识、平台 open_id、身份证、银行卡、电话
- 中文场景下紧贴汉字的号码也能识别
- 可用
extra_patterns追加自定义规则 - 输入为 None 或处理出错的行返回 None
算子参数
输入
| 输入 | 含义 |
|---|---|
| texts | 文本字符串 |
输出
| 输出 | 含义 |
|---|---|
| text | 脱敏后的文本(string) |
| hit_count | 本行命中总次数(int32) |
| hits | 按类型的命中次数(struct) |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| mask_paths / mask_emails / mask_secrets / mask_ids / mask_phones / mask_id_cards / mask_bank_cards / mask_channel_ids / mask_platform_open_ids / mask_pem / mask_jwt / mask_ips / mask_macs | bool | True | 各类型是否脱敏 |
| mask_urls | bool | False | 是否脱敏 URL |
| id_card_checksum | bool | False | 只脱敏第 18 位校验码正确的身份证号 |
| bank_card_luhn | bool | True | 只脱敏通过 Luhn 校验的卡号 |
| *_replacement | str | 如 [PHONE_REDACTED] | 各类型的占位符 |
| extra_patterns | list[list[str]] | None | 自定义 [[正则, 替换串], ...] |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.text.text_pii_redaction import TextPiiRedaction
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18samples = {
19 "texts": [
20 "可通过邮箱baiyaning@163.com或手机13945671234联系,身份证号码为230103198008273629。",
21 "银行卡 6222021234567890128,座机 010-62345678",
22 '{"password": "hunter2", "api_key": "sk-abc123"}',
23 None,
24 ]
25 }
26 ds = daft.from_pydict(samples)
27 ds = ds.with_column(
28 "redacted",
29 aihc_udf(
30 TextPiiRedaction,
31 construct_args={"id_card_checksum": False},
32 num_cpus=1,
33 concurrency=1,
34 batch_size=64,
35 )(col("texts")),
36 )
37 ds.select(col("redacted").struct.get("text"), col("redacted").struct.get("hit_count")).show()
评价此篇文章
