2017-05-30 17 views

答えて

0

にスタンフォードCoreNLPパッケージを使用してトリプル(主語、述語およびオブジェクト)を抽出するコードスニペットをしたいです関係トリプル(例えば、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()); 
     } 
    } 
    } 
} 
関連する問題