Я хочу извлечь "текст" из токенов в файл JSONL. Если есть ярлык, я тоже хочу его извлечь. Если его нет, я хочу вставить «O» в качестве значения для метки
{"text":"This is the first sentence.","_input_hash":2083129218,"_task_hash":-536378640,"spans":[],"meta":{"score":0.5,"pattern":65},"answer":"accept","tokens":[
{"text":"This","id":0},
{"text":"is","id":1},
{"text":"the","id":2},
{"text":"first","id":3},
{"text":"sentence","id":4},
{"text":".","id":5}]}
{"text":"This is coded in python.","_input_hash":2083129218,"_task_hash":-536378640,"spans":[],"meta":{"score":0.5,"pattern":65},"answer":"accept","tokens":[
{"text":"This","id":0},
{"text":"is","id":1},
{"text":"coded","id":2},
{"text":"in","id":3},
{"text":"python","label":"Programming"},
{"text":".","id":5}]}
Код, который можно использовать для извлечения текста и идентификатора из токенов, выглядит следующим образом, если метка отсутствует: (спасибо @DeveshKumarSingh в моем предыдущем вопросе )
import jsonlines
#Open the file, iterate over the tokens and make the tuples
result = [(idx+1, i['text'], i['id']+1) for idx, obj in enumerate(jsonlines.open('file.txt')) for i in obj['tokens']]
print(result)
Ожидаемый результат:
![enter image description here](https://i.stack.imgur.com/KBDHr.png)