【问题标题】:Get GrammaticalRelation of root node获取根节点的语法关系
【发布时间】:2019-02-18 01:41:58
【问题描述】:

我正在尝试使用斯坦福 NLP 解析医学研究报告。我可以获得除第一个或根节点之外的所有节点的 GrammaticalRelation。我如何获得这个值。

我写了一个java程序,它通过获取依赖图来解析报告,并且可以获取除根节点之外的所有节点的子对。

    public void DocAnnotationParse(String Input_text) {
    Annotation document = new Annotation(Input_text);
    Properties props = new Properties();
    //props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse");
    props.setProperty("annotators", "tokenize,ssplit,pos,parse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    pipeline.annotate(document);
    int sentNum = 0;
    Map<String, Map<String, Map<String,IndexedWord>>> sentMap = new LinkedHashMap<>(); // A map contains maps of each sentence
    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
        SemanticGraph dependencyParse = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
        IndexedWord firstVertex = dependencyParse.getFirstRoot();
        Map<String, Map<String,IndexedWord>> outterMap = new LinkedHashMap<>();
        RecursiveChild(outterMap, dependencyParse, firstVertex, 0);
        sentMap.put(Integer.toString(++sentNum), outterMap);
        logger.debug("outtermap: "+outterMap);
    }
    logger.debug("all sentMaps: "+sentMap);
    PrettyPrintBySentence(sentMap);
}


public void RecursiveChild(Map<String, Map<String, IndexedWord>> outterMap,
        SemanticGraph dependencyParse, 
        IndexedWord vertex, int hierLevel) {

    Map<String, IndexedWord> pairMap = new LinkedHashMap<>();
    pairMap.put("Root", vertex);
    List<IndexedWord>indxwdsL = dependencyParse.getChildList(vertex);
    List<Pair<GrammaticalRelation,IndexedWord>>childPairs = dependencyParse.childPairs(vertex);
    List<IndexedWord> nxtLevalAL = new ArrayList<>();
    if(!indxwdsL.isEmpty()) {
        ++hierLevel;    
        for(Pair<GrammaticalRelation, IndexedWord> aPair : childPairs) { //at level hierLevel x
            logger.debug(aPair);
            String grammRel = aPair.first.toString(); //Gramatic Relation
            IndexedWord indxwd = aPair.second;
            pairMap.put(grammRel, indxwd);
            List<Pair<GrammaticalRelation,IndexedWord>>childPairs2 = dependencyParse.childPairs(indxwd);
            if(!childPairs2.isEmpty()) {
                nxtLevalAL.add(indxwd);
            }
        }
    }
    String level = Integer.toString(hierLevel);     
    outterMap.put(level, pairMap);
    //Go to each lower level
    for(IndexedWord nxtIwd : nxtLevalAL) {
        RecursiveChild(outterMap, dependencyParse, nxtIwd, hierLevel);
    }
}

根顶点的 childPair 不包含我想要的语法关系。查看依赖关系图没有任何价值,只有字符串根。如何获得该节点的语法关系。例如简单的句子“我喜欢炸薯条”。给出图表:

-> love/VBP (root)
  -> I/PRP (nsubj)
  -> fries/NNS (dobj)
    -> French/JJ (amod)
  -> ./. (punct)

【问题讨论】:

    标签: parsing stanford-nlp


    【解决方案1】:

    您好,我不是语言学人士,但我的理解是在 SemanticGraph 之外只有一个 ROOT 节点,而 root 边缘从根指向句子。

    因此,在您的示例中,ROOT 节点通过root 关系附加到单词love

    如果您查看 SemanticGraph 的代码,它会明确指出:

    * The root is not at present represented as a vertex in the graph.
    * At present you need to get a root/roots
    * from the separate roots variable and to know about it.
    

    您可以使用getRoots() 方法访问根列表(我猜可能不止一个?)。但我认为这意味着root 边缘从ROOT 节点流入这些单词。

    如果您想要一个实际的 Java 对象而不是字符串来表示它,那么 edu.stanford.nlp.trees.GrammaticalRelation.ROOT 表示“伪造的 ROOT 节点”和根之间的这种关系。

      /**
       *  The "root" grammatical relation between a faked "ROOT" node, and the root of the sentence.
       */
      public static final GrammaticalRelation ROOT =
        new GrammaticalRelation(Language.Any, "root", "root", null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多