与stanford corenlp崩溃的依赖关系

时间:2013-10-17 02:43:59

标签: java nlp stanford-nlp

我已多次搜索,但我找不到答案。我仍然尝试使用类型化的依赖项,但这还不足以获得解决方案。 我想用stanford core nlp v.3.0提取一个句子的折叠依赖。 但我无法,每次我得到类型依赖示例和演示。如果有人帮助使用这个API在一个句子中使用折叠的依赖关系,我将非常感激。

我使用java和类型化的依赖项对我的项目来说还不够。任何建议和参考也都很好。

2 个答案:

答案 0 :(得分: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 dependecies。

SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
SemanticGraph dependencies2 = sentence.get(BasicDependenciesAnnotation.class);

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

答案 1 :(得分:1)

一旦你有一个Annotation来解析一个句子(就像在其他例子中一样),你想要这样做(用一些额外的代码来检查句子列表是非空的等等):

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