in main.py [0:0]
def get_slot_values(tokens, labels):
assert len(tokens) == len(labels), f'tokens:{tokens}\nlabels:{labels}'
ans = dict()
active_type = None
active_value = ''
for i in range(len(tokens)):
if labels[i] == 'O':
if active_type is not None:
if active_type not in ans:
ans[active_type] = []
ans[active_type].append(active_value)
active_type = None
active_value = ''
else:
if active_type is None:
active_type = labels[i]
active_value = tokens[i]
elif active_type == labels[i]:
active_value = active_value + ' ' + tokens[i]
elif active_type != labels[i]:
if active_type not in ans:
ans[active_type] = []
ans[active_type].append(active_value)
active_type = labels[i]
active_value = tokens[i]
else:
assert False
if active_type is not None:
if active_type not in ans:
ans[active_type] = []
ans[active_type].append(active_value)
return ans