使用RDF API(Jena,OpenRDF或Protege)转换OpenIE输出 [英] Use RDF API (Jena, OpenRDF or Protege) to convert OpenIE outputs

查看:73
本文介绍了使用RDF API(Jena,OpenRDF或Protege)转换OpenIE输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议我使用一种API(Jena,OpenRDF或Protege)转换从OpenIE4.1 jar文件生成的输出(可从

I was recommended to use one of the APIs (Jena, OpenRDF or Protege) to convert the outputs that I generated from OpenIE4.1 jar file (downloadable from http://knowitall.github.io/openie/). The following is the sample OpenIE4.1 output format: confidence score followed by subject, predicate, object triplet

    The rail launchers are conceptually similar to the underslung SM-1
    0.93 (The rail launchers; are; conceptually similar to the underslung SM-1)

我计划从上方的输出中生成遵循此模式的三元组(实际上,通过处理一组自由文本文档已生成了数百个以上的输出,仅会处理大于特定值的置信度分数):

I planned to produce triples that follow this pattern from above output (in fact, hundreds of above outputs have been generated by processing a sets of free text documents, only confidence score greater than certain value will be processed):

给予

    subject: The rail launchers
    predicate: are
    object: conceptually similar to the underslung SM-1

(忽略置信度分数)

  1. 为文件中的每个不同的:subject创建一个空白节点标识符(我们将其称为bnode_s)
  2. 为文件中的每个不同的:object创建一个空白节点标识符(我们将其称为bnode_o)
  3. 为每个不同的谓词定义一个URI

顺便说一句,例如,我确实有产生比三胞胎更多的输出 约翰给玛丽一个生日礼物 (约翰;给了;玛丽;生日礼物) 这对于RDF三元组的生产更为复杂.

BTW, I do have outputs that produce more than triplets, for instance John gave Mary a birthday gift (John; gave; Mary; a birthday gift) which is more complicated to product RDF triplet.

但是,我不熟悉上面提到的任何API,也不知道API可能采用的输入格式.

However, I'm not familiar with any of the API mentioned above and don't know the input format that API may take.

推荐答案

import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.URIref;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.XSD;

public class OpenIETripletConversionExample {
    public static void main(String[] args) {
        // Create the model and define some prefixes (for nice serialization in RDF/XML and TTL)
        Model model = ModelFactory.createDefaultModel();
        String NS = "http://stackoverflow.com/q/24897405/1281433/";
        model.setNsPrefix( "", NS );
        model.setNsPrefix( "rdf", RDF.getURI() );
        model.setNsPrefix( "xsd", XSD.getURI() );
        model.setNsPrefix( "rdfs", RDFS.getURI() );

        // Preserve the confidence level (optional).
        Property confidence = model.createProperty( NS+"confidence" ); 

        // Define some triplets to convert.
        Object[][] triplets = {
                { 0.57, "The quick brown fox", "jumped", "over the lazy dog." },
                { 0.93, "The rail launchers", "are", "conceptually similar to the underslung SM-1." }
        };

        // For each triplet, create a resource representing the sentence, as well as the subject, 
        // predicate, and object, and then add the triples to the model.
        for ( Object[] triplet : triplets )  {
            Resource statement = model.createResource();
            Resource subject = model.createResource().addProperty( RDFS.label, (String) triplet[1] );
            Property predicate = model.createProperty( NS+URIref.encode( (String) triplet[2] ));
            Resource object = model.createResource().addProperty( RDFS.label, (String) triplet[3] );

            statement.addLiteral( confidence, triplet[0] );
            statement.addProperty( RDF.subject, subject );
            statement.addProperty( RDF.predicate, predicate );
            statement.addProperty( RDF.object, object );
        }

        // Show the model in a few different formats.
        RDFDataMgr.write( System.out, model, Lang.TTL );
        RDFDataMgr.write( System.out, model, Lang.RDFXML );
        RDFDataMgr.write( System.out, model, Lang.NTRIPLES );
    }
}

@prefix :      <http://stackoverflow.com/q/24897405/1281433/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

[ rdf:object     [ rdfs:label  "conceptually similar to the underslung SM-1." ] ;
  rdf:predicate  :are ;
  rdf:subject    [ rdfs:label  "The rail launchers" ] ;
  :confidence    "0.93"^^xsd:double
] .

[ rdf:object     [ rdfs:label  "over the lazy dog." ] ;
  rdf:predicate  :jumped ;
  rdf:subject    [ rdfs:label  "The quick brown fox" ] ;
  :confidence    "0.57"^^xsd:double
] .

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://stackoverflow.com/q/24897405/1281433/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdf:Description>
    <rdf:object rdf:parseType="Resource">
      <rdfs:label>conceptually similar to the underslung SM-1.</rdfs:label>
    </rdf:object>
    <rdf:predicate rdf:resource="http://stackoverflow.com/q/24897405/1281433/are"/>
    <rdf:subject rdf:parseType="Resource">
      <rdfs:label>The rail launchers</rdfs:label>
    </rdf:subject>
    <confidence rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
    >0.93</confidence>
  </rdf:Description>
  <rdf:Description>
    <rdf:object rdf:parseType="Resource">
      <rdfs:label>over the lazy dog.</rdfs:label>
    </rdf:object>
    <rdf:predicate rdf:resource="http://stackoverflow.com/q/24897405/1281433/jumped"/>
    <rdf:subject rdf:parseType="Resource">
      <rdfs:label>The quick brown fox</rdfs:label>
    </rdf:subject>
    <confidence rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
    >0.57</confidence>
  </rdf:Description>
</rdf:RDF>

_:BX2D492663e1X3A1475ff7864cX3AX2D7ffe <http://www.w3.org/2000/01/rdf-schema#label> "The quick brown fox" .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffc <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> _:BX2D492663e1X3A1475ff7864cX3AX2D7ffa .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffc <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://stackoverflow.com/q/24897405/1281433/are> .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffc <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> _:BX2D492663e1X3A1475ff7864cX3AX2D7ffb .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffc <http://stackoverflow.com/q/24897405/1281433/confidence> "0.93"^^<http://www.w3.org/2001/XMLSchema#double> .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffa <http://www.w3.org/2000/01/rdf-schema#label> "conceptually similar to the underslung SM-1." .
_:BX2D492663e1X3A1475ff7864cX3AX2D7fff <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> _:BX2D492663e1X3A1475ff7864cX3AX2D7ffd .
_:BX2D492663e1X3A1475ff7864cX3AX2D7fff <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://stackoverflow.com/q/24897405/1281433/jumped> .
_:BX2D492663e1X3A1475ff7864cX3AX2D7fff <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> _:BX2D492663e1X3A1475ff7864cX3AX2D7ffe .
_:BX2D492663e1X3A1475ff7864cX3AX2D7fff <http://stackoverflow.com/q/24897405/1281433/confidence> "0.57"^^<http://www.w3.org/2001/XMLSchema#double> .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffd <http://www.w3.org/2000/01/rdf-schema#label> "over the lazy dog." .
_:BX2D492663e1X3A1475ff7864cX3AX2D7ffb <http://www.w3.org/2000/01/rdf-schema#label> "The rail launchers" .

这篇关于使用RDF API(Jena,OpenRDF或Protege)转换OpenIE输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