使用 Stanford CoreNLP 的共指解析

Coreference resolution using Stanford CoreNLP

我是 Stanford CoreNLP 工具包的新手,正在尝试将其用于解决新闻文本中的共指问题的项目。为了使用 Stanford CoreNLP 指代系统,我们通常会创建一个管道,这需要标记化、句子拆分、词性标记、词法化、命名实体识别和解析。例如:

Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

// read some text in the text variable
String text = "As competition heats up in Spain's crowded bank market, Banco Exterior de Espana is seeking to shed its image of a state-owned bank and move into new activities.";

// create an empty Annotation just with the given text
Annotation document = new Annotation(text);

// run all Annotators on this text
pipeline.annotate(document);

然后我们可以很容易地得到句子注释:

List<CoreMap> sentences = document.get(SentencesAnnotation.class);

但是,我正在使用其他工具进行预处理,只需要一个独立的共指解析系统。创建标记和解析树注释并将它们设置为注释非常容易:

// create new annotation
Annotation annotation = new Annotation();

// create token annotations for each sentence from the input file
List<CoreLabel> tokens = new ArrayList<>();
for(int tokenCount = 0; tokenCount < parsedSentence.size(); tokenCount++) {

        ArrayList<String> parsedLine = parsedSentence.get(tokenCount);
        String word = parsedLine.get(1);
        String lemma = parsedLine.get(2);
        String posTag = parsedLine.get(3);
        String namedEntity = parsedLine.get(4); 
        String partOfParseTree = parsedLine.get(6);

        CoreLabel token = new CoreLabel();
        token.setWord(word);
        token.setWord(lemma);
        token.setTag(posTag);
        token.setNER(namedEntity);
        tokens.add(token);
    }

// set tokens annotations to annotation
annotation.set(TokensAnnotation.class, tokens);

// set parse tree annotations to annotation
Tree stanfordParseTree = Tree.valueOf(inputParseTree);
annotation.set(TreeAnnotation.class, stanfordParseTree);

然而,创建句子注释是非常棘手的,因为据我所知,没有文档可以详细解释它。我能够为句子注释创建数据结构并将其设置为注释:

List<CoreMap> sentences = new ArrayList<CoreMap>();
annotation.set(SentencesAnnotation.class, sentences);

我相信这不会那么难,但是没有关于如何从标记注释创建句子注释的文档,即如何用实际的句子注释填充 ArrayList。

有什么想法吗?

顺便说一句,如果我使用我的处理工具提供的标记和解析树注释,并且仅使用 StanfordCoreNLP 管道提供的句子注释并应用 StanfordCoreNLP 独立的共指解析系统,我将得到正确的结果。因此,完整的独立共指解析系统唯一缺少的部分是从标记注释创建句子注释的能力。

有一个 Annotation constructor 和一个 List<CoreMap> sentences 参数,如果你有一个已经标记化的句子列表,它会设置文档。

为每个句子创建一个 CoreMap 对象,如下所示。 (请注意,我还分别为每个句子和标记对象添加了句子和标记索引。)

int sentenceIdx = 1;
List<CoreMap> sentences = new ArrayList<CoreMap>();
for (parsedSentence : parsedSentences) {
    CoreMap sentence = new CoreLabel();
    List<CoreLabel> tokens = new ArrayList<>();
    for(int tokenCount = 0; tokenCount < parsedSentence.size(); tokenCount++) {

        ArrayList<String> parsedLine = parsedSentence.get(tokenCount);
        String word = parsedLine.get(1);
        String lemma = parsedLine.get(2);
        String posTag = parsedLine.get(3);
        String namedEntity = parsedLine.get(4); 
        String partOfParseTree = parsedLine.get(6);

        CoreLabel token = new CoreLabel();
        token.setWord(word);
        token.setLemma(lemma);
        token.setTag(posTag);
        token.setNER(namedEntity);
        token.setIndex(tokenCount + 1);
        tokens.add(token);
    }

    // set tokens annotations and id of sentence 
    sentence.set(TokensAnnotation.class, tokens);
    sentence.set(SentenceIndexAnnotation.class, sentenceIdx++);

    // set parse tree annotations to annotation
    Tree stanfordParseTree = Tree.valueOf(inputParseTree);
    sentence.set(TreeAnnotation.class, stanfordParseTree);

    // add sentence to list of sentences
    sentences.add(sentence);
}

然后您可以使用 sentences 列表创建一个 Annotation 实例:

Annotation annotation = new Annotation(sentences);