我正在使用nltk作为Stanford NER Tagger的接口。我有疑问是否可以使用NLTK 以IOB格式获取NER结果?我已经读过此question,但它是针对Java用户的
NLTK版本:3.4
Java版本:jdk1.8.0_211 / bin
斯坦福大学NER模型:english.conll.4class.distsim.crf.ser.gz
输入:我叫Donald Trumph
预期输出:我/ O的名字/ O是/ O Donald / B-PERSON Trumph / I-PERSON
答案 0 :(得分:1)
编写一个简单的循环并遍历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')]