1

我已经从dbpedia下载了 dbpedia_quotationsbook.zip,其中包含 dbpedia_quotationsbook.nt Triplestore。

在这个三联店

主题是作者名
谓词是“sameas”
对象是作者代码

我已经使用 JENA 尝试了这个查询三元存储,简单的查询正在运行。

现在我想要其作者名与给定字符串部分匹配的所有作者代码。所以我尝试了以下查询

select ?code
where
{

FILTER regex(?name, "^Rob")  <http://www.w3.org/2002/07/owl#sameAs> ?code.

}

上面的查询应该返回作者名包含“Rob”的所有作者代码

我收到以下异常

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "." ". "" at line 5, column 74.
Was expecting one of:
    <IRIref> ...
    <PNAME_NS> ...
    <PNAME_LN> ...
    <BLANK_NODE_LABEL> ...
    <VAR1> ...
    <VAR2> ...
    "true" ...
    "false" ...
    <INTEGER> ...
    <DECIMAL> ...
    <DOUBLE> ...
    <INTEGER_POSITIVE> ...
    <DECIMAL_POSITIVE> ...
    <DOUBLE_POSITIVE> ...
    <INTEGER_NEGATIVE> ...
    <DECIMAL_NEGATIVE> ...
    <DOUBLE_NEGATIVE> ...
    <STRING_LITERAL1> ...
    <STRING_LITERAL2> ...
    <STRING_LITERAL_LONG1> ...
    <STRING_LITERAL_LONG2> ...
    "(" ...
    <NIL> ...
    "[" ...
    <ANON> ...

    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
    at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:34)
    at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
    at rdfcreate.NewClass.query(NewClass.java:55)
    at rdfcreate.NewClass.main(NewClass.java:97)

耶拿代码

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.util.FileManager;

/**
 *
 * @author swapnil
 */
public class NewClass {
    String read()
        {
               final String tdbDirectory = "C:\\TDBLoadGeoCoordinatesAndLabels"; 


    String dataPath = "F:\\Swapnil Drive\\Project Stuff\\Project data 2015 16\\Freelancer\\SPARQL\\dbpedia_quotationsbook.nt";


     Model tdbModel = TDBFactory.createModel(tdbDirectory);
             /*Incrementally read data to the Model, once per run , RAM > 6 GB*/

             FileManager.get().readModel( tdbModel, dataPath, "N-TRIPLES");

             tdbModel.close();

             return tdbDirectory;
        }

       void query(String tdbDirectory, String query1)
       {


    Dataset dataset = TDBFactory.createDataset(tdbDirectory);
              Model tdb = dataset.getDefaultModel();
              Query query = QueryFactory.create(query1);
              QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
              /*Execute the Query*/
                ResultSet results = qexec.execSelect();
                System.out.println(results.getRowNumber());
                while (results.hasNext()) {
                      // Do something important
                    QuerySolution qs =   results.nextSolution();
              qs.toString();
                    System.out.println("sol "+qs);
                }
                   qexec.close();
               tdb.close() ;
       }

     public static void main(String[] args) {

            NewClass nc = new NewClass();
         String tdbd=    nc.read();
          nc.query(tdbd, "select ?code\n" +
    "WHERE\n" +
    "{\n" +
    "<http://dbpedia.org/resource/Robert_H._Schuller> <http://www.w3.org/2002/07/owl#sameAs> ?code.\n" +
    "}");


       }

}

}

结果

溶胶 (?code = http://quotationsbook.com/author/6523 )

上面的查询给了我给定作者的代码。

请帮助我

4

1 回答 1

6

您不能混合使用模式和过滤器。您必须首先使用三重模式绑定(即选择)?name,然后过滤结果。Jena 基本上会抱怨,因为您的 SPARQL 语法无效。

现在,您可以运行以下查询,但您的数据仅包含 dbpedia URI 和报价单 URI 之间的映射。

PREFIX owl: <http://www.w3.org/2002/07/owl#>    
select ?code
where
{
   ?author <name> ?name .
   ?author owl:sameAs ?code .

   FILTER regex(?name, "^Rob")
}

以上是指

  1. 获取作者姓名
  2. 获取作者代码
  3. 仅包括姓名与正则表达式匹配的作者
  4. 选择他们的代码

同样,这仅适用于本地可用的数据。问题是您没有实际姓名。当然,您可以将查询更改为正则表达式整个 dbpedia 标识符,但这并不完美。

FILTER regex(?author, "Rob")

由于 dbpedia 资源是dereferencable,您可以做的是将名称三元组模式包装在GRAPH 模式中

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

select ?author ?code
where
{
   GRAPH  <file://path/to/dbpedia_quotationsbook.nt>
   {
      ?author owl:sameAs ?code .
   }

   GRAPH ?author
   {
      ?author <http://www.w3.org/2000/01/rdf-schema#label> ?name .
      FILTER regex(?name, "^Rob")
   }
}

这是正在发生的事情

  1. 从导入文件中获取?authors 和?codes(SPARQL GUI 导入到图形中)
  2. 当作?author图名,这样就可以从网上下载了
  3. 得到?author?name_
  4. 以Rob?name开头的过滤器

根据您的 SPARQL 处理器(我使用的是 dotNetRDF 工具包中的 SPARQL GUI),有两个重要的部分可以完成这项工作。

这是我得到的结果的屏幕截图。注意突出显示的设置和 dbpedia 请求的 Fiddler 日志。

SPARQL GUI 中的联合查询

底线是我刚刚为您提供了一个联合 SPARQL 查询的示例。

于 2015-12-09T12:04:07.453 回答