Skip to content

Commit c4427eb

Browse files
authored
Update convert_prompt_data.py
1 parent 87fb479 commit c4427eb

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

convert_prompt_data.py

+88
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,94 @@ def convert_ner_cluener_prompt(inputfile,outputfile):
7474
print('max_target',max_target)
7575

7676

77+
def process_text(text,output,entity_map):
78+
text_list = list(text)
79+
for key in output:
80+
for w in output[key]:
81+
for item in output[key][w]:
82+
w_s = item[0]
83+
w_e = item[1]
84+
text_list[w_s] = entity_map[key][0] + text_list[w_s]
85+
text_list[w_e] = text_list[w_e] + entity_map[key][1]
86+
return ''.join(text_list)
87+
88+
89+
90+
def convert_ner_cluener_prompt4(inputfile,outputfile):
91+
92+
entity_map = {'name': ['<name>','</name>'],
93+
'organization': ['<organization>','</organization>'],
94+
'scene': ['<scene>','</scene>'],
95+
'company': ['<company>','</company>'],
96+
'movie': ['<movie>','</movie>'],
97+
'book': ['<book>','</book>'],
98+
'government': ['<government>','</government>'],
99+
'position': ['<position>','</position>'],
100+
'address': ['<address>','</address>'],
101+
'game': ['<game>','</game>']
102+
}
103+
104+
105+
maxlen = 0
106+
max_source = 0
107+
max_target = 0
108+
data = []
109+
with open(inputfile, 'r', encoding='utf-8') as f:
110+
for line in f:
111+
line = json.loads(line)
112+
text = line['text']
113+
prefix = """这是一个命名实体识别任务,需要你参考<样例>,对给定的<文本>和<实体标签>信息,按<要求>,抽取<文本>包含的实体。
114+
115+
<文本>
116+
{}
117+
118+
<实体标签>
119+
<company>,</company>
120+
<name>,</name>
121+
<organization>,</organization>
122+
<scene>,</scene>
123+
<movie>,</movie>
124+
<book>,</book>
125+
<government>,</government>
126+
<position>,</position>
127+
<address>,</address>
128+
<game>,</game>
129+
130+
<样例>
131+
输入:浙商银行企业信贷部叶老桂博士则从另一个角度对五道门槛进行了解读。叶老桂认为,对目前国内商业银行而言,
132+
输出:<company>浙商银行</company>企业信贷部<name>叶老桂</name>博士则从另一个角度对五道门槛进行了解读。叶老桂认为,对目前国内商业银行而言,
133+
134+
输入:生生不息CSOL生化狂潮让你填弹狂扫
135+
输出:生生不息<game>CSOL</game>生化狂潮让你填弹狂扫
136+
137+
<要求>
138+
1.提供的<实体标签>为<type>,</type>形式,表示type实体类型对应的开始与结束标签,需要你在<实体标签>限定的实体类型进行识别,不要识别之外的实体类型;
139+
2.识别过程是判断<文本>中某个连续的片段若是实体,就用对应的实体起始标签进行标记;
140+
3.输出形式参考<样例>中的输出;"""
141+
142+
output = process_text(text, line['label'],entity_map)
143+
instruction = prefix.format(text)
144+
#print(instruction)
145+
#print(output)
146+
147+
data.append({"text": text, "instruction": instruction, "output": output, "task_type": "ner_cluener"})
148+
149+
if len(instruction) + len(output) > maxlen:
150+
maxlen = len(instruction) + len(output)
151+
if len(instruction) > max_source:
152+
max_source = len(instruction)
153+
if len(output) > max_target:
154+
max_target = len(output)
155+
156+
with open(outputfile, 'w', encoding='utf-8') as f:
157+
for line in data:
158+
f.write(json.dumps(line, ensure_ascii=False)+'\n')
159+
160+
print('maxlen',maxlen)
161+
print('max_source',max_source)
162+
print('max_target',max_target)
163+
164+
77165

78166
if __name__=="__main__":
79167
inputfile = r'E:\open_data\cluener_public\train.json'

0 commit comments

Comments
 (0)