import os
import sys
import json
def read_by_lines(path):
result = list()
with open(path, "r", encoding="utf8") as infile:
for line in infile:
result.append(line.strip())
return result
def write_by_lines(path, data):
with open(path, "w", encoding="utf8") as outfile:
[outfile.write(d + "\n") for d in data]
def extract_result(text, labels):
ret, is_start, cur_type = [], False, None
if len(text) != len(labels):
labels = labels[:len(text)]#截断
for i, label in enumerate(labels):
if label != u"O":#实体
_type = label[2:]#事件类型或角色类型
if label.startswith(u"B-"):#以B-开头
is_start = True
cur_type = _type
#这是一个实体的开始
ret.append({"start": i, "text": [text[i]], "type": _type})
elif _type != cur_type:#
cur_type = _type
is_start = True
ret.append({"start": i, "text": [text[i]], "type": _type})#新实体
elif is_start: #如果还在当前实体内,追加文本
ret[-1]["text"].append(text[i])
else:
cur_type = None
is_start = False
else:#非实体
cur_type = None
is_start = False
return ret
trigger_file='./ckp