W3C home > Mailing lists > Public > public-rdf-ruby@w3.org > August 2011

RE: Parsing DBPedia result

From: Arne De Herdt <arne.de.herdt@tenforce.com>
Date: Thu, 25 Aug 2011 08:35:04 +0200
Message-ID: <60d7c58028d24b45f96c5894813bc80c@mail.gmail.com>
To: ben@dydra.com
Cc: Ross Singer <rossfsinger@gmail.com>, Tom Morris <tom@tommorris.org>, public-rdf-ruby@w3.org
Iíll definitly keep this snippet around, and see if I can get it working
later on

*Arne De Herdt
**Software Engineer .NET/Ruby on Rails*

Email: arne.de.herdt@tenforce.com

Phone: +32 (0)16 31 48 64
Mobile: +32 (0)473 80 84 89
Web: http://www.tenforce.com

[image: Description:

*From:* blavender@gmail.com [mailto:blavender@gmail.com] *On Behalf Of *Ben
*Sent:* woensdag 24 augustus 2011 19:57
*To:* Arne De Herdt
*Cc:* Ross Singer; Tom Morris; public-rdf-ruby@w3.org
*Subject:* Re: Parsing DBPedia result

The documentation for sparql-client is woeful on this, but the gem actually
does everything you need to do here. The DSL is there to make simple use
cases look Rubyish, but I doubt it'll ever be extended to rubify complicated
queries like unions. You can access the querying and parsing facilities

ben@ben-2:~$ irb

ruby-1.9.2-p290 :001 > require 'sparql/client'

 => true

ruby-1.9.2-p290 :002 > c = SPARQL::Client.new('http://dbpedia.org/sparql')

 => #<SPARQL::Client:0x80c40588(http://dbpedia.org/sparql)>

ruby-1.9.2-p290 :004 > c.query('ASK where { ?s ?p ?o }')

 => true

ruby-1.9.2-p290 :005 > c.query('select * where { ?s ?p ?o } limit 1')

 => [#<RDF::Query::Solution:0x80759b00({:s=>#<RDF::URI:0x8074a6a0(

The result from a select query is an array of RDF::Query::Solutions, which
lets you do things like:

ruby-1.9.2-p290 :012 > result.s

=> #<RDF::URI:0x8074a6a0(

Ask queries return true/false, construct/describe return RDF::Repositories
(or maybe RDF::Graphs, I'm not actually sure).

result[:s] should work too.

If you are doing something fancy at the protocol level, you can also do:


=> # an array of RDF::Query::Solutions OR an RDF::Repository OR true/false



=> # an array of RDF::Query::Solutions OR an RDF::Repository OR true/false

The latter two are what we use for the Dydra client, which needs to support
our authentication methods above and beyond the core SPARQL protocol.

Hope this helps,


On Wed, Aug 24, 2011 at 10:03 AM, Arne De Herdt <arne.de.herdt@tenforce.com>

I've tried playing around with it, but I couldn't get it to do what I
need, cause I rely on UNIONS alot for certain operations.
At the moment I just have the endpoint return my data as JSON and parse
that information myself, which seems to work as well.

Arne De Herdt
Software Engineer .NET/Ruby on Rails

Email: arne.de.herdt@tenforce.com
Phone: +32 (0)16 31 48 64
Mobile: +32 (0)473 80 84 89
Web: http://www.tenforce.com

-----Original Message-----
From: Ross Singer [mailto:rossfsinger@gmail.com]
Sent: woensdag 24 augustus 2011 16:22
To: Tom Morris
Cc: Arne De Herdt; public-rdf-ruby@w3.org
Subject: Re: Parsing DBPedia result

It's also worth looking at sparql-client
(http://sparql.rubyforge.org/client/) although it doesn't appear to
support more complicated queries, UNIONs, etc., but would definitely
handle your current queries.

The nice part of this is that it supplies a simple DSL for building your
queries and also parses the results into native ruby objects.


On Wed, Aug 24, 2011 at 8:52 AM, Tom Morris <tom@tommorris.org> wrote:
> [On Mon, Aug 22, 2011 at 14:37, Arne De Herdt
> <arne.de.herdt@tenforce.com> wrote:
>> Iíve just been thrown into a project that requires me to parse
information from DBPedia based upon certain keywords. The example in the
demo is the name of the city, which should return me the English
description of that city for the demo.
>> Iíve scrambled my own SPARQL query together for the endpoint at
DBPedia. Iím by no means experienced in semantics, as I just got plunged
into this world. My query looks like this:
>> The info returned is what I want to work with.
> Hi Arne,
> You are doing a SELECT query which returns back XML in the SPARQL
> Query Results XML Format [1] rather than RDF. Unlike RDF, this can be
> easily parsed using standard XML tools. In Ruby, the best tool for
> this is Nokogiri [2], which basically gives you a DOM-style object
> graph for navigating the returned XML.
> Basically, the SPARQL XML results format is essentially a big table,
> and isn't RDF data: it's just a list of all the results, much as you
> might get from a SQL query.
> Just run the query as an HTTP call (using something like Curb or even
> open-uri), parse it into Nokogiri, then extract from the XML document
> the data you need.
> [1] http://www.w3.org/TR/rdf-sparql-XMLres/
> [2] http://nokogiri.org/
> --
> Tom Morris
> <http://tommorris.org/>

Ben Lavender | ben@dydra.com | http://dydra.com
twitter/github: bhuga | +15047221016

(image/jpeg attachment: image001.jpg)

Received on Thursday, 25 August 2011 06:35:46 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 17:02:15 UTC