Java n-triple RDF解析

Ank*_*kur 11 java parsing rdf n-triples

我想解析一个n-triple形式的RDF文件.

我可以编写自己的解析器,但我宁愿使用一个库,Jena似乎为此目的而无法复杂(或者至少我看不到他们的文档解释如何以合理的方式读取n-triples).

你可以请我指出任何有用的图书馆,或者如果你了解芝麻或耶拿,你可能会知道如何解决这个问题.

Mar*_*coS 8

对Jena来说并不是那么困难:

给定一个rdfexample.ntriple包含N-TRIPLE形式的以下RDF 的文件(示例取自此处):

<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#year> "1988" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#price> "9.90" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#company> "CBS Records" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#country> "UK" .
<http://www.recshop.fake/cd/Hide your heart> <http://www.recshop.fake/cd#artist> "Bonnie Tyler" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#year> "1985" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#price> "10.90" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#company> "Columbia" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#country> "USA" .
<http://www.recshop.fake/cd/Empire Burlesque> <http://www.recshop.fake/cd#artist> "Bob Dylan" .
Run Code Online (Sandbox Code Playgroud)

以下代码

public static void main(String[] args) {
    String fileNameOrUri = "src/a/rdfexample.ntriple";
    Model model = ModelFactory.createDefaultModel();
    InputStream is = FileManager.get().open(fileNameOrUri);
    if (is != null) {
        model.read(is, null, "N-TRIPLE");
        model.write(System.out, "TURTLE");
    } else {
        System.err.println("cannot read " + fileNameOrUri);;
    }
}
Run Code Online (Sandbox Code Playgroud)

读取文件,并以TURTLE形式打印出来:

<http://www.recshop.fake/cd/Hide your heart>
      <http://www.recshop.fake/cd#artist>
              "Bonnie Tyler" ;
      <http://www.recshop.fake/cd#company>
              "CBS Records" ;
      <http://www.recshop.fake/cd#country>
              "UK" ;
      <http://www.recshop.fake/cd#price>
              "9.90" ;
      <http://www.recshop.fake/cd#year>
              "1988" .

<http://www.recshop.fake/cd/Empire Burlesque>
      <http://www.recshop.fake/cd#artist>
              "Bob Dylan" ;
      <http://www.recshop.fake/cd#company>
              "Columbia" ;
      <http://www.recshop.fake/cd#country>
              "USA" ;
      <http://www.recshop.fake/cd#price>
              "10.90" ;
      <http://www.recshop.fake/cd#year>
              "1985" .
Run Code Online (Sandbox Code Playgroud)

因此,使用Jena,您可以轻松地将RDF(以任何形式)解析为com.hp.hpl.jena.rdf.model.Model对象,从而允许您以编程方式对其进行操作.


Rob*_*obV 7

如果你只想解析NTriples而不需要做除基本处理和查询之外的任何事情,那么你可以试试NxParser.这是一个非常简单的Java代码,它将传递任何NTriples格式(如NQuads等),它为您提供了文件中语句的迭代器.如果您只想要NTriples,则可以轻松忽略少于/超过3个项目的语句.

在链接页面上调整示例将提供以下简单代码:

NxParser nxp = new NxParser(new FileInputStream("filetoparse.nq"),false);

while (nxp.hasNext()) 
{
  Node[] ns = nxp.next();
  if (ns.length == 3)
  {
    //Only Process Triples  
    //Replace the print statements with whatever you want
    for (Node n: ns) 
    {
      System.out.print(n.toN3());
      System.out.print(" ");
    }
    System.out.println(".");
  }
}
Run Code Online (Sandbox Code Playgroud)