寻找与coref实例有关的特定令牌-Spacy和Neuralcoref

时间:2019-02-05 15:44:22

标签: python spacy

我正在寻找一种适当的方法来查找正在查找的某些文档中在共指称谓中使用了特定代词的实例。我的记忆力有问题,这使我觉得我没有以非常经济的方式进行记忆,但是我也不确定我是否会以正确的方式进行记忆。这个想法是要识别它的所有实例,并且暂时将它们定位在共同引用实例中,但是我可能想对此进行扩展。然后,我想继续用共指的首字母替换所有这些实例。

此刻我的代码如下:

import spacy
import neuralcoref
import en_coref_md
import os

nlp_en = en_coref_md.load()
path = './input_zenodo/'
filelist = os.listdir(path)

for file in filelist:
    try:
        file = open(path+file).read()
        doc = nlp_en(file)
        is_pronoun = 'it' 
    except MemoryError:
        print('MemoryError:'+file+'\n')  

    for token in doc:
        if token.text == is_pronoun:
                print(token._.in_coref)
                print(token._.coref_clusters)

如果任何人都可以就这样做的潜在更经济的方式提供建议,或者就如何仅替换文档中的这些共同引用集群提供建议,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果您只想用每个引用的标题替换引用,则accumulate中有一个内置的select date,max(case when type='First' then value end) as First, max(case when type='Second' then value end) as Second from t group by date 属性:

coref_resolved

返回

neuralcoref

实现它的代码是here,如果您真的只想解析代词而不是全部corefs,那么似乎可以对其进行修改。