通过IOB格式通过NLTK获得Stanford Ner的结果



我正在使用NLTK作为Stanford Ner Tagger的接口。我有疑问,使用nltk 获得获得IOB格式的NER结果的选项?我已经阅读了这个问题,但它适用于Java用户

NLTK版本:3.4

Java版本:JDK1.8.0_211/bin

Stanford ner模型:English.conll.4class.distsim.crf.ser.gz

输入:我叫Donald Trumph

预期输出:我的/o名/

tl; dr

首先请参见Stanford Parser和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)

[out]:

[('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')]

最新更新