vote up 2 vote down
star

Hi,

I am using the dotNetRDF library (www.dotnetrdf.org) and attempting to read a Freebase RDF Page. I am a newcomer to RDF so any help is appreciated from people who have tried to access Freebase RDF data.

I am setting the Base Graph URI:

Graph g = new Graph ();
URILoader.Load(g, new Uri("http://rdf.freebase.com/ns/en.roman_abramovich"));

And now I am a little stuck.

Follwoing the documentation, I can now loop through the triples:

//Loop through Triples
        foreach (Triple t in g.Triples)
        {
            Console.WriteLine(t.Subject.ToString());
        }

But it is not obvious how I map data. For example: From the results, fetch the "Date of Birth" for Roman Abramovich.

Any guidance for a beginner would be appreciated.

flag

3 Answers

vote up 2 vote down

Here is an example using dotNetRDF of getting a set of triples out of the graph that match a specific subject and predicate.

Graph g = new Graph();
URILoader.Load(g, new Uri("http://rdf.freebase.com/ns/en.roman_abramovich"));

var subject = g.GetURINode(new Uri("http://rdf.freebase.com/ns/en.roman_abramovich"));
var predicate = g.CreateURINode(new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));

var selector = new SubjectHasPropertySelector(subject, predicate); 

foreach(var t in g.GetTriples(selector))
    Console.WriteLine(t.ToString());

Here is the output:

http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/user.narphorium.people.topic
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/business.company_founder
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/military.military_person
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/base.popstra.celebrity
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/sports.sports_team_owner
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/base.popstra.sww_base
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/user.narphorium.people.nndb_person
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/boats.ship_owner
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/government.politician
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/base.popstra.topic
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/base.billionaires.billionaire
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/people.person
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/user.narphorium.people.wealthy_person
http://rdf.freebase.com/ns/en.roman_abramovich , http://www.w3.org/1999/02/22-rdf-syntax-ns#type , http://rdf.freebase.com/ns/common.topic

I came up with a sample using my preferred .NET SDK for working with graph data from Intellidimension. Posting here for comparison.

var ds = new GraphDataSource();
ds.Read(new Uri("http://rdf.freebase.com/ns/en.roman_abramovich"));

foreach (var s in ds.GetStatements("http://rdf.freebase.com/ns/en.roman_abramovich", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", null))
    Console.WriteLine(s.ToString());
link|flag
The reason I didn't get birth date as the OP requested in my examples is because I didn't see the birth date predicate on a quick scan of the data. – spoon16 Nov 6 at 6:16
vote up 0 vote down

Sorry I may be wrong, I don't know the dotNetRDF library, but the souldn't the url be http://rdf.freebase.com/rdf/en.roman_abramovich instead of http://rdf.freebase.com/ns/en.roman_abramovich ?

link|flag
1 
Hi Pierre, from what I can see if the client is a web browser and accesses rdf.freebase.com/ns/en.roman_abramovich then it serves the Freebase page, if not it returns the actual RDF. Both give the same response from the client that uses dotNetRDF. Thanks – Jim Nov 5 at 12:45
Jim's right - when using the URILoader (or any of the HTTP based retrieval methods) dotNetRDF will send an Accept Header that specifies all the RDF syntaxes it supports so if the server does content-negotiation as Freebase does it'll have it's request redirected to an appropriate RDF representation – Rob Vesse Dec 10 at 10:17
vote up 0 vote down

You could do this more simply as follows:

Graph g = new Graph();
UriLoader.Load(g, new Uri("http://rdf.freebase.com/ns/en.roman_abramovich"));

UriNode dob = g.CreateUriNode("fb:people.person.date_of_birth");

foreach(Triple t in g.GetTriplesWithSubjectPredicate(g.CreateUriNode(), dob))
{
    Console.WriteLine(t.ToString());
}

That gets you the date of birth as you require. Note the use of g.CreateUriNode() to create a URI Node that refers to the Base URI of the Graph.

If you point out which bit of the documentation is unclear I can update it appropriately since I'm the developer of dotNetRDF so I'm keen to encourage adoption and improve documentation where needed.

With regards to Pierre's post dotNetRDF sends appropriate HTTP Accept headers so that if the URI does content negotiation to return RDF then it should get RDF back. I don't have enough reputation to put this as a comment yet.

EDIT - Changed the example to the use the improved API

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.