NLP - 我可以找出谁"它"是什么?

时间:2014-04-28 11:02:30

标签: java nlp stanford-nlp

我正在使用斯坦福分析器从文本中逐句获取依赖关系,如下所示:

    Reader reader = new StringReader("The room was not nice. It was bright, but cold.");
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

    // the dependencies of the entire text
    List<TypedDependency> textDependencies = new ArrayList<TypedDependency>();
    // get the dependencies of each sentence and add it to the list
    for (List<HasWord> sentence : new DocumentPreprocessor(reader)) {
        Tree parse = lp.apply(sentence);
        GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
        textDependencies.addAll(gs.typedDependenciesCCprocessed());
    }

运行上面的代码后,名为textDependencies的列表将包含以下依赖项:

    det(room-2, The-1)
    nsubj(nice-5, room-2)
    cop(nice-5, was-3)
    neg(nice-5, not-4)
    root(ROOT-0, nice-5)
    nsubj(warm-3, It-1)
    nsubj(noisy-6, It-1)
    cop(warm-3, was-2)
    root(ROOT-0, warm-3)
    conj_but(warm-3, noisy-6)

有没有办法找出谁&#34;它&#34;是,为了得到一些东西显示它实际上是房间?

3 个答案:

答案 0 :(得分:1)

您想要的是共同分辨率。斯坦福CoreNLP does that already。我无法以编程方式找到它的演示,但如果您正在运行预编译的可执行文件,则需要将dcoref添加到注释器列表中,如下所示:

java -cp <all jars> edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,parse,dcoref -file input.txt

答案 1 :(得分:0)

以下是Stanford CoreNLP共指解析的Java代码示例(由mbatchkarov建议):

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefChain.CorefMention;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations.CorefChainAnnotation;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;

public class StanfordExample {

protected StanfordCoreNLP pipeline;

public StanfordExample() {

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

    this.pipeline = new StanfordCoreNLP(props);
}

public void getCoref(String documentText)
{
    Annotation document = new Annotation(documentText);
    this.pipeline.annotate(document);
    Map<Integer, CorefChain> sentences = document.get(CorefChainAnnotation.class);
    for(CorefChain chain : sentences.values()) 
    {
        List<CorefMention> mentionsInTextualOrder = chain.getMentionsInTextualOrder();
        for (CorefMention corefMention : mentionsInTextualOrder)
        {
            System.out.println(corefMention.toString());
        }
    }
}


public static void main(String[] args) {
    String text = "The room was not nice. It was bright, but cold.";
    StanfordExample slem = new StanfordExample();
    slem.getCoref(text);
}

}

答案 2 :(得分:0)

在下载的CoreNLP压缩文件中使用StanfordCoreNlpDemo.java