它非常简单。
使用文本执行管道后,导入以下包 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);
希望这能解决您的问题...