0

我想使用斯坦福解析器提取句子中的所有依赖项。考虑以下代码

        LexicalizedParser lp = LexicalizedParser.loadModel();
        lp.setOptionFlags(new String[] { "-maxLength", "80", "-retainTmpSubcategories" });
        String[] sent = "There is a football on the grass pitch which is 50% white with a pressure flow of 1".split(" ");
        List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
        Tree parse = lp.apply(rawWords);
        parse.pennPrint();
        System.out.println();

        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
        List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
        System.out.println(tdl);
        TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
        tp.printTree(parse);

        Collection<TypedDependency> td = gs.typedDependenciesCollapsed();
        //System.out.println(td);

        Object[] list = td.toArray();
        System.out.println(list.length);
        TypedDependency typedDependency;
        for (Object object : list) {
            typedDependency = (TypedDependency) object;
            System.out.println(
                    "Depdency Name " + typedDependency.dep().toString() + " :: " + typedDependency.reln());
            if (typedDependency.reln().getShortName().equals("something")) {
                // your code
            }
        }

所以这使我可以将依赖项视为

Depdency Name There/EX :: expl
Depdency Name is/VBZ :: root
Depdency Name a/DT :: det
Depdency Name football/NN :: nsubj
Depdency Name on/IN :: case
Depdency Name the/DT :: det
Depdency Name grass/NN :: compound
Depdency Name pitch/NN :: nmod:on
Depdency Name which/WDT :: nsubj
Depdency Name is/VBZ :: cop
Depdency Name 50%/CD :: nummod
Depdency Name white/JJ :: amod
Depdency Name with/IN :: case
Depdency Name a/DT :: det
Depdency Name pressure/NN :: nmod:with
Depdency Name flow/NN :: acl:relcl
Depdency Name of/IN :: case
Depdency Name 1/CD :: nmod:of

现在,当我在http://nlp.stanford.edu:8080/corenlp/process

我明白

在此处输入图像描述

我如何提取依赖于的所有内容nsubj?例如在这种情况下,

  1. 我想知道sunbject是什么。这很容易,因为我只查找节点名称nsubj

  2. 现在我也想要以下信息

    • 足球在哪里?on the grass pitch
    • 足球还有其他属性吗?which is 50% whitepressure flow of 1

我应该如何导航树以提取这些依赖项?有没有办法让我从nsubj节点开始并转到所有依赖项?

4

0 回答 0