265 lines
7.3 KiB
Python
265 lines
7.3 KiB
Python
import textwrap
|
||
|
||
|
||
def get_ner_name_prompt(text: str) -> str:
|
||
"""
|
||
Returns a prompt that generates a mapping of original names/companies to their masked versions.
|
||
|
||
Args:
|
||
text (str): The input text to be analyzed for masking
|
||
|
||
Returns:
|
||
str: The formatted prompt that will generate a mapping dictionary
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的法律文本实体识别助手。请从以下文本中抽取出所有需要脱敏的敏感信息,并按照指定的类别进行分类。请严格按照JSON格式输出结果。
|
||
|
||
实体类别包括:
|
||
- 人名 (不包括律师、法官、书记员、检察官等公职人员)
|
||
- 英文人名
|
||
|
||
|
||
待处理文本:
|
||
{text}
|
||
|
||
输出格式:
|
||
{{
|
||
"entities": [
|
||
{{"text": "原始文本内容", "type": "人名"}},
|
||
{{"text": "原始文本内容", "type": "英文人名"}},
|
||
...
|
||
]
|
||
}}
|
||
|
||
|
||
|
||
请严格按照JSON格式输出结果。
|
||
|
||
""")
|
||
return prompt.format(text=text)
|
||
|
||
|
||
def get_ner_company_prompt(text: str) -> str:
|
||
"""
|
||
Returns a prompt that generates a mapping of original companies to their masked versions.
|
||
|
||
Args:
|
||
text (str): The input text to be analyzed for masking
|
||
|
||
Returns:
|
||
str: The formatted prompt that will generate a mapping dictionary
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的法律文本实体识别助手。请从以下文本中抽取出所有需要脱敏的敏感信息,并按照指定的类别进行分类。请严格按照JSON格式输出结果。
|
||
|
||
实体类别包括:
|
||
- 公司名称
|
||
- 英文公司名称
|
||
- Company with English name
|
||
- 公司名称简称
|
||
- 公司英文名称简称
|
||
|
||
|
||
待处理文本:
|
||
{text}
|
||
|
||
输出格式:
|
||
{{
|
||
"entities": [
|
||
{{"text": "原始文本内容", "type": "公司名称"}},
|
||
{{"text": "原始文本内容", "type": "英文公司名称"}},
|
||
{{"text": "原始文本内容", "type": "公司名称简称"}},
|
||
{{"text": "原始文本内容", "type": "公司英文名称简称"}},
|
||
...
|
||
]
|
||
}}
|
||
|
||
请严格按照JSON格式输出结果。
|
||
""")
|
||
return prompt.format(text=text)
|
||
|
||
|
||
def get_ner_address_prompt(text: str) -> str:
|
||
"""
|
||
Returns a prompt that generates a mapping of original addresses to their masked versions.
|
||
|
||
Args:
|
||
text (str): The input text to be analyzed for masking
|
||
|
||
Returns:
|
||
str: The formatted prompt that will generate a mapping dictionary
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的法律文本实体识别助手。请从以下文本中抽取出所有需要脱敏的敏感信息,并按照指定的类别进行分类。请严格按照JSON格式输出结果。
|
||
|
||
实体类别包括:
|
||
- 地址
|
||
|
||
|
||
待处理文本:
|
||
{text}
|
||
|
||
输出格式:
|
||
{{
|
||
"entities": [
|
||
{{"text": "原始文本内容", "type": "地址"}},
|
||
...
|
||
]
|
||
}}
|
||
|
||
请严格按照JSON格式输出结果。
|
||
""")
|
||
return prompt.format(text=text)
|
||
|
||
|
||
def get_address_masking_prompt(address: str) -> str:
|
||
"""
|
||
Returns a prompt that generates a masked version of an address following specific rules.
|
||
|
||
Args:
|
||
address (str): The original address to be masked
|
||
|
||
Returns:
|
||
str: The formatted prompt that will generate a masked address
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的地址脱敏助手。请对给定的地址进行脱敏处理,遵循以下规则:
|
||
|
||
脱敏规则:
|
||
1. 保留区级以上地址(省、市、区、县)
|
||
2. 路名以大写首字母替代,例如:恒丰路 -> HF路
|
||
3. 门牌数字以**代替,例如:66号 -> **号
|
||
4. 大厦名、小区名以大写首字母替代,例如:白云大厦 -> BY大厦
|
||
5. 房间号以****代替,例如:1607室 -> ****室
|
||
|
||
示例:
|
||
- 输入:上海市静安区恒丰路66号白云大厦1607室
|
||
- 输出:上海市静安区HF路**号BY大厦****室
|
||
|
||
- 输入:北京市海淀区北小马厂6号1号楼华天大厦1306室
|
||
- 输出:北京市海淀区北小马厂**号**号楼HT大厦****室
|
||
|
||
请严格按照JSON格式输出结果:
|
||
|
||
{{
|
||
"masked_address": "脱敏后的地址"
|
||
}}
|
||
|
||
原始地址:{address}
|
||
|
||
请严格按照JSON格式输出结果。
|
||
""")
|
||
return prompt.format(address=address)
|
||
|
||
|
||
def get_ner_project_prompt(text: str) -> str:
|
||
"""
|
||
Returns a prompt that generates a mapping of original project names to their masked versions.
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的法律文本实体识别助手。请从以下文本中抽取出所有需要脱敏的敏感信息,并按照指定的类别进行分类。请严格按照JSON格式输出结果。
|
||
|
||
实体类别包括:
|
||
- 项目名(此处项目特指商业、工程、合同等项目)
|
||
|
||
待处理文本:
|
||
{text}
|
||
|
||
输出格式:
|
||
{{
|
||
"entities": [
|
||
{{"text": "原始文本内容", "type": "项目名"}},
|
||
...
|
||
]
|
||
}}
|
||
|
||
请严格按照JSON格式输出结果。
|
||
""")
|
||
return prompt.format(text=text)
|
||
|
||
|
||
def get_ner_case_number_prompt(text: str) -> str:
|
||
"""
|
||
Returns a prompt that generates a mapping of original case numbers to their masked versions.
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的法律文本实体识别助手。请从以下文本中抽取出所有需要脱敏的敏感信息,并按照指定的类别进行分类。请严格按照JSON格式输出结果。
|
||
|
||
实体类别包括:
|
||
- 案号
|
||
|
||
待处理文本:
|
||
{text}
|
||
|
||
输出格式:
|
||
{{
|
||
"entities": [
|
||
{{"text": "原始文本内容", "type": "案号"}},
|
||
...
|
||
]
|
||
}}
|
||
|
||
请严格按照JSON格式输出结果。
|
||
""")
|
||
return prompt.format(text=text)
|
||
|
||
|
||
def get_entity_linkage_prompt(entities_text: str) -> str:
|
||
"""
|
||
Returns a prompt that identifies related entities and groups them together.
|
||
|
||
Args:
|
||
entities_text (str): The list of entities to be analyzed for linkage
|
||
|
||
Returns:
|
||
str: The formatted prompt that will generate entity linkage information
|
||
"""
|
||
prompt = textwrap.dedent("""
|
||
你是一个专业的法律文本实体关联分析助手。请分析以下实体列表,识别出相互关联的实体(如全称与简称、中文名与英文名等),并将它们分组。
|
||
|
||
关联规则:
|
||
1. 公司名称关联:
|
||
- 全称与简称(如:"阿里巴巴集团控股有限公司" 与 "阿里巴巴")
|
||
- 中文名与英文名(如:"腾讯科技有限公司" 与 "Tencent Technology Ltd.")
|
||
- 母公司与子公司(如:"腾讯" 与 "腾讯音乐")
|
||
|
||
|
||
2. 每个组中应指定一个主要实体(is_primary: true),通常是:
|
||
- 对于公司:选择最正式的全称
|
||
- 对于人名:选择最常用的称呼
|
||
|
||
待分析实体列表:
|
||
{entities_text}
|
||
|
||
输出格式:
|
||
{{
|
||
"entity_groups": [
|
||
{{
|
||
"group_id": "group_1",
|
||
"group_type": "公司名称",
|
||
"entities": [
|
||
{{
|
||
"text": "阿里巴巴集团控股有限公司",
|
||
"type": "公司名称",
|
||
"is_primary": true
|
||
}},
|
||
{{
|
||
"text": "阿里巴巴",
|
||
"type": "公司名称简称",
|
||
"is_primary": false
|
||
}}
|
||
]
|
||
}}
|
||
]
|
||
}}
|
||
|
||
注意事项:
|
||
1. 只对确实有关联的实体进行分组
|
||
2. 每个实体只能属于一个组
|
||
3. 每个组必须有且仅有一个主要实体(is_primary: true)
|
||
4. 如果实体之间没有明显关联,不要强制分组
|
||
5. group_type 应该是 "公司名称"
|
||
|
||
请严格按照JSON格式输出结果。
|
||
""")
|
||
return prompt.format(entities_text=entities_text) |