【问题标题】:Coreference resolution using Stanford CoreNLP使用斯坦福 CoreNLP 的共指解析
【发布时间】:2015-06-20 13:43:21
【问题描述】:

我是斯坦福 CoreNLP 工具包的新手,并试图将它用于解决新闻文本中的共同引用的项目。为了使用斯坦福 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 独立的共指解析系统,我会得到正确的结果。因此,完整的独立共指解析系统唯一缺少的部分是能够从标记注释创建句子注释。

【问题讨论】:

    标签: java nlp stanford-nlp


    【解决方案1】:

    如果您有一个已经标记化的句子列表,则有一个 Annotation constructor 和一个 List&lt;CoreMap&gt; 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);
    

    【讨论】:

    • @Sebastian Schuster 非常感谢你,就像一个魅力。刚刚添加token.setValue(word);设置token值和sentence.set(ValueAnnotation.class, SENTENCE_CONTENT)设置句子值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多