2016-03-06 110 views
0

我正在嘗試使用CoreNLP,並且想知道是否可以使用短語級別拆分句子,而不用詳細說明單詞級別。Stanford CoreNLP - 短語級別

基本上,我想分析一個句子,然後獲取它的短語標記,然後獲得分裂成變量。例如,對於一個句子,如果它包含名詞短語(X)和動詞短語(Y),我想用CoreNLP分析X和Y,然後將X和Y分別獲得到變量中。

任何想法如何做到這一點?

回答

1

有我的回答一些示例代碼,這個問題它演示瞭如何訪問選區解析

我提供了一個名爲RootFinderExample.java樣本類。

How to get the root node in Stanford Parse-Tree?

這裏就是樹被訪問:

Tree tree = sentence.get(TreeAnnotation.class); 

這裏是樹類的一些文檔:

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/Tree.html

+0

謝謝!我試用了示例中的代碼,它工作。現在我試圖訪問根的POS標記和樹中的單詞。在您提供的示例代碼中,我試圖獲取您提供的示例代碼,http://stackoverflow.com/questions/34928739/how-to-get-the-root-node-in-stanford-parse-tree/35012082#35012082 我試圖獲得「沃斯瓶裝水」的POS標籤和POS標籤(即NP)。你能幫我嗎? – Tharindu

0

下面的代碼所有NP和VP在給定的文字中:

props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse"); 
    props.setProperty("ner.model","edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz"); 
    List<mention> NPS = new ArrayList<mention>(); 

    HeadFinder headFinder = null; 
    if (doc != null && doc.length() > 0) { 
    edu.stanford.nlp.pipeline.Annotation annotation = pipeline.process(doc); 
     for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) { 
      Tree tree = sentence.get(TreeAnnotation.class); 
      headFinder = new SemanticHeadFinder(); 
      for (Tree subtree : tree) { 
       if (subtree.label().value().equals("NP")) { 
       //this is a NP, you can assign it to a variable here 
       } 

       if (subtree.label().value().equals("VP")) { 
       //this is a VP, you can assign it to a variable here 
       } 

       } 
       } 
       } 
相關問題