首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java从RDF文件中获取特定的标签相关信息

如何使用java从RDF文件中获取特定的标签相关信息
EN

Stack Overflow用户
提问于 2013-08-02 17:58:27
回答 1查看 1.2K关注 0票数 0

我正在使用Jena读取RDF文件,但我只想获取特定语句的信息。下面是我用来读取sample.rdf的示例代码。

sample.rdf

代码语言:javascript
复制
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/Instance/143">
    <c:length>4</c:length>
    <c:offset>6588</c:offset>
    <c:suffix> network, specific mechanisms for implementing</c:suffix>
    <c:exact>VoIP</c:exact>
    <c:prefix>applications. Topics include imple-menting a 
   </c:prefix>
    <c:detection>[applications. Topics include imple-
menting a ]VoIP[ network, specific mechanisms for implementing]</c:detection>
    <c:subject rdf:resource="http://d.opencalais.com/genericHasher-1/1bc26b65-5ef5-306d-9203-fd0f8aa3ba18"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
    <rdf:type rdf:resource="http://s.opencalais.com/1/type/sys/InstanceInfo"/>
  </rdf:Description>

<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:originalValue>Cisco IOS</c:originalValue>
    <c:importance>2</c:importance>
    <c:name>Cisco IOS</c:name>
    <c:socialtag rdf:resource="http://d.opencalais.com/genericHasher-1/8ed51994-de69-3307-acf7-be18cc0d06e2"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
    <rdf:type rdf:resource="http://s.opencalais.com/1/type/tag/SocialTag"/>
  </rdf:Description>

sample.rdf加载到Jena模型中的Java代码:

代码语言:javascript
复制
public class FirstRDFReader extends Object {
   public static void main (String args[]) {
       String inputFile="C://Sample.rdf";
      Model model = ModelFactory.createDefaultModel();
   try{
   InputStream in =new  FileInputStream(inputFile);
    if (in == null) {  
     System.out.println("File not found");
     }  
     model.read(in," ");
    model.write(System.out);
}catch(Exception e){}
  }
}

所需输出:

代码语言:javascript
复制
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
<c:importance>2</c:importance>
 <c:name>Cisco IOS</c:name>
</rdf:Description>

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-08-02 20:49:55

工作数据

这里有一些我们可以实际使用的数据。它基于您的数据,带有定义的rdf前缀,以及一些用于使ctag:SocialTagcsys:InstanceInfo工作的附加前缀。这些是可选的,但是c的前缀定义是必需的,因为它在您的数据中使用。为了方便起见,我只是使用了http://example.org/c#,但您可能已经定义了其他内容。

代码语言:javascript
复制
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:c="http://example.org/c#"
    xmlns:ctag="http://s.opencalais.com/1/type/tag/"
    xmlns:csys="http://s.opencalais.com/1/type/sys/">
  <ctag:SocialTag rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:originalValue>Cisco IOS</c:originalValue>
    <c:importance>2</c:importance>
    <c:name>Cisco IOS</c:name>
    <c:socialtag rdf:resource="http://d.opencalais.com/genericHasher-1/8ed51994-de69-3307-acf7-be18cc0d06e2"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
  </ctag:SocialTag>
  <csys:InstanceInfo rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/Instance/143">
    <c:length>4</c:length>
    <c:offset>6588</c:offset>
    <c:suffix> network, specific mechanisms for implementing</c:suffix>
    <c:exact>VoIP</c:exact>
    <c:prefix>applications. Topics include imple-menting a 
   </c:prefix>
    <c:detection>[applications. Topics include imple-
menting a ]VoIP[ network, specific mechanisms for implementing]</c:detection>
    <c:subject rdf:resource="http://d.opencalais.com/genericHasher-1/1bc26b65-5ef5-306d-9203-fd0f8aa3ba18"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
  </csys:InstanceInfo>
</rdf:RDF>

一旦您在某个地方获得了数据,有两种简单的方法可以使用Jena将其提取出来。第一种是使用Jena的Model API,它提供了检索语句的方法。第二种方法是使用SPARQL查询。您可以使用Jena的命令行工具运行SPARQL查询,也可以从Java程序运行。

使用模型API

下面的代码创建一个results模型来存储所需的输出,从input检索SocialTags和定义其名称和重要性的语句,并将语句复制到results中。

代码语言:javascript
复制
public static Model queryWithAPI() { 
    // Create a model for the output, and add the prefix mappings
    // from the input model.  This step isn't necessary, but it 
    // makes the output easier to read.
    final Model results = ModelFactory.createDefaultModel();
    results.setNsPrefixes( input );

    // Iterate through the SocialTags in the data, and for each SocialTag s, retrieve
    // the statements [s, name, ?name] and [s, importance, ?importance] from the input
    // model, and add them to the results.
    for ( final ResIterator it = input.listResourcesWithProperty( RDF.type, SocialTag ); it.hasNext(); ) {
        final Resource socialTag = it.next();
        results.add( socialTag.getProperty( importance ));
        results.add( socialTag.getProperty( name ));
    }
    return results;
}

