上一个答案在正确的情况下迭代了解析树中的所有节点。
(我使用番石榴的Function
在代码中一点点额外的优雅,但:虽然是返回POS标签数没有现成的方法,你可以使用在edu.stanford.nlp.trees.Trees
类方法叶节点如下直接得到一个简单的for循环将工作一样好。)
Tree tree = sentence.get(TreeAnnotation.class); // parse tree of the sentence
List<CoreLabel> labels = Trees.taggedLeafLabels(tree); // returns the labels of the leaves in a Tree, augmented with POS tags.
List<String> tags = Lists.transform(labels, getPOSTag);
for (String tag : tags)
Collections.frequency(tags, tag);
其中
Function<CoreLabel, String> getPOSTag = new Function<CoreLabel, String>() {
public String apply(CoreLabel core_label) { return core_label.get(PartOfSpeechAnnotation.class); }
};
优雅的回答,真的! –
感谢您的帮助。对不起,如果这是显而易见的,但这意味着创建一个Java模块呢?目前我刚刚在终端命令行中运行它(例如,java -mx200m edu.stanford.nlp.parser.lexparser.LexicalizedParser -retainTMPSubcategories -outputFormat“wordsAndTags,penn,typedDependencies”englishPCFG.ser.gz mumbai.txt) – JRUK
是的,你需要你自己的代码。尽管斯坦福大学NLP的作者为CLI的使用提供了很大的灵活性,但除了解析结果的直接输出之外,您通常需要使用他们的API(顺便提一下,这是非常有用的) 。 –