Tuesday, August 24, 2010

Retrieving Other Info From a Dbpedia Page

While still using the dbpedia Virtuoso browser, http://dbpedia.org/sparql, pointing to a single page about Joan Baez, lets explore what it takes to retrieve different types of data.

DISPLAYING THE ABSTRACT


The subject ?s will point back to this page.

The predicate was the dbpedia-owl:abstract link copied from the page.

The object ?o will be the abstract returned from the query.

RUN QUERY


See the results.

GET ONLY THE ENGLISH ABSTRACTS

The last query returned abstracts in many different languages. To return just the English version, we modify the Sparql query as follows:
select distinct * 
where {?s <http://dbpedia.org/ontology/abstract> ?o .
 FILTER(langMatches(lang(?o),"en")).
}

The filter condition is how we specify the language tag associated with English,
abbreviated "en". The built-in function, langMatches provides the capability to detect the language associated with a column.

RUN QUERY


See the results.

GET ONLY THE ENGLISH ABSTRACTS OR ABSTRACTS AS SIMPLE STRINGS

Sometimes you might have no language tag at all, just a simple text string. To pick that up, you need the UNION operator to handle this possibility.

Note:The clause describing the abstracts with the @en label, for English, is enclosed in braces {} followed by the keyword UNION and then braces must enclose the clause for label-less abstracts.

select distinct * 
where 
{
{
  ?s <http://dbpedia.org/ontology/abstract> ?o .
  FILTER(langMatches(lang(?o),"en")).
 
 }
 UNION
 {
  ?s <http://dbpedia.org/ontology/abstract> ?o .
  FILTER(!langMatches(lang(?o),"*")).
 }
} 

The second filter condition is how we specify a string without a language tag, or rather, it does not match the object that has a language value of *, meaning any language.

In this case it doesn't make a difference because all abstracts include a language tag.

RUN QUERY


See the results.

Monday, August 23, 2010

Querying a Single DBPedia Page - for Joan Baez

Still using the dbpedia Virtuoso browser, http://dbpedia.org/sparql, I applied the last query to a single dbpedia page by specifying the default graph uri as http://dbpedia.org/resource/Joan_Baez. This will show the various ways dbpedia classifies Joan Baez.

Virtuoso SPARQL Query Form


RUN QUERY


See the results.

COMPARE TO THE DBPEDIA PAGE FOR JOAN BAEZ

When starting with SPARQL, I often pored over the dbpedia resource file in order to compare it with my SPARQL output to see if I got the SPARQL code right. These results match the entries under rdf:type (the predicate which was abbreviated as a).

Note: I did not specify the Joan Baez human readable page, http://dbpedia.org/page/Joan_Baez, in the Default Graph URI.

Instead, I specified the RDF page, http://dbpedia.org/resource/Joan_Baez" which is designed for SPARQL querying. However, if you point your browser to to resource page, dbpedia will show you the human readable page. The Sparql query engine however can go straight to the underlying RDF.

Tuesday, August 17, 2010

An Online SPARQL Query Form

I must apologize to my reader(s) about hogging all the Sparql fun. Here I've been querying and revising queries and not even given you a bit of help on running your own Sparql queries. Forgive me.

Virtuoso SPARQL Query Form

 The link for the website is http://dbpedia.org/sparql



I like it for querying dbpedia because of it's immediacy. I put in the Sparql query and get out my answer, nothing to install on my machine, sweet and simple SPARQL power over the web.

Default Graph URI

The default graph textbox allows you to tell SPARQL what information source to query when you do not explicitly name a source in your query. The Virtuoso browser prefills the field with http://dbpedia.org meaning query all of dbpedia.

THE QUERY TEXT

The query text box is where you enter your SPARQL query. I generally open a second window pointing to the Virtuoso query, then I can copy the SPARQL queries from this blog or any other SPARQL learning resource, put it in this box, and run it. That way I can not only see the queries in action, but can tweak them and explore a little further on my own.

select distinct ?Concept where { [] a ?Concept }

This says: Show me the distinct Concepts in http://dbpedia.org.

The symbols [] indicates I don't care what the subject is.

The predicate a is shorthand for rdf:type. I translate it as "is a" .

The object is called a ?Concept.


DISPLAY RESULTS AS

The Virtuoso browser provides many different output formats. I use HTML most of the time. If you write a query that outputs only a few columns and a few records, then running the same query in each of the different formats is a very clear way to appreciate the differences between each output format.


