我想使用斯坦福解析器在给定句子中查找多个名词短语。我正在使用 Java。
例句:
画质真的很好。
现在我需要提取“图片质量”。
有没有办法遍历依赖树来达到想要的结果?
此外,斯坦福解析器可以标记 XML 格式的句子吗?
我想使用斯坦福解析器在给定句子中查找多个名词短语。我正在使用 Java。
例句:
画质真的很好。
现在我需要提取“图片质量”。
有没有办法遍历依赖树来达到想要的结果?
此外,斯坦福解析器可以标记 XML 格式的句子吗?
如果您想查找所有名词短语,那么通过使用短语结构解析树而不是依赖关系表示可能最容易做到这一点。您可以手动遍历 Tree 对象的节点并查看 label().value() 是否为“NP”,或者您可以使用“@NP”的 TregexPattern,然后使用 TregexMatcher 遍历 NP。
您可以使用以下命令行标志从解析器获取 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;
}