使用SPARQL

下面的SPARQL construct查询还检索SocialTags,然后构造所需的图。

代码语言:javascript
复制
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix ctag: <http://s.opencalais.com/1/type/tag/>
prefix c: <http://example.org/c#>
construct {
  ?tag c:name ?name ;
       c:importance ?importance .
}
where {
  ?tag a ctag:SocialTag ;
       c:name ?name ;
       c:importance ?importance .
}

下面是在input模型上执行该查询Java代码。

代码语言:javascript
复制
public static Model queryWithSPARQL() { 
    // A SPARQL query that retrieves each SocialTag and its name
    // and importance, and constructs a model containing just the 
    // name and importance statements.
    final String query = "" +
            "prefix rdf: <"+RDF.getURI()+">\n" +
            "prefix ctag: <"+CTAG+">\n" +
            "prefix c: <"+C+">\n" +
            "construct {\n" +
            "  ?tag c:name ?name ;\n" +
            "       c:importance ?importance .\n" +
            "}\n" +
            "where {\n" +
            "  ?tag a ctag:SocialTag ;\n" +
            "       c:name ?name ;\n" +
            "       c:importance ?importance .\n" +
            "}";
    // Create and execute the query on the input model.
    return QueryExecutionFactory.create( query, input ).execConstruct();
}

现在一起来吧

上面的清单只是一个定义input并读取数据的工作示例的代码片段。下面是完整的清单:

代码语言:javascript
复制
import com.hp.hpl.jena.query.QueryExecutionFactory;
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.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.vocabulary.RDF;

public class CalaisExample {
    static final String C = "http://example.org/c#";
    static final String CTAG = "http://s.opencalais.com/1/type/tag/";

    static final Resource SocialTag = ResourceFactory.createResource( CTAG+"SocialTag" );
    static final Property importance = ResourceFactory.createProperty( C+"importance" );
    static final Property name = ResourceFactory.createProperty( C+"name" );

    // Create a model for the input and read in the data.
    static final Model input = ModelFactory.createDefaultModel()
            .read( "file:///home/taylorj/tmp/jena-calais/calais.rdf" );

    public static void main(String[] args) {
        System.out.println( "== Using API ==" );
        queryWithAPI().write( System.out );
        System.out.println();
        System.out.println( "== Using SPARQL ==" );
        queryWithSPARQL().write( System.out );
    }

    public static Model queryWithAPI() { 
        // Create a model for the output, and add the prefix mappings
        // from the input model.  This step isn't necessary, but it 
        // makes the output easier to read.
        final Model results = ModelFactory.createDefaultModel();
        results.setNsPrefixes( input );

        // Iterate through the SocialTags in the data, and for each SocialTag s, retrieve
        // the statements [s, name, ?name] and [s, importance, ?importance] from the input
        // model, and add them to the results.
        for ( final ResIterator it = input.listResourcesWithProperty( RDF.type, SocialTag ); it.hasNext(); ) {
            final Resource socialTag = it.next();
            results.add( socialTag.getProperty( importance ));
            results.add( socialTag.getProperty( name ));
        }
        return results;
    }

    public static Model queryWithSPARQL() { 
        // A SPARQL query that retrieves each SocialTag and its name
        // and importance, and constructs a model containing just the 
        // name and importance statements.
        final String query = "" +
                "prefix rdf: <"+RDF.getURI()+">\n" +
                "prefix ctag: <"+CTAG+">\n" +
                "prefix c: <"+C+">" +
                "construct {\n" +
                "  ?tag c:name ?name ;\n" +
                "       c:importance ?importance .\n" +
                "}\n" +
                "where {\n" +
                "  ?tag a ctag:SocialTag ;\n" +
                "       c:name ?name ;\n" +
                "       c:importance ?importance .\n" +
                "}";
        // Create and execute the query on the input model.
        return QueryExecutionFactory.create( query, input ).execConstruct();
    }
}

下面是输出:

代码语言:javascript
复制
== Using API ==
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:c="http://example.org/c#"
    xmlns:ctag="http://s.opencalais.com/1/type/tag/"
    xmlns:csys="http://s.opencalais.com/1/type/sys/" > 
  <rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:name>Cisco IOS</c:name>
    <c:importance>2</c:importance>
  </rdf:Description>
</rdf:RDF>

== Using SPARQL ==
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:c="http://example.org/c#"
    xmlns:ctag="http://s.opencalais.com/1/type/tag/"
    xmlns:csys="http://s.opencalais.com/1/type/sys/" > 
  <rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:importance>2</c:importance>
    <c:name>Cisco IOS</c:name>
  </rdf:Description>
</rdf:RDF>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18014074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档