ruby-on-rails - Ruby on Rails SPARQL 客户端

标签 ruby-on-rails semantics rdf sparql

我目前正在使用 Ruby on Rails SPARQL 客户端尝试从 dbpedia 的公共(public) 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 中解析响应的方法。

目前我正在尝试使用 Ruby 的 RDF sparql 客户端 .代码如下所示(基于我能找到的示例):
result = {}
    client = SPARQL::Client.new("http://dbpedia.org/sparql")
    client.query(query).first.each_binding { |name, value| result[name] << value}
    result

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

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

最佳答案

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

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 可能已重载。

关于ruby-on-rails - Ruby on Rails SPARQL 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7158335/

相关文章:

ruby-on-rails - 从中间件中触发基本的 HTTP 身份验证

ruby-on-rails - SimpleCov 在覆盖率报告中包含引擎代码 - Rails 测试

ruby-on-rails - 403 Forbidden when using nginx + rails + Passenger

jquery - 如何在没有div切换功能的情况下使用jquery的语义选项卡?

rdf - SPARQL查询rdf容器(rdf : Bag)

neo4j - 由于缺乏具体化,与 RDF 图相比,属性图的表达能力太弱?

ruby-on-rails - 如何扩展 Ruby Test::Unit 断言以包含 assert_false?

vhdl - 进程范围内的本地信号

c# - 我应该为计算值使用属性还是方法?

c# - .NET 中的 OpenCalais 解析 RDF