斯坦福核心 NLP - 了解共指分辨率

2022-09-03 05:41:52

我在理解上一版斯坦福NLP工具中对coref解析器所做的更改时遇到了一些麻烦。例如,下面是一个句子和相应的CorefChainAnnotation:

The atom is a basic unit of matter, it consists of a dense central nucleus surrounded by a cloud of negatively charged electrons.

{1=[1 1, 1 2], 5=[1 3], 7=[1 4], 9=[1 5]}

我不确定我是否理解这些数字的含义。查看来源也没有真正的帮助。

谢谢


答案 1

我一直在使用共指依赖关系图,我从使用这个问题的另一个答案开始。过了一会儿,我意识到上面的这个算法并不完全正确。它产生的输出甚至不接近我拥有的修改版本。

对于使用本文的其他任何人来说,这是我最终使用的算法,该算法还可以过滤掉自我引用,因为每个代表Mention也提到自己,而很多提及只引用自己。

Map<Integer, CorefChain> coref = document.get(CorefChainAnnotation.class);

for(Map.Entry<Integer, CorefChain> entry : coref.entrySet()) {
    CorefChain c = entry.getValue();

    //this is because it prints out a lot of self references which aren't that useful
    if(c.getCorefMentions().size() <= 1)
        continue;

    CorefMention cm = c.getRepresentativeMention();
    String clust = "";
    List<CoreLabel> tks = document.get(SentencesAnnotation.class).get(cm.sentNum-1).get(TokensAnnotation.class);
    for(int i = cm.startIndex-1; i < cm.endIndex-1; i++)
        clust += tks.get(i).get(TextAnnotation.class) + " ";
    clust = clust.trim();
    System.out.println("representative mention: \"" + clust + "\" is mentioned by:");

    for(CorefMention m : c.getCorefMentions()){
        String clust2 = "";
        tks = document.get(SentencesAnnotation.class).get(m.sentNum-1).get(TokensAnnotation.class);
        for(int i = m.startIndex-1; i < m.endIndex-1; i++)
            clust2 += tks.get(i).get(TextAnnotation.class) + " ";
        clust2 = clust2.trim();
        //don't need the self mention
        if(clust.equals(clust2))
            continue;

        System.out.println("\t" + clust2);
    }
}

示例句子的最终输出如下:

representative mention: "a basic unit of matter" is mentioned by:
The atom
it

通常“原子”最终会成为代表性的提及,但在这种情况下,这并不奇怪。另一个输出稍微更准确的示例是以下句子:

独立战争发生在1700年代,这是美国的第一次战争。

生成以下输出:

representative mention: "The Revolutionary War" is mentioned by:
it
the first war in the United States

答案 2

第一个数字是群集 ID(表示令牌,代表同一实体),请参阅 的源代码。对数是CorefChain#toString()SieveCoreferenceSystem#coref(Document)

public String toString(){
    return position.toString();
}

其中位置是一组位置对的实体提及(以使它们使用)。下面是一个完整的代码示例(在 groovy 中),它显示了如何从仓位到令牌:CorefChain.getCorefMentions()

class Example {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        props.put("dcoref.score", true);
        pipeline = new StanfordCoreNLP(props);
        Annotation document = new Annotation("The atom is a basic unit of matter, it   consists of a dense central nucleus surrounded by a cloud of negatively charged electrons.");

        pipeline.annotate(document);
        Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);

        println aText

        for(Map.Entry<Integer, CorefChain> entry : graph) {
          CorefChain c =   entry.getValue();                
          println "ClusterId: " + entry.getKey();
          CorefMention cm = c.getRepresentativeMention();
          println "Representative Mention: " + aText.subSequence(cm.startIndex, cm.endIndex);

          List<CorefMention> cms = c.getCorefMentions();
          println  "Mentions:  ";
          cms.each { it -> 
              print aText.subSequence(it.startIndex, it.endIndex) + "|"; 
          }         
        }
    }
}

输出(我不明白“s”来自哪里):

The atom is a basic unit of matter, it consists of a dense central nucleus surrounded by a cloud of negatively charged electrons.
ClusterId: 1
Representative Mention: he
Mentions: he|atom |s|
ClusterId: 6
Representative Mention:  basic unit 
Mentions:  basic unit |
ClusterId: 8
Representative Mention:  unit 
Mentions:  unit |
ClusterId: 10
Representative Mention: it 
Mentions: it |

推荐