【发布时间】:2021-07-01 00:08:08
【问题描述】:
我有以下列表:
data= [
[[0.025],
['-DOCSTART-'],
['O']],
[[0.166, 0.001, 4.354, 4.366, 7.668],
['Summary', 'of', 'Consolidated', 'Financial', 'Data'],
['O', 'O', 'B-ORG', 'I-ORG', 'E-ORG']],
[[0.195, 0.1, 0.0, 3.561, 3.793, 6.741, 4.0, 0.05],
['Port', 'conditions', 'from', 'Lloyds', 'Shipping', 'Intelligence', 'Service', '--'],
['S-PER', 'O', 'O', 'B-ORG', 'I-ORG', 'I-ORG', 'E-ORG', 'O']]
]
注意:data[i] 中的每个列表都具有相同的长度,i in [0, 1, 2]。
我想创建一个 JSON 文件如下:
[{
"sentence": "-DOCSTART- Summary of Consolidated Financial Data Port conditions from Lloyds Shipping Intelligence Service --",
"annotations": [
{
"decision": "Consolidated Financial Data",
"category": "ORG",
"token_loss": [4.354, 4.366, 7.668],
"totalloss": 4.354+4.366+7.668 # Here, I consider the sum of "token_loss"
},
{
"decision": "Port",
"category": "PER",
"token_loss": 18.44,
"totalloss": 18.44
},
{
"decision": "Lloyds Shipping Intelligence Service",
"category": "ORG",
"token_loss": [3.561, 3.793, 6.741, 4.0],
"totalloss": 3.561+3.793+6.741+4.0
}]
}]
在列表中,总是有“B-”(开始)、“I-”(内部)和“E-”(结束)的序列。总是有一个单词带有“S-”(Single)。我不考虑“O-”(外部)的词。
这就是我开始尝试解决这个问题的方法。
startIdx = 0
endIdx = 10
decisions = []
for tag in tags:
if tag.startswith('B'):
start = tags.index(tag)
startIdx = start
while startIdx<10:
if tags[startIdx+1].startswith('I'):
decisions.append(tokens[startIdx:startIdx+1])
startIdx += 1
if tags[startIdx+1].startswith('E'):
decisions.append(tokens[startIdx:startIdx+1])
startIdx = 11
【问题讨论】:
-
我试过但没有找到完整的答案我没有发布我的代码,因为它的结构还不够好。