1

I have googled many times but i can't find the answer. And i still tried with typed dependencies but it was not enough to get the solution. I wanted to extract collapsed dependency of a sentence using stanford core nlp v.3.0. But i was unable and every time i get typed dependencies example and demos. I would be very thankful if anyone help to get collapsed Dependencies with in a sentence using this api.

I am using java and typed dependencies is not enough for my project. Any kind of advice and reference is also good.

4

2 回答 2

7

它非常简单。

使用文本执行管道后,导入以下包 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);

希望这能解决您的问题...

于 2013-11-08T04:52:16.657 回答
1

一旦你有一个解析句子的注释(如在其他示例中),你想要这样做(使用一些额外的代码来检查句子列表是否为非空等):

List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
CoreMap sentence = (CoreMap) sentences.get(0);
SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
System.out.println(graph.toString("plain"));
于 2013-10-20T00:03:19.970 回答