2011-08-23 176 views
4

我目前正在使用Ruby on Rails SPARQL客戶端嘗試從dbpedia的公共SPARQL端點檢索信息。我正在運行的通用查詢如下:Ruby on Rails 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想這對RubySPARQL客戶端。代碼看起來像這樣(根據我能找到的例子):

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

但我無法檢索任何東西。當使用調試器手動進入變量時,只要執行查詢就會停止,並且我甚至無法查看返回值。

任何人都有一個關於如何針對SPARQL端點執行查詢並解析響應的好例子?

+0

重複上http://answers.semanticweb.com/questions/11263/sparql-endpoint-in-ruby-on-rails – RobV

+0

現在有dbpedia gem與sparql支持https://github.com/farbenmeer/dbpedia – mahemoff

回答

4

我只是想你的代碼(與未成年人修改使用=代替< <),它似乎工作:

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可能已經被重載。

+0

我會再次看看它在工作中的表現,也許在Windows上jruby和ruby之間有區別 –

+0

這是不太可能的(儘管不是不可能),這是不同的。根據我的經驗,dbpedia端點可能無法預測,所以我的錢就是這樣。 – user205512

+0

好吧,很高興知道。 JSON目前也在運作,所以我現在堅持使用這個解決方案。 –

相關問題