とてもシンプルです。
テキストでパイプラインを実行した後、以下のパッケージ edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation; をインポートします。
以下のスクリプトで...
// create an empty Annotation just with desired text
Annotation document = new Annotation(text);
// run all Annotators on this text
pipeline.annotate(document);
//for each sentence you can get a list of collapsed dependencies as follows
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
SemanticGraph dependencies1 = sentence.get(CollapsedDependenciesAnnotation.class);
dep_type = "CollapsedDependenciesAnnotation";
System.out.println(dep_type+" ===>>");
System.out.println("Sentence: "+sentence.toString());
System.out.println("DEPENDENCIES: "+dependencies1.toList());
System.out.println("DEPENDENCIES SIZE: "+dependencies1.size());
Set<SemanticGraphEdge> edge_set1 = dependencies1.getEdgeSet();
int j=0;
for(SemanticGraphEdge edge : edge_set1){
j++;
System.out.println("------EDGE DEPENDENCY: "+j);
Iterator<SemanticGraphEdge> it = edge_set1.iterator();
IndexedWord dep = edge.getDependent();
String dependent = dep.word();
int dependent_index = dep.index();
IndexedWord gov = edge.getGovernor();
String governor = gov.word();
int governor_index = gov.index();
GrammaticalRelation relation = edge.getRelation();
System.out.println("No:"+j+" Relation: "+relation.toString()+" Dependent ID: "+dependent.index()+" Dependent: "+dependent.toString()+" Governor ID: "+governor.index()+" Governor: "+governor.toString());
}
同様に、次のように行を変更することで、基本的な依存関係と ccprocessed 依存関係を取得できます。
SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
SemanticGraph dependencies2 = sentence.get(BasicDependenciesAnnotation.class);
これで問題が解決することを願っています...