RIGOROUS CHECK OF THE QUERY

The rigorous check checkbox probably provides more feedback about less desirable SPARQL, but unchecking it won't help you run a query with fatal errors. I personally find the SPARQL compiler's error feedback less helpful than the feedback from Oracle's sql compiler, but Oracle has been around a lot longer than SPARQL.


EXECUTION TIMEOUT IN MILLISECONDS

I haven't used it.


RUN QUERY

When you click the Run Query button you can see the output of your query in the format your specified, in this example, HTML. Such a raw dump of all the distinct concepts in dbpedia shows an enormous number of concepts that appear unintelligible. Farther down the list you see concepts that make sense even at a casual glance.

See the results.

Monday, August 16, 2010

All Simpson's Blackboard gags - Filtered and Ordered

FILTER AND ORDER THE RESULTS

There are extra rows showing because a season sometimes has more than 1 label assigned to it. We now want to FILTER the rows to just those containing The Simpsons episodes in the season title.

THE SPARQL QUERY - FILTERED AND ORDERED

SELECT distinct ?season_title,?episode,?chalkboard_gag
WHERE 
{
 ?episode
   <http://www.w3.org/2004/02/skos/core#subject>
     ?season
 .
 ?episode
   <http://dbpedia.org/property/blackboard>
     ?chalkboard_gag
 .
     ?season
       <http://www.w3.org/2000/01/rdf-schema#label>
         ?season_title
     .
    FILTER(regex(?season_title,"The Simpsons episodes","i")).
}
ORDER BY ?season_title
Show results

FILTER WITH REGULAR EXPRESSIONS

13)FILTER(regex(?season_title,"The Simpsons episodes","i")).

  • Line 13) FILTER lets us restrict the rows in our output to only those that make our expression true. These are similar to Oracle's where clauses. In this example we used the regex function in a very simple way to search ?season_title for the string, The Simpsons episodes, ignoring any variations in uppercase or lower case letters.

ORDER THE RESULTS

15) ORDER BY ?season_title

Line 15) We would like to order by season title, so we see the gags over time. This command orders the title as a string, at least keeping the seasons together, but putting season 1 and season 11 before season 2.
We could work harder to isolate the number at the end of the season and sort on its
numeric representation, but let's not.
Some episodes have an airdate, the first time the episode was shown, but the data item is too sparse to order all our results. Let's move on.

All Simpson's Blackboard gags - With Season Title

DISPLAY THE SEASON LABEL







THE QUERY DIAGRAM


THE SPARQL QUERY - WITH THE SEASON TITLE

SELECT distinct ?season_title,?episode,?chalkboard_gag
WHERE 
{
 ?episode
   <http://www.w3.org/2004/02/skos/core#subject>
     ?season
 .
 ?episode
   <http://dbpedia.org/property/blackboard>
     ?chalkboard_gag
 .
   ?season
       <http://www.w3.org/2000/01/rdf-schema#label>
         ?season_title
     .
}
Show results

PICK UP THE SEASON TITLE

12)?season
13) <http://www.w3.org/2000/01/rdf-schema#label>
14) ?season_title
15).


Line 12) For each season
Line 13) return the it's user-friendly label
Line 14) and call it ?season_title.

Friday, July 30, 2010

All Simpson's Blackboard gags

Let's expand our second query and get all the blackboard gags from all the Simpson seasons and episodes.

THE QUERY DIAGRAM


THE SPARQL QUERY - ANY EPISODE ANY SEASON WITH A BLACKBOARD GAG

SELECT distinct ?episode,?chalkboard_gag
WHERE 
{
 ?episode
   <http://www.w3.org/2004/02/skos/core#subject>
     ?season
 .
 ?episode
   <http://dbpedia.org/property/blackboard>
     ?chalkboard_gag
 .
}
Show results.

GET ANY EPISODE - ANY SEASON

4) ?episode  
5)  <http://www.w3.org/2004/02/skos/core#subject>  
6)   ?season  
7) .
 
Note: I'll only discuss the newest items of interest instead of endlessly repeating that the select clause lists the variables you want to see.
  • Line 4) The subject we are talking about.
  • Line 5) We already learned that the skos:subject referred to the season this episode belonged to.
  • Line 6)Instead of limiting our results to The Simpsons season 12, we will put in ?season, meaning any season.
  • Line 7) The period marks the end of the first selection clause.
  • Recapping, lines 4-7 ask the SPARQL engine to search all off dbpedia to find the page(s) that I will call ?episode that have a skos:subject value of anything. We will call that anything a season. We will have to count on the 2nd clause to restrict our results to ?episodes that have a blackboard variable for the blackboard gag.

