pom.xml引入jar
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
<classifier>models</classifier>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
<classifier>models-chinese</classifier>
</dependency>
加载模型和初始化
// set up pipeline properties
Properties props = new Properties();
// set the list of annotators to run
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,depparse,coref,kbp,quote");
// set a property for an annotator, in this case the coref annotator is being set to use the neural algorithm
props.setProperty("coref.algorithm", "neural");
// build pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
关系抽取
只适用于英文分析
String text = "Joe Smith was born in California. " +
"In 2017, he went to Paris, France in the summer. " +
"His flight left at 3:00pm on July 10th, 2017. " +
"After eating some escargot for the first time, Joe said, \"That was delicious!\" " +
"He sent a postcard to his sister Jane Smith,He beat his daughter Tom. " +
"After hearing about Joe's trip, Jane decided she might go to France one day.";
// build pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// create a document object
CoreDocument document = new CoreDocument(text);
// annnotate the document
pipeline.annotate(document);
// text of the sentence
List<CoreSentence> sentences = document.sentences();
System.out.println("-----------分句-------------");
for (int i =0;i<sentences.size();i++) {
CoreSentence sentence = sentences.get(i);
System.out.println(sentence.text());
System.out.println("************************");
List<RelationTriple> relationTriples = sentence.relations();
for (int j = 0; j < relationTriples.size(); j++) {
RelationTriple relationTriple = relationTriples.get(j);
System.out.println("----------------------------");
System.out.println("confidence : " +relationTriple.confidence);
System.out.println("relation : " + relationTriple.relation);
System.out.println("canonicalObject : "+relationTriple.canonicalObject);
System.out.println("canonicalSubject : "+relationTriple.canonicalSubject);
System.out.println("object : " +relationTriple.object);
System.out.println("subject : "+relationTriple.subject);
System.out.println(relationTriple.allTokens());
System.out.println(relationTriple);
/**
* He sent a postcard to his sister Jane Smith,He beat his daughter Tom.
* confidence : 1.0
* relation : [per:children]
* canonicalObject : [Tom-15]
* canonicalSubject : [Joe-1, Smith-2]
* object : [Tom-15]
* subject : [his-13]
* [Joe-1, Smith-2, per:children, Tom-15]
* 1.0 Joe Smith per:children Tom
*/
List<CoreLabel> coreLabels = relationTriple.relation;
for (int k = 0; k < coreLabels.size(); k++) {
System.out.println(coreLabels.get(k).word());
}
System.out.println("///");
}
}
-----------分词-------------
Joe Smith was born in California . In 2017 , he went to Paris , France in the summer . His flight left at 3:00 pm on July 10th , 2017 . After eating some escargot for the first time , Joe said , `` That was delicious ! '' He sent a postcard to his sister Jane Smith , He beat his daughter Tom . After hearing about Joe 's trip , Jane decided she might go to France one day . -----------分句-------------
Joe Smith was born in California.
************************
----------------------------
confidence : 1.0
relation : [per:stateorprovince_of_birth]
canonicalObject : [California-6]
canonicalSubject : [Joe-1, Smith-2]
object : [California-6]
subject : [Joe-1, Smith-2]
[Joe-1, Smith-2, per:stateorprovince_of_birth, California-6]
1.0 Joe Smith per:stateorprovince_of_birth California
per:stateorprovince_of_birth
///
In 2017, he went to Paris, France in the summer.
************************
His flight left at 3:00pm on July 10th, 2017.
************************
After eating some escargot for the first time, Joe said, "That was delicious!"
************************
He sent a postcard to his sister Jane Smith,He beat his daughter Tom.
************************
----------------------------
confidence : 1.0
relation : [per:children]
canonicalObject : [Tom-15]
canonicalSubject : [Joe-1, Smith-2]
object : [Tom-15]
subject : [his-13]
[Joe-1, Smith-2, per:children, Tom-15]
1.0 Joe Smith per:children Tom
per:children
///
----------------------------
confidence : 0.5521205339992503
relation : [per:children]
canonicalObject : [Tom-15]
canonicalSubject : [Jane-8, Smith-9]
object : [Tom-15]
subject : [Jane-8, Smith-9]
[Jane-8, Smith-9, per:children, Tom-15]
0.5521205339992503 Jane Smith per:children Tom
per:children
///
----------------------------
confidence : 1.0
relation : [per:siblings]
canonicalObject : [Joe-1, Smith-2]
canonicalSubject : [Jane-8, Smith-9]
object : [his-6]
subject : [Jane-8, Smith-9]
[Jane-8, Smith-9, per:siblings, Joe-1, Smith-2]
1.0 Jane Smith per:siblings Joe Smith
per:siblings
///
----------------------------
confidence : 1.0
relation : [per:siblings]
canonicalObject : [Jane-8, Smith-9]
canonicalSubject : [Joe-1, Smith-2]
object : [Jane-8, Smith-9]
subject : [his-6]
[Joe-1, Smith-2, per:siblings, Jane-8, Smith-9]
1.0 Joe Smith per:siblings Jane Smith
per:siblings
///
----------------------------
confidence : 1.0
relation : [per:parents]
canonicalObject : [Joe-1, Smith-2]
canonicalSubject : [Tom-15]
object : [his-13]
subject : [Tom-15]
[Tom-15, per:parents, Joe-1, Smith-2]
1.0 Tom per:parents Joe Smith
per:parents
///
After hearing about Joe's trip, Jane decided she might go to France one day.
************************