提取包含共同引用的句子

时间:2018-06-21 05:25:19

标签: python nlp spacy

我的任务是查找包含给定共同引用的句子。 我发现使用spacy的共同引用。 例如

nlp1 = spacy.load('en_coref_md')
doc1 = nlp1('My sister has a dog. She loves him. They play every weekend. She gives him good food to eat. The dog likes her too')    

现在我可以使用命令doc1._.coref_clusters获取群集,输出为[My sister: [My sister, She, She, her], a dog: [a dog, him, him, The dog]]
因此,现在的任务是提取与“我的妹妹”有关的所有句子。
我可以使用doc1._.coref_clusters[0].mentions获得所有对“我的妹妹”的引用,这给出了输出[My sister, She, She, her]。所以现在我想要包含特殊提及的句子。
重要的是,我不能接受所有包含提及“她”的句子,因为在其他句子中,它可能涉及其他内容。为了帮助我解决此问题,命令doc1._.coref_clusters[0].mentions[1].start为我提供了特定提及的令牌编号(此处为“ She”),并且输出符合预期,即6(索引从0开始)。我们还可以使用代码

对此进行验证
doc1_list = []
for i in range(len(doc1)):
    doc1_list.append(doc1[i])    

现在我们可以看到doc1_list中的内容,输出是
 [My, sister, has, a, dog, ., She, loves, him, ., They, play, every, weekend, ., She, gives, him, good, food, to, eat, ., The, dog, likes, her, too]
我们可以看到第六个(从0开始)令牌是She。
而任务是提取包含特定共同引用“ She”的句子

0 个答案:

没有答案