2014-04-28 28 views
0

我使用斯坦福解析器得到從文本的依賴關係,一句一句是這樣的: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) 

有沒有辦法找出誰是「它」,以獲得顯示它實際上是房間的東西?

回答

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 
+0

我不使用這種命令... –

0

這裏是斯坦福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); 
} 

} 
0

使用StanfordCoreNlpDemo.java在下載CoreNLP的拉鍊

相關問題