2017-08-07 19 views
0

私は現在、件名とオブジェクトタイプのみNameEntitiesが含まれている関係トリプル(主語、述語、オブジェクト)のためCoreNLPオープン情報抽出(OpenIE)で検索しています。しかし、List<CoreMap>のオブジェクトRelationTripleのエンティティタイプを取得する方法はわかりません。私は助けを感謝されるRelationTripleクラスからエンティティタイプを取得するためのいくつかの方法が存在する場合スタンフォードCoreNLP:Name Entities(OpenIE)のみでRelationTripleトリプルを取得するには?

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) { 
     // Here is where I get the entity type from a triple's subject or object 
     System.out.println(triple.confidence + "\t" + 
      triple.subjectLemmaGloss() + "\t" + 
      triple.relationLemmaGloss() + "\t" + 
      triple.objectLemmaGloss()); 
     } 
    } 
    } 
} 

は以下https://stanfordnlp.github.io/CoreNLP/openie.htmlからのコードです。

答えて

0

subjectobjectのインスタンス変数は、#ner()メソッドを使用して名前付きエンティティ情報が添付されたCoreLablのリストである必要があります。以下のようなものは、あなたが望むことをする必要があります:

Collection<RelationTriple> triples = sentence.get(RelationTriplesAnnotation.class); 
List<RelationTriple> withNE = triples.stream() 
    // make sure the subject is entirely named entities 
    .filter(triple -> 
     triple.subject.stream().noneMatch(token -> "O".equals(token.ner()))) 
    // make sure the object is entirely named entities 
    .filter(triple -> 
     triple.object.stream().noneMatch(token -> "O".equals(token.ner()))) 
    // Convert the stream back to a list 
    .collect(Collectors.toList()); 
+0

私が望むのは、エンティティの**ラベル**を取得することです。たとえば、「テンサー** [PERSON] **は[NR]ブラジル** [LOCATION] **の審査員でした。 RelationTripleクラスは、ラベルではなくタイプの光沢しか持たない。 –

+0

トリプルのトークンの.ner関数は、PERSONのような型を与えるはずです。 –

関連する問題