使用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 sentences = document.get(SentencesAnnotation.class); 

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

 // create new annotation Annotation annotation = new Annotation(); // create token annotations for each sentence from the input file List tokens = new ArrayList(); for(int tokenCount = 0; tokenCount < parsedSentence.size(); tokenCount++) { ArrayList 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 sentences = new ArrayList(); annotation.set(SentencesAnnotation.class, sentences); 

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

有任何想法吗?

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

有一个带有List sentences参数的Annotation 构造函数 ,如果你有一个已经标记化的句子列表,它会设置文档。

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

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