簡體   English   中英

使用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獨立的共指解析系統,我將獲得正確的結果。 因此,完整的獨立共指解析系統唯一缺少的部分是能夠根據標記注釋創建句子注釋。

有一個帶List<CoreMap> sentences參數的Annotation 構造函數 ,如果您有一個已標記化的句子的列表,它將設置文檔。

您要為每個句子創建一個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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM