如何使用Stanford NER在python中对具有相同命名实体类别的两个句子进行对齐?

2024-05-08 17:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我将使用python中的stanfordner来对齐两个不同的句子,它们包含相同的NER类别('PERSON','LOCATION','ORGANIZATION')。你知道吗

sentence1 = John is reading a booklet in London Library
sentence2 = Michael reads a brochure in England Library

我想要的结果是:

result= [[[u'John', u'Person'],[u'Michael',u'PERSON']] , [[u'London',u'LOCATION],['u'England',u'LOCATION']]]

我试过我的密码

def alignNER(self, sent1, sent2):
    java.path = 'C:/program files'/java/jre/bin/java.exe'
    os.environ ['JAVAHOME'] = java.path
    st = StanfordNERTagger('usr/english.all.3class.distsim.crf.ser','usr/stanford-ner.jar')

    result = []
    for i in xrange(len(sent1)):
        if sent1[i]:
            word = sent1[i]
            temp = st.tag(word.split())
            for token, tag in temp:
                if tag in ['PERSON','LOCATION','ORGANIZATION']:
                    result.append([temp,i])
    return result

    for j in xrange(len(sent2)):
        if sent2[j]:
            word = sent2[j]
            temp = st.tag(word.split())
            for token, tag in temp:
                if tag in ['PERSON','LOCATION','ORGANIZATION']:
                    result.append([temp,j])
    return result

当我叫它

checkNER = alignNER(sent1, sent2)
print checkNER

结果只显示了带有检测类别位置索引的句子1:

[[[u'John', u'Person'],0],[[u'London',u'LOCATION],6]]

有人能帮忙吗?谢谢


Tags: inforiftaglocationjavaresultjohn

热门问题