2017-04-25 3 views
0

私は特定の依存関係を含むフレーズを取得しようとしています。例えば、私は、たとえばなど件名が含まれている名詞句、同格として動作する名詞句を、欲しい:依存関係からフレーズ

再び
   Sentence: John Smith and Robert Alan Jones ate the warm pizza and cold salad by the car for an hour. 
     Phrasal Subject: John Smith and Robert Alan Jones 
       Negation: 
        Verbs: ate 
    Phrasal Direct Object: the warm pizza and cold salad 
Phrasal Indirect Object: 
       Root Noun: 
     Phrasal Root Noun: 
     Phrasal Appositive: 
Phrasal Subject Complement: 
Phrasal Object Complement: 
Phrasal Clausal Complement: 
     Adjective Phrase: warm 
     Adverbial Phrase: 
    Prepositional Phrases: [by the car, for an hour] 

- 私は、依存関係パーサを使用しています。私は、TypedDependencyコレクションを再帰的にナビゲートするためのコードをいくつか書いたが、それはハッキリと感じる。依存関係からフレーズや単語の組み合わせ(MWE、POSSなど)を返すための組み込みの方法はありますか? Jeff

答えて

0

OpenIEシステムは、このようなトリプルを得るのに適していると思います。

ここには私が書いた基本的な例がありますが、より良い方法があるかもしれません。 containsNounPhraseメソッドはTreeに追加するのに便利です。また、これをStanford CoreNLP 3.8.0リリースに追加することもできます。

package edu.stanford.nlp.examples; 

import edu.stanford.nlp.ling.*; 
import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.semgraph.*; 
import edu.stanford.nlp.trees.*; 
import edu.stanford.nlp.util.*; 

import java.util.*; 

public class PhraseDependencyExample { 

    public static Tree containingNounPhrase(Tree tree, Tree leaf) { 
    Tree currTree = leaf; 
    Tree largestNPTree = null; 
    while (currTree != null) { 
     if (currTree.label().value().equals("NP")) 
     largestNPTree = currTree; 
     currTree = currTree.parent(tree); 
    } 
    return largestNPTree; 
    } 

    public static void main(String[] args) { 
    // set up pipeline properties 
    Properties props = new Properties(); 
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse"); 
    // use faster shift reduce parser 
    props.setProperty("parse.model", "edu/stanford/nlp/models/srparser/englishSR.ser.gz"); 
    props.setProperty("parse.maxlen", "100"); 
    // set up Stanford CoreNLP pipeline 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
    // build annotation for a review 
    Annotation annotation = new Annotation("John Smith and Robert Alan Jones ate the warm pizza and cold salad."); 
    // annotate the review 
    pipeline.annotate(annotation); 
    for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) { 
     System.err.println("---"); 
     Tree sentenceConstituencyParse = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
     System.err.println(sentenceConstituencyParse); 
     SemanticGraph sg = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class); 
     for (IndexedWord iw : sg.vertexListSorted()) { 
     if (iw.tag().equals("VBD")) { 
      System.err.println("---"); 
      System.err.println("verb: "+iw.word()); 
      for (SemanticGraphEdge sge : sg.outgoingEdgeList(iw)) { 
      if (sge.getRelation().getShortName().equals("dobj") || sge.getRelation().getShortName().equals("nsubj")) { 
       int tokenIndex = sge.getDependent().backingLabel().index()-1; 
       String fullPhrase = containingNounPhrase(sentenceConstituencyParse, 
        sentenceConstituencyParse.getLeaves().get(tokenIndex)).yieldWords().toString(); 
       System.err.println("\t"+sge.getRelation() + " --> "+fullPhrase); 
      } 
      } 
     } 
     } 
    } 
    } 
} 
  1. このコードは、選挙の木の葉や依存解析グラフの頂点を取得する方法を示しています。

  2. 単語を含む最大の名詞句を取得するように設定されていますが、最小などを得るためにそれを変更することができ...

関連する問題