自然语言处理 -- 关系提取(用于关系图谱建立)

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.
************************
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值