Java 通过stanford语法分析器查找名词短语

Java 通过stanford语法分析器查找名词短语,java,parsing,stanford-nlp,Java,Parsing,Stanford Nlp,我想使用斯坦福语法分析器在给定的句子中找到多个名词短语。我正在使用Java 例句: 画质真的很好 现在我需要提取图片质量 有没有办法遍历依赖关系树以获得所需的结果? 此外,stanford parser能否以XML格式标记句子?如果您想查找所有名词短语,那么使用短语结构解析树而不是依赖项表示法可能最容易做到这一点。您可以手动遍历树对象的节点并查看label.value是否为NP,也可以使用@NP的TregexPattern,然后使用TregexMatcher遍历NPs 您可以使用的命令行标志从解

我想使用斯坦福语法分析器在给定的句子中找到多个名词短语。我正在使用Java

例句:

画质真的很好

现在我需要提取图片质量

有没有办法遍历依赖关系树以获得所需的结果?
此外,stanford parser能否以XML格式标记句子?

如果您想查找所有名词短语,那么使用短语结构解析树而不是依赖项表示法可能最容易做到这一点。您可以手动遍历树对象的节点并查看label.value是否为NP,也可以使用@NP的TregexPattern,然后使用TregexMatcher遍历NPs

您可以使用的命令行标志从解析器获取XML格式输出

-outputFormatOptions xml

或者在代码中,通过使用xml的选项字符串构造TreePrint对象。

仅扩展@christopher manning的答案,下面是一些您可以使用的代码:

private List<String> getNounPhrases(Tree parse) {
    List<String> result = new ArrayList<>();
    TregexPattern pattern = TregexPattern.compile("@NP");
    TregexMatcher matcher = pattern.matcher(parse);
    while (matcher.find()) {
        Tree match = matcher.getMatch();
        List<Tree> leaves = match.getLeaves();
        System.out.println(leaves);
        // Some Guava magic.
        String nounPhrase = Joiner.on(' ').join(Lists.transform(leaves, Functions.toStringFunction()));
        result.add(nounPhrase);
        List<LabeledWord> labeledYield = match.labeledYield();
        System.out.println("labeledYield: " + labeledYield);
    }
    return result;
}