【问题标题】:collpased dependencies with stanford core nlp与 stanford corenlp 的依赖关系崩溃
【发布时间】:2016-05-04 21:32:59
【问题描述】:

我用谷歌搜索了很多次,但我找不到答案。而且我仍然尝试使用类型化的依赖项,但这还不足以获得解决方案。 我想使用 stanford core nlp v.3.0 提取句子的折叠依赖项。 但是我每次都无法输入依赖项示例和演示。如果有人使用此 api 帮助在句子中使用折叠的依赖项,我将非常感激。

我正在使用 java 并且类型化的依赖项对于我的项目来说是不够的。任何形式的建议和参考也很好。

【问题讨论】:

    标签: java nlp stanford-nlp


    【解决方案1】:

    很简单。

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

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

    【讨论】:

    • 它没有给出根元素。如何获得?
    • 可以通过dependencies1.getFirstRoot()方法获取根元素(我这里用的是coreNLP 1.3.5)
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多