如何在java中使用StanfordCorenlp包提取三元组?
我想要一个代码片段,它可以使用java中的Stanford CoreNLP包输入一个句子或一组句子,并输出或提取三元组(主语、谓语和宾语)您是否在寻找OpenIE三元组,或更结构化的关系三元组(例如,对于如何在java中使用StanfordCorenlp包提取三元组?,java,stanford-nlp,triples,Java,Stanford Nlp,Triples,我想要一个代码片段,它可以使用java中的Stanford CoreNLP包输入一个句子或一组句子,并输出或提取三元组(主语、谓语和宾语)您是否在寻找OpenIE三元组,或更结构化的关系三元组(例如,对于per:city_of_birth)?对于前者,OpenIE系统很可能就是您想要的:。从那里的示例复制: import edu.stanford.nlp.ie.util.RelationTriple; import edu.stanford.nlp.simple.*; /** * A dem
per:city_of_birth
)?对于前者,OpenIE系统很可能就是您想要的:。从那里的示例复制:
import edu.stanford.nlp.ie.util.RelationTriple;
import edu.stanford.nlp.simple.*;
/**
* A demo illustrating how to call the OpenIE system programmatically.
*/
public class OpenIEDemo {
public static void main(String[] args) throws Exception {
// Create a CoreNLP document
Document doc = new Document("Obama was born in Hawaii. He is our president.");
// Iterate over the sentences in the document
for (Sentence sent : doc.sentences()) {
// Iterate over the triples in the sentence
for (RelationTriple triple : sent.openieTriples()) {
// Print the triple
System.out.println(triple.confidence + "\t" +
triple.subjectLemmaGloss() + "\t" +
triple.relationLemmaGloss() + "\t" +
triple.objectLemmaGloss());
}
}
}
}
或者,使用注释器API:
import edu.stanford.nlp.ie.util.RelationTriple;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.naturalli.NaturalLogicAnnotations;
import edu.stanford.nlp.util.CoreMap;
import java.util.Collection;
import java.util.Properties;
/**
* A demo illustrating how to call the OpenIE system programmatically.
*/
public class OpenIEDemo {
public static void main(String[] args) throws Exception {
// Create the Stanford CoreNLP pipeline
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// Annotate an example document.
Annotation doc = new Annotation("Obama was born in Hawaii. He is our president.");
pipeline.annotate(doc);
// Loop over sentences in the document
for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
// Get the OpenIE triples for the sentence
Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
// Print the triples
for (RelationTriple triple : triples) {
System.out.println(triple.confidence + "\t" +
triple.subjectLemmaGloss() + "\t" +
triple.relationLemmaGloss() + "\t" +
triple.objectLemmaGloss());
}
}
}
}
导入edu.stanford.nlp.ie.util.RelationTriple;
导入edu.stanford.nlp.ling.core注释;
导入edu.stanford.nlp.pipeline.Annotation;
导入edu.stanford.nlp.pipeline.StanfordCoreNLP;
导入edu.stanford.nlp.naturalli.naturallogicanotations;
导入edu.stanford.nlp.util.CoreMap;
导入java.util.Collection;
导入java.util.Properties;
/**
*演示如何以编程方式调用OpenIE系统。
*/
公共类OpenIEDemo{
公共静态void main(字符串[]args)引发异常{
//创建斯坦福CoreNLP管道
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、pos、引理、depparse、natlog、openie”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
//为示例文档添加注释。
注释doc=新注释(“奥巴马出生在夏威夷,他是我们的总统。”);
管道注释(doc);
//在文档中循环句子
for(CoreMap语句:doc.get(coreanotations.SentencesAnnotation.class)){
//获取句子的OpenIE三元组
集合三元组=句子.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
//打印三份
for(关系三重:三重){
System.out.println(三重信心+“\t”+
triple.subjectLemmaGloss()+“\t”+
triple.relationLemmaGloss()+“\t”+
triple.objectLemmaGloss());
}
}
}
}
您是在寻找OpenIE三元组,还是更多的结构化关系三元组(例如,对于per:city\u of\u Borth
)之类的东西?对于前者,OpenIE系统很可能就是您想要的:。从那里的示例复制:
import edu.stanford.nlp.ie.util.RelationTriple;
import edu.stanford.nlp.simple.*;
/**
* A demo illustrating how to call the OpenIE system programmatically.
*/
public class OpenIEDemo {
public static void main(String[] args) throws Exception {
// Create a CoreNLP document
Document doc = new Document("Obama was born in Hawaii. He is our president.");
// Iterate over the sentences in the document
for (Sentence sent : doc.sentences()) {
// Iterate over the triples in the sentence
for (RelationTriple triple : sent.openieTriples()) {
// Print the triple
System.out.println(triple.confidence + "\t" +
triple.subjectLemmaGloss() + "\t" +
triple.relationLemmaGloss() + "\t" +
triple.objectLemmaGloss());
}
}
}
}
或者,使用注释器API:
import edu.stanford.nlp.ie.util.RelationTriple;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.naturalli.NaturalLogicAnnotations;
import edu.stanford.nlp.util.CoreMap;
import java.util.Collection;
import java.util.Properties;
/**
* A demo illustrating how to call the OpenIE system programmatically.
*/
public class OpenIEDemo {
public static void main(String[] args) throws Exception {
// Create the Stanford CoreNLP pipeline
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// Annotate an example document.
Annotation doc = new Annotation("Obama was born in Hawaii. He is our president.");
pipeline.annotate(doc);
// Loop over sentences in the document
for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
// Get the OpenIE triples for the sentence
Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
// Print the triples
for (RelationTriple triple : triples) {
System.out.println(triple.confidence + "\t" +
triple.subjectLemmaGloss() + "\t" +
triple.relationLemmaGloss() + "\t" +
triple.objectLemmaGloss());
}
}
}
}
导入edu.stanford.nlp.ie.util.RelationTriple;
导入edu.stanford.nlp.ling.core注释;
导入edu.stanford.nlp.pipeline.Annotation;
导入edu.stanford.nlp.pipeline.StanfordCoreNLP;
导入edu.stanford.nlp.naturalli.naturallogicanotations;
导入edu.stanford.nlp.util.CoreMap;
导入java.util.Collection;
导入java.util.Properties;
/**
*演示如何以编程方式调用OpenIE系统。
*/
公共类OpenIEDemo{
公共静态void main(字符串[]args)引发异常{
//创建斯坦福CoreNLP管道
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、pos、引理、depparse、natlog、openie”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
//为示例文档添加注释。
注释doc=新注释(“奥巴马出生在夏威夷,他是我们的总统。”);
管道注释(doc);
//在文档中循环句子
for(CoreMap语句:doc.get(coreanotations.SentencesAnnotation.class)){
//获取句子的OpenIE三元组
集合三元组=句子.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
//打印三份
for(关系三重:三重){
System.out.println(三重信心+“\t”+
triple.subjectLemmaGloss()+“\t”+
triple.relationLemmaGloss()+“\t”+
triple.objectLemmaGloss());
}
}
}
}