TL; DR
Сначала см. Стэнфордский парсер и NLTK
Напишите простой цикл и переберите выходные данные NER:
def stanford_to_bio(tagged_sent):
prev_tag = "O"
bio_tagged_output = []
current_ner = []
for word, tag in tagged_sent:
if tag == 'O':
bio_tagged_output += current_ner
bio_tagged_output.append((word, tag))
current_ner = []
prev_tag = 'O'
else:
if prev_tag == 'O':
current_ner.append((word, 'B-'+tag))
prev_tag = 'B'
else:
current_ner.append((word, 'I-'+tag))
prev_tag = 'I'
if current_ner:
bio_tagged_output += current_ner
return bio_tagged_output
tagged_sent = [('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'), ('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'), ('in', 'O'), ('NY', 'STATE_OR_PROVINCE')]
stanford_to_bio(tagged_sent)
[выход]:
[('Rami', 'B-PERSON'),
('Eid', 'I-PERSON'),
('is', 'O'),
('studying', 'O'),
('at', 'O'),
('Stony', 'B-ORGANIZATION'),
('Brook', 'I-ORGANIZATION'),
('University', 'I-ORGANIZATION'),
('in', 'O'),
('NY', 'B-STATE_OR_PROVINCE')]