Ruby on Rails SPARQL 客户端

发布于 2024-12-01 02:12:50 字数 1201 浏览 1 评论 0原文

我目前正在使用 Ruby on Rails SPARQL 客户端来尝试从 dbpedia 上的公共 SPARQL 端点检索信息。我正在运行的通用查询如下:

query = "
      PREFIX dbo: <http://dbpedia.org/ontology/>
      PREFIX prop: <http://dbpedia.org/property/>
      PREFIX foaf: <http://xmlns.com/foaf/0.1/>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      SELECT * WHERE {
        ?city prop:name 'Antwerpen'@en;
              a dbo:PopulatedPlace;
              dbo:abstract ?abstract .
        FILTER langMatches( lang(?abstract), 'en')
        OPTIONAL { ?city foaf:homepage ?homepage }
        OPTIONAL { ?city rdfs:comment ?comment .
                  FILTER langMatches( lang(?comment), 'en') }
      }"

这是一个通用查询,从 dbpedia 返回有关城市的一些常规信息。我已经在 sparql 端点上手动测试了这一点,它检索了我期望它返回的信息。 但是,我似乎无法找到一种方法来解析 Ruby on Rails 中的响应。

目前我正在使用 RDF for Ruby 和 sparql-client 进行尝试。代码如下所示(基于我能找到的示例):

result = {}
    client = SPARQL::Client.new("http://dbpedia.org/sparql")
    client.query(query).first.each_binding { |name, value| result[name] << value}
    result

但我无法检索任何内容。当使用调试器运行以手动单步执行变量时,它会在执行查询后立即停止,我什至无法查看返回值。

有人有关于如何针对 SPARQL 端点执行查询并解析响应的好例子吗?

I'm currently working with the Ruby on Rails SPARQL client to try and retrieve information from the public SPARQL endpoint at dbpedia. The generic query I'm running is the following:

query = "
      PREFIX dbo: <http://dbpedia.org/ontology/>
      PREFIX prop: <http://dbpedia.org/property/>
      PREFIX foaf: <http://xmlns.com/foaf/0.1/>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      SELECT * WHERE {
        ?city prop:name 'Antwerpen'@en;
              a dbo:PopulatedPlace;
              dbo:abstract ?abstract .
        FILTER langMatches( lang(?abstract), 'en')
        OPTIONAL { ?city foaf:homepage ?homepage }
        OPTIONAL { ?city rdfs:comment ?comment .
                  FILTER langMatches( lang(?comment), 'en') }
      }"

It's a generic query that returns some general information about a city from dbpedia. I've tested this manually on the sparql endpoint and it retrieves the information I expect it to return.
However, I can't seem to find a way to parse the response in Ruby on Rails.

Currently I'm trying this with RDF for Ruby and sparql-client. The code looks like this (based upon the examples I could find):

result = {}
    client = SPARQL::Client.new("http://dbpedia.org/sparql")
    client.query(query).first.each_binding { |name, value| result[name] << value}
    result

But I'm unable to retrieve anything. When running with the debugger to step into the variables manually, it just stops as soon as the query is executed, and I can't even view the return values.

Anyone have a good example on how to execute a query against a SPARQL endpoint and parse the response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2024-12-08 02:12:50

我刚刚尝试了你的代码(使用 = 而不是 << 进行了细微修改),它似乎有效:

require 'rubygems'
require 'sparql/client'

query = "
  PREFIX dbo: <http://dbpedia.org/ontology/>
  PREFIX prop: <http://dbpedia.org/property/>
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  SELECT * WHERE {
    ?city prop:name 'Antwerpen'@en;
          a dbo:PopulatedPlace;
          dbo:abstract ?abstract .
    FILTER langMatches( lang(?abstract), 'en')
    OPTIONAL { ?city foaf:homepage ?homepage }
    OPTIONAL { ?city rdfs:comment ?comment .
              FILTER langMatches( lang(?comment), 'en') }
  }"

result = {}
client = SPARQL::Client.new("http://dbpedia.org/sparql")
client.query(query).first.each_binding { |name, value| result[name] = value}
p result

