SPARQL клиент Ruby on Rails - PullRequest
       20

SPARQL клиент Ruby on Rails

4 голосов
/ 23 августа 2011

В настоящее время я работаю с клиентом Ruby on Rails SPARQL, чтобы попытаться получить информацию из общедоступной конечной точки SPARQL в dbpedia.Общий запрос, который я выполняю, выглядит следующим образом:

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 для Ruby и SPARQL-клиент .Код выглядит следующим образом (основываясь на примерах, которые я смог найти):

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

Но я ничего не могу найти.При запуске с отладчиком для перехода к переменным вручную, он просто останавливается, как только выполняется запрос, и я даже не могу просмотреть возвращаемые значения.

У всех есть хороший пример того, как выполнитьвыполнить запрос к конечной точке SPARQL и проанализировать ответ?

1 Ответ

4 голосов
/ 23 августа 2011

Я только что попробовал ваш код (с незначительной модификацией, использующей = вместо <<), и похоже, что он работает: </p>

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 может быть перегружен.

...