2013-10-17 50 views
1

我搜索了很多次,但找不到答案。而且我仍然尝试使用类型化的依赖关系,但它不足以获得解决方案。 我想用stanford core nlp v.3.0来提取句子的折叠依赖关系。 但我无法和每次我得到打字的依赖关系的例子和演示。我会非常感谢有人帮助崩溃依赖在一个句子中使用这个api。与斯坦福核心依存关系nlp

我正在使用java和输入的依赖关系对我的项目是不够的。任何建议和参考也很好。

回答

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")); 
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); 

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

+0

它不给根元素。如何获得? – alienCoder

+0

你可以通过dependencies1.getFirstRoot()方法获得根元素(我在这里使用coreNLP 1.3.5) –

+0

谢谢。但我已经解决了它。 :) – alienCoder