当我运行它时:

$ jruby sparql.rb 
{:city=>#<RDF::URI:0x890(http://dbpedia.org/resource/Antwerp)>, :abstract=>#<RDF::Literal:0x892("Antwerp is a city and municipality in Belgium and the capital of the Antwerp province in Flanders, one of Belgium's three regions. Antwerp's total population is 472,071 (as of 1 January 2008) and its total area is 204.51 km, giving a population density of 2,308 inhabitants per km\u00B2. The metropolitan area, including the outer commuter zone, covers an area of 1,449 km with a total of 1,190,769 inhabitants as of 1 January 2008. The nickname of inhabitants of Antwerp is Sinjoren, after the Spanish word se\u00F1or, which means 'mister' or 'gent'. Antwerp has long been an important city in the nations of the Benelux both economically and culturally, especially before the Spanish Fury of the Dutch Revolt. It is located on the right bank of the river Scheldt, which is linked to the North Sea by the estuary Westerschelde."@en)>, :homepage=>#<RDF::URI:0x898(http://www.antwerpen.be/)>, :comment=>#<RDF::Literal:0x89a("Antwerp is a city and municipality in Belgium and the capital of the Antwerp province in Flanders, one of Belgium's three regions. Antwerp's total population is 472,071 (as of 1 January 2008) and its total area is 204.51 km, giving a population density of 2,308 inhabitants per km\u00B2. The metropolitan area, including the outer commuter zone, covers an area of 1,449 km with a total of 1,190,769 inhabitants as of 1 January 2008."@en)>}

根据你的描述,可能是你有连接问题,或者 dbpedia 可能已经超载。

I just tried your code (with a minor modification using = instead of <<), and it seems to work:

require 'rubygems'
require 'sparql/client'

query = "
  PREFIX dbo: <http://dbpedia.org/ontology/>
  PREFIX prop: <http://dbpedia.org/property/>
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  SELECT * WHERE {
    ?city prop:name 'Antwerpen'@en;
          a dbo:PopulatedPlace;
          dbo:abstract ?abstract .
    FILTER langMatches( lang(?abstract), 'en')
    OPTIONAL { ?city foaf:homepage ?homepage }
    OPTIONAL { ?city rdfs:comment ?comment .
              FILTER langMatches( lang(?comment), 'en') }
  }"

result = {}
client = SPARQL::Client.new("http://dbpedia.org/sparql")
client.query(query).first.each_binding { |name, value| result[name] = value}
p result

and when I run it:

$ jruby sparql.rb 
{:city=>#<RDF::URI:0x890(http://dbpedia.org/resource/Antwerp)>, :abstract=>#<RDF::Literal:0x892("Antwerp is a city and municipality in Belgium and the capital of the Antwerp province in Flanders, one of Belgium's three regions. Antwerp's total population is 472,071 (as of 1 January 2008) and its total area is 204.51 km, giving a population density of 2,308 inhabitants per km\u00B2. The metropolitan area, including the outer commuter zone, covers an area of 1,449 km with a total of 1,190,769 inhabitants as of 1 January 2008. The nickname of inhabitants of Antwerp is Sinjoren, after the Spanish word se\u00F1or, which means 'mister' or 'gent'. Antwerp has long been an important city in the nations of the Benelux both economically and culturally, especially before the Spanish Fury of the Dutch Revolt. It is located on the right bank of the river Scheldt, which is linked to the North Sea by the estuary Westerschelde."@en)>, :homepage=>#<RDF::URI:0x898(http://www.antwerpen.be/)>, :comment=>#<RDF::Literal:0x89a("Antwerp is a city and municipality in Belgium and the capital of the Antwerp province in Flanders, one of Belgium's three regions. Antwerp's total population is 472,071 (as of 1 January 2008) and its total area is 204.51 km, giving a population density of 2,308 inhabitants per km\u00B2. The metropolitan area, including the outer commuter zone, covers an area of 1,449 km with a total of 1,190,769 inhabitants as of 1 January 2008."@en)>}

Given your description it may be that you have a connection issue, or dbpedia may have been overloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文