我试图通过斯坦福解析器从给定文本中提取所有名词和形容词。
我当前的尝试是在 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());
}
}
顺便说一句,如果您想要的只是名词和动词等词性,则应该使用词性标注器,例如斯坦福词性标注器。它将运行速度提高几个数量级,并且至少同样准确。
但你可以用解析器来做到这一点。你想要的方法是taggedYield()
它返回一个List<TaggedWord>
。所以你有了
List<TaggedWord> 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 开头。您可以更普遍地检查一组标签中的成员资格。)
附注使用 stanford-nlp 标签最适合 stackoverflow 上的斯坦福 NLP 工具。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)