0

A 有一个 RDF 文件,我想进行一个 select SPARQL 查询,该查询将选择特定作者的描述。我尝试了如下查询,但没有成功。我是 SPARQL 的新手,所以如果有人能提供帮助,我将不胜感激……</p>

我的尝试:

String query = "PREFIX schema: <"+Constants.SCHEMA+"> \n" + 
               "SELECT ?description \n" +
               "WHERE { \n" +
                   "?review a schema:Review; \n" +
                   "schema:author \"Judson C.\"; \n" +
               "}"; 

RDF 文件如下所示:

<rdf:Description rdf:nodeID="A3">
    <schema:ratingValue>5.0</schema:ratingValue>
    <rdf:type rdf:resource="http://schema.org/Rating" />
</rdf:Description>
<rdf:Description rdf:nodeID="A4">
    < schema:reviewRating rdf:nodeID="A5"/>
    <schema:description>OMG Love this place!! But glad I don't have to
        stand in line for an hour like back in the day to get my fix!</schema:description>
    <schema:datePublished>2013-05-18</schema:datePublished>
    <schema:author>Judson C.</schema:author>
    <rdf:type rdf:resource="http://schema.org/Review" />
</rdf:Description>
<rdf:Description rdf:nodeID="A6">
    <schema:reviewRating rdf:nodeID="A7" />
    <schema:description>If you're ever wondering of where to stuff your
        face with a good ol' sandwich, you'll definitely have to pop in at
        Ike's Place. Not only are the different sandwiches uniquely named,
        they're also uniquely flavor profiled. A wonderful experience
        indeed!Sandwiches I've personally ordered are the Love Triangle,
        Nacho girl, and the Al Bundy. For all you people out there who loved
        the tv show, Married with Children, I think you could appreciate the
        sentiment of having a sammie named after the 'No Ma'am enthusiast. I
        know I did. Ahhh... *tucks hand under pant waistband*All kidding
        aside, these sandwiches are pretty legit. You go to the counter and
        order whichever sandwich sounds appealing and they can either add
        chips or a drink to your meal. Simple. They have a wide range of
        sandwiches that are made to please any meat lover, vegetarian, or
        vegan palate alike. The huge variety is an A+ in my book. They can
        even individualize and customize your sandwich to your liking. This
        means that even the pickiest of eaters are welcome!So if you're
        interested in a fun place to enjoy a good sandwich, most definitely
        give 'em a try!</schema:description>
    <schema:datePublished>2013-04-30</schema:datePublished>
    <schema:author>Ann S.</schema:author>
    <rdf:type rdf:resource="http://schema.org/Review" />
</rdf:Description>
4

2 回答 2

2

简答

简短的回答是查询的语法格式不正确,并且它不会返回您要查找的内容。查询需要用句号 ( .) 而不是分号 ( ;) 来终止其三元组模式。固定查询应如下所示:

PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
  ?review a schema:Review ;
          schema:author "Judson C." .
}

但是,这是查询中未使用SELECT的变量?description,因此它始终为空。您可能想要SELECT ?review或添加到模式schema:description ?description中。

长答案

更多细节表明这是有效的。您提供的 RDF 格式不正确,因此很难对其进行测试。我接受了您提供的内容,并将其写为 N3 以获得以下内容。(RDF/XML 也在答案的最后。)

@prefix schema: <http://schema.org/> .

[]
  a schema:Rating ;
  schema:ratingValue "5.0" .

[]
  a schema:Review ;
  schema:reviewRating [] ;
  schema:description "OMG Love this place!!  But glad I don't have to stand in line for an hour like back in the day to get my fix!" ;
  schema:datePublished "2013-05-18" ;
  schema:author "Judson C." .

