Stanford Parser:如何提取依赖关系

时间:2017-01-24 02:10:44

标签: nlp stanford-nlp stanford-parser

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

        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

显示相同内容时

我看到了

enter image description here

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

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

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

    • 足球在哪里? on the grass pitch
    • 足球的其他任何属性? which is 50% whitepressure flow of 1
  3. 我应该如何导航树以提取这些依赖项?有没有办法让我从nsubj节点开始并转到所有依赖项?

0 个答案:

没有答案