【问题标题】:How to extract triples using Stanford CoreNLP package in java?如何在 Java 中使用斯坦福 CoreNLP 包提取三元组?
【发布时间】:2017-05-30 09:43:26
【问题描述】:

我想要一个代码 sn-p,它将输入一个句子或一组句子,并使用 java 中的 Stanford CoreNLP 包输出或提取三元组(主语、谓语和宾语)

【问题讨论】:

    标签: java stanford-nlp triples


    【解决方案1】:

    您是否在寻找 OpenIE 三元组,或更结构化的关系三元组(例如,per:city_of_birth)?对于前者,OpenIE 系统很可能是您正在寻找的:https://stanfordnlp.github.io/CoreNLP/openie.html。从那里的示例复制:

    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());
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多