首页 > 解决方案 > 如何使用 NLP Java 提取主语动词对象?对于每一句话

问题描述

我想为每个句子找到一个主语、动词和宾语,然后将其传递给自然语言生成库simpleNLG以形成一个句子。

我尝试了多个库,例如Cornlp、opennlp、Standford 解析器。但我无法准确找到它们。

现在在最坏的情况下,我将不得不编写一长串 if-else 来查找每个句子的主语、动词和宾语,这对于 simpleNLG 并不总是准确的

像,

我尝试了词法解析器

LexicalizedParser lp = **new LexicalizedParser("englishPCFG.ser.gz");**
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
parse.pennPrint();
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.print(parse);

这给出了这个输出,

nsubj(use-2, I-1)
root(ROOT-0, use-2)
det(parser-4, a-3)
dobj(use-2, parser-4)

我想要这样的东西

subject = I
verb = use
det = a
object = parser

有没有更简单的方法可以在 JAVA 中找到它,或者我应该使用 if-else 吗?请帮帮我。

标签: stanford-nlpopennlppos-taggerpart-of-speechsimplenlg

解决方案


您可以使用openie注释器来获取三元组。您可以在命令行运行它或使用这些注释器构建管道。

命令:

java -Xmx10g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,depparse,natlog,openie -file example.txt

爪哇:

Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,depparse,natlog,openie");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation result = pipeline.process("..."); 

输入:

Joe ate some pizza.

输出:

Extracted the following Open IE triples:
1.0     Joe     ate     pizza

更多细节在这里:https ://stanfordnlp.github.io/CoreNLP/openie.html


推荐阅读