LETS CHECK OUR RESULTS

The results look like Simpson blackboard gags, but lets make sure.

Thursday, July 29, 2010

The Simpson's Blackboard gags for Season 12

Let's expand your first query and get all the blackboard gags from the 12th Simpson season.

STARTING FROM ONE EPISODE

  • We'll start with the first sparql query's dbpedia page, the dbpedia version of The Worst Episode.
  • Here are the dbpedia page's sections identifying the episode, the blackboard gag for this episode, and lastly, the season this episode is part of. We will use this information for our query.



Note: click any picture to enlarge it.

THE QUERY DIAGRAM

THE SPARQL QUERY - SEASON 12 EPISODES WITH BLACKBOARD GAGS

SELECT distinct ?episode,?chalkboard_gag
WHERE 
{
 ?episode
   <http://www.w3.org/2004/02/skos/core#subject>
     <http://dbpedia.org/resource/Category:The_Simpsons_episodes%2C_season_12>
 .
 ?episode
   <http://dbpedia.org/property/blackboard>
     ?chalkboard_gag
 .
}
Show results.

SPECIFY YOUR OUTPUT VARIABLES

1) SELECT distinct ?episode,?chalkboard_gag
  • The select clause specifies what fields you want in your output.
  • The distinct keyword removes duplicate values of ?episode - ?chalkboard_gag combinations.
  • I specified the same two variables, ?episode and ?chalkboard_gag.

START OF YOUR SELECTION CRITERIA

2) WHERE { . . . } Enough said.

IDENTIFY THE EPISODES IN SEASON 12







 
4) ?episode  the subject
5)  <http://www.w3.org/2004/02/skos/core#subject>  the predicate
6)   <http://dbpedia.org/resource/Category:The_Simpsons_episodes%2C_season_12>  the object
7) .
 
  • Line 4) The subject we are talking about.
  • Line 5) At first sight, skos:subject didn't mean much to me, but when I saw its value refered to the Simpson's season 12, I figured it had to describe the season that this episode belonged to. I copied the link's value into my sparql query.
  • Line 6) For the object, I picked up the value of the long link to the right of skos:subject.
  • Line 7) The period marks the end of the first selection clause.
  • Recapping, lines 4-7 ask the SPARQL engine to search all off dbpedia to find the page(s) that I will call ?episode that have a skos:subject value of http://dbpedia.org/resource/Category:The_Simpsons_episodes%2C_season_12.

GET THE BLACKBOARD GAG


8) ?episode  the subject 
9)  <http://dbpedia.org/property/blackboard>  the predicate 
10)   ?chalkboard_gag  the object 
11) .

  • Line 8) By using the same variable, ?episode as subject in lines 4 and 8, I am telling SPARQL that I want to see the blackboard gag for the episode defined above.
  • Line 9) I copied the link,dbprop:blackboard from the dbpedia page as mentioned before.
  • Line 10) I specified the object as a variable called ?chalkboard_gag, meaning pick it up from the page (or really from the RDF data underlying the page).

    This will now show different blackboard gags for different episodes.
  • Line 11) End of the 2nd search clause.
  • Line 12)The closing bracket ends your selection criteria.

LETS CHECK OUR RESULTS

So that's your second query. Let's compare our results. versus The object link to the Simpson episodes season 12.

We have a little problem. Our results show 7 gags and season 12 had 21 episodes. What gives?
Not all the episodes had blackboard gags, so lets ask for each episode in season 12 and then optionally show the blackboard gag if it exists.

SPARQL QUERY WITH THE BLACKBOARD GAG OPTIONAL

SELECT distinct ?episode,?chalkboard_gag
WHERE 
{
 ?episode
   <http://www.w3.org/2004/02/skos/core#subject>
<http://dbpedia.org/resource/Category:The_Simpsons_episodes%2C_season_12>
 .
 OPTIONAL
 { 
 ?episode
   <http://dbpedia.org/property/blackboard>
     ?chalkboard_gag
 .
 } # end of optional blackboard gag
} # end of where clause

Show results
Now there are 21 episodes and still the same 7 blackboard gags. That clears up the mystery.