上一個答案在正確的情況下迭代瞭解析樹中的所有節點。
(我使用番石榴的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(順便提一下,這是非常有用的) 。 –