[]
 a schema:Review ;
 schema:reviewRating [] ;
 schema:description "If you're ever wondering of where to stuff your face with a good ol' sandwich, you'll definitely have to pop in at Ike's Place. Not only are the different sandwiches uniquely named, they're also uniquely flavor profiled. A wonderful experience indeed!Sandwiches I've personally ordered are the Love Triangle, Nacho girl, and the Al Bundy. For all you people out there who loved the tv show, Married with Children, I think you could appreciate the sentiment of having a sammie named after the 'No Ma'am enthusiast. I know I did. Ahhh... *tucks hand under pant waistband*All kidding aside, these sandwiches are pretty legit. You go to the counter and order whichever sandwich sounds appealing and they can either add chips or a drink to your meal. Simple. They have a wide range of sandwiches that are made to please any meat lover, vegetarian, or vegan palate alike. The huge variety is an A+ in my book. They can even individualize and customize your sandwich to your liking. This means that even the pickiest of eaters are welcome!So if you're interested in a fun place to enjoy a good sandwich, most definitely give 'em a try!" ;
 schema:datePublished "2013-04-30" ;
 schema:author "Ann S." .

如上所述,该查询有一些语法错误;三重模式应该以 a 结尾.,而不是 a ;,并且应该是:

PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
  ?review a schema:Review ;
          schema:author "Judson C." .
}

但是,这不会返回任何有用?description的信息,因为查询中没有使用它。使用 Jena 的 ARQ(并将查询编写为 schema.query),我们输出一行(因为模式匹配),但带有一个未绑定的变量:

$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
  --query schema.query \
  --data schema.n3
---------------
| description |
===============
|             |
---------------

这是一个绑定的查询?description

PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
  ?review a schema:Review ;
          schema:author "Judson C." ;
          schema:description ?description .
}

正如预期的那样,它给出了一些有用的结果:

$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
  --query schema2.query \
  --data schema.n3
-------------------------------------------------------------------------------------------------------------------
| description                                                                                                     |
===================================================================================================================
| "OMG Love this place!!  But glad I don't have to stand in line for an hour like back in the day to get my fix!" |
-------------------------------------------------------------------------------------------------------------------

RDF/XML 附录

这是手头数据的 RDF/XML 序列化。

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:schema="http://schema.org/">
  <schema:Review>
    <schema:reviewRating rdf:parseType="Resource">
    </schema:reviewRating>
    <schema:description>If you're ever wondering of where to stuff your face with a good ol' sandwich, you'll definitely have to pop in at Ike's Place. Not only are the different sandwiches uniquely named, they're also uniquely flavor profiled. A wonderful experience indeed!Sandwiches I've personally ordered are the Love Triangle, Nacho girl, and the Al Bundy. For all you people out there who loved the tv show, Married with Children, I think you could appreciate the sentiment of having a sammie named after the 'No Ma'am enthusiast. I know I did. Ahhh... *tucks hand under pant waistband*All kidding aside, these sandwiches are pretty legit. You go to the counter and order whichever sandwich sounds appealing and they can either add chips or a drink to your meal. Simple. They have a wide range of sandwiches that are made to please any meat lover, vegetarian, or vegan palate alike. The huge variety is an A+ in my book. They can even individualize and customize your sandwich to your liking. This means that even the pickiest of eaters are welcome!So if you're interested in a fun place to enjoy a good sandwich, most definitely give 'em a try!</schema:description>
    <schema:datePublished>2013-04-30</schema:datePublished>
    <schema:author>Ann S.</schema:author>
  </schema:Review>
  <schema:Review>
    <schema:reviewRating rdf:parseType="Resource">
    </schema:reviewRating>
    <schema:description>OMG Love this place!!  But glad I don't have to stand in line for an hour like back in the day to get my fix!</schema:description>
    <schema:datePublished>2013-05-18</schema:datePublished>
    <schema:author>Judson C.</schema:author>
  </schema:Review>
  <schema:Rating>
    <schema:ratingValue>5.0</schema:ratingValue>
  </schema:Rating>
</rdf:RDF>
于 2013-05-23T17:53:07.417 回答
0

我想这个问题真的是关于 SPARQL,而不是 java。

您是否尝试过写作:

?review schema:author "Judson C."^^xsd:string

或者:

?review schema:author ?author:
FILTER(str(?author)=="Judson C.")
于 2013-05-23T16:43:33.297 回答