通过斯坦福解析器提取所有名词,形容词forms和文本

我试图通过斯坦福解析器从给定的文本中提取所有名词和形容词。

我目前的尝试是在Tree-Object的getChildrenAsList()中使用模式匹配来定位如下内容:

(NN paper), (NN algorithm), (NN information), ... 

并将它们保存在一个数组中。

输入句子:

在本文中,我们提出了一种从任意文本中提取语义信息的算法。

结果 – 字符串:

 [(S (PP (IN In) (NP (DT this) (NN paper))) (NP (PRP we)) (VP (VBP present) (NP (NP (DT an) (NN algorithm)) (SBAR (WHNP (WDT that)) (S (VP (VBD extracts) (NP (JJ semantic) (NN information)) (PP (IN from) (NP (DT an) (ADJP (JJ arbitrary)) (NN text)))))))) (. .))] 

我尝试使用模式匹配,因为我无法在斯坦福解析器中找到返回所有单词类的方法,例如名词。

有没有更好的方法来提取这些单词类或解析器提供特定的方法?

 public static void main(String[] args) { String str = "In this paper we present an algorithm that extracts semantic information from an arbitrary text."; LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz"); Tree parseS = (Tree) lp.apply(str); System.out.println("tr.getChildrenAsList().toString()"+ parseS.getChildrenAsList().toString()); } } 

顺便说一句,如果您想要的只是名词和动词等词性,您应该使用词性标注器,例如Stanford POS标签器。 它会更快地运行几个数量级,并且至少是准确的。

但你可以用解析器来做。 你想要的方法是taggedYield() ,它返回一个List 。 所以你有了

 List taggedWords = (Tree) lp.apply(str); for (TaggedWord tw : taggedWords) { if (tw.tag().startsWith("N") || tw.tag().startsWith("J")) { System.out.printf("%s/%s%n", tw.word(), tw.tag()); } } 

(这种方法会削减一个角落,因为知道所有且只有形容词和名词标签在Penn树库标签集中以J或N开头。您可以更一般地检查一组标签中的成员资格。)

ps使用标签stanford-nlp最适合stackoverflow上的Stanford NLP工具。

我确定你会注意到nltk(自然语言工具包)只需安装这个python库以及maxent pos tagger以及下面的代码就可以了。 标签器已在Penn上接受过培训,因此标签没有区别。 上面的代码不是,但我喜欢nltk,因此。

  import nltk nouns=[] adj=[] #read the text into the variable "text" text = nltk.word_tokenize(text) tagged=nltk.pos_tag(text) for i in tagged: if i[1][0]=="N": nouns+=[i[0]] elif i[1][0]=="J": adj+=[i[0]]