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

ruby-on-rails – Ruby on Rails SPARQL客户端,第1张

概述我目前正在使用 Ruby on Rails SPARQL客户端尝试从dbpedia的公共SPARQL端点检索信息.我正在运行的通用查询如下: query = " PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX prop: <http://dbpedia.org/property/> PREFIX foaf: < 我目前正在使用 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端点执行查询并解析响应?

解决方法 我只是尝试了你的代码(使用=而不是<<进行了一些小修改),它似乎工作:

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,769 inhabitants as of 1 January 2008."@en)>}

根据您的描述,可能是您遇到连接问题,或者dbpedia可能已经过载.

总结

以上是内存溢出为你收集整理的ruby-on-rails – Ruby on Rails SPARQL客户端全部内容,希望文章能够帮你解决ruby-on-rails – Ruby on Rails SPARQL客户端所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1278555.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存