Extract Chemical Data from DBpedia via SPARQL - sparql

I'd like to know how to submit a SPARQL query to DDBpedia and be given back a table that includes the information found in the Wikipedia "chembox" info box template, such as molecular weight or formula.
So, the first step was just to make a query whose results should be a list of chemical substances that had the formula and molecularWeight properties included. But the following returns no results:
SELECT * WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y rdfs:label ?Name .
?y dbpedia:molecularWeight ?molecularWeight .
?y dbpedia:formula ?formula .
OPTIONAL {?y dbpedia-owl:iupacName ?iupacname} .
FILTER (langMatches(lang(?Name),"en"))
}
LIMIT 50
SPARQL Explorer at dbpedia.org
And so I'm stuck. Is something wrong with this query or does DBPedia really not collect that information from the Wikipedia chemboxes?

You caught the wrong namespace for both dbpedia:molecularWeight and dbpedia:formula. The correct namespace here would be dbpedia2.
Furthermore, there seem rarely any entries having a dbpedia-owl:iupacName, dbpedia2:molecularWeight and dbpedia2:formula.
SELECT * WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y rdfs:label ?Name .
OPTIONAL {?y dbpedia2:formula ?formula }.
OPTIONAL {?y dbpedia2:molecularWeight ?molecularWeight}.
OPTIONAL {?y dbpedia-owl:iupacName ?iupacname} .
FILTER (langMatches(lang(?Name),"en"))
}
LIMIT 50
SPARQL Explorer #dbpedia.org
To get the correct namespaces, you could either look at one example like this or get a list of all used properties for type dbpedia-owl:ChemicalSubstance using
SELECT DISTINCT ?rel WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y ?rel ?x
}
SPARQL Explorer #dbpedia.org

Related

How to catch correct resource among disambiguations / interrogate subresources in DBPedia using SPARQL?

I have the title of a Youtube music video and I need to gather additional information from DBPedia like album, artist, release date, etc. I'd also like to get the 'abstract' of all these entities from DBPedia. I already use Musicbrainz to differentiate between song and artist in the title and it works rather well, except a few cases.
However my main problem is: when I pass the song to ask DBPedia (using a query with resource/{song}), sometimes I get no answer because {song} causes disambiguation. Example: resource/It's_My_Life has 11 disambiguates, 6 of which are songs. I need the resource "It's_My_Life_(Bon_Jovi_song)". How can I tell DBPedia that I need a resource of 'MusicalWork' type of a certain artist?
I tried in many ways to do this with SPARQL, but I always get an empty result and I don't know what I'm doing wrong. So far I was able to only get the abstract of every disambiguation, but I cannot get a specific property (like abstract) of a subresource of a disambiguation.
SELECT ?x ?y WHERE {
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x .
?x dbo:abstract ?y .
}
I can't seem to go father than this. I tried with:
SELECT ?x ?y WHERE {
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x .
?x dbo:MusicalArtist ?y .
{ SELECT ?z WHERE {
?y dbo:abstract ?z}
}
}
and
SELECT ?x ?y WHERE {
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x
?x rdfs:type ?y .
}
But results are always empty. How am I supposed to interrogate a subresource of a resource? Can anyone help me?
Solution, pulled from comments by #AKSW --
SELECT ?x ?y
WHERE
{
<http://dbpedia.org/resource/It's_My_Life> dbo:wikiPageDisambiguates ?x .
?x rdf:type dbo:MusicalWork .
?x dbo:abstract ?y .
FILTER ( LANGMATCHES ( LANG (?y), 'en' ) )
}

How to query for publication date of books using SPARQL in DBPEDIA

I am trying to retrieve the publication date and the no.of pages for books in DBpedia. I tried the following query and it gives empty results. I see that these are properties under book(http://mappings.dbpedia.org/server/ontology/classes/Book) but could not retrieve it.
I would like to know if there is an error in the code or if dbpedia does not store these dates related to books.
SELECT ?book ?genre ?date ?numberOfPages
WHERE {
?book rdf:type dbpedia-owl:Book .
?book dbp:genre ?genre .
?book dbp:firstPublicationDate ?date .
OPTIONAL {?book dbp:numberOfPages ?numberOfPages .}
}
The dbp:firstPublicationDate does not work for two reasons:
First, as pointed in the first answer, you used the wrong prefix.
But even if you correct it, you'll see that you would still have no results. Then the best thing to do is to test with the minimum number of patters, in you case you should as for books with first publication date, two triple pattern only. If you still don't get results, you should test how <http://dbpedia.org/ontology/firstPublicationDate> is actually used with a query like this:
SELECT ?class (COUNT (DISTINCT ?s) AS ?instances)
WHERE {
?s <http://dbpedia.org/ontology/firstPublicationDate> ?date ;
a ?class
}
GROUP BY ?class
ORDER BY DESC(?instances)
LIMIT 1000
Mapping based properties are using the namespace http://dbpedia.org/ontology/, thus, the prefix must be dbo instead of dbp, which stands for http://dbpedia.org/property/.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?book ?genre ?date ?numberOfPages
WHERE {
?book a dbo:Book ;
dbp:genre ?genre ;
dbo:firstPublicationDate ?date .
OPTIONAL {?book dbp:numberOfPages ?numberOfPages .}
}
Some additional comments:
put the prefixes to the SPARQL query such that others here can run it without any exceptions (also in the future) - the current SPARQL query uses dbpedia-owl but this one is not pre-defined on the official DBpedia anymore - it's called dbo instead
which brings me to the second point -> if you're using a public SPARQL endpoint, show its URL
you can start debugging your own SPARQL query by simply starting with only parts of it and adding more triple patterns then, e.g. in your case you could check if there is any triple with the property with
PREFIX dbp: <http://dbpedia.org/property/>
SELECT * WHERE {?book dbp:firstPublicationDate ?date } LIMIT 10
Update
As Ivo Velitchkov noticed in his answer below, the property dbo:firstPublicationDate is only used for mangas, etc., i.e. written work that was published periodically. Thus, the result will be empty.

Listing Properties and Values of an Individual on Dbpedia

How can I list properties with their values for any given DBpedia class? I'm new to this and have looked at several other questions on this but I haven't found exactly what I'm looking for.
What I'm trying to do is providing some relevant additional information to topics of conversation I have got from text mining.
Say for example the topic of conversation in a certain community is iPhones. I would like to use this word to query the DBpedia page for this word, IPhone, to get an output such as:
Type: Smartphone
Operating System: IOS
Manufacturer: Foxconn
EDIT:
Using the query from AKSW I can print the p (property?) and o (object?), although I'm still not getting the output I want. Instead of getting something like:
weight: 133.0
I get
http://dbpedia.org/property/weight:133.0
Is there a way to just get the name of the property instead of the DBpedia link?
My Code
Classes do not "have" properties with values. Instances (resp. resources or individuals) do have a relationship via a property to some value which can be an individual itself or a literal (or some anonymous instance aka blank node). And instances belong to a class. e.g. Berlin belongs to the class City
What you want is to get all outgoing values of a given resource in DBpedia:
SELECT * WHERE { <http://dbpedia.org/resource/IPhone> ?p ?o }
Alternatively, you can use SPARQL DESCRIBE, which return the data in forms of an RDF graph resp. a set of RDF triples:
DESCRIBE <http://dbpedia.org/resource/IPhone>
This might also return incoming information because it's not really specified in the W3C recommendation what has to be returned.
As stated by AKSW properties often link to other classes rather than values. If you want all properties and their values, including other classes the the below gives you the label and filters by language (put the language code you need where have put "en").
SELECT DISTINCT ?label ?o
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
If you don't want any properties that link to other classes, then you only want datatype properties so this code could help:
SELECT DISTINCT ?label ?o
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?p a owl:DatatypeProperty .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
Obviously this gives you far less information and functionality, but it might just be what you're after?
Edit: In reply to your comment, it is also possible to get the labels for the values, using the same technique:
SELECT DISTINCT ?label ?oLabel
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?o <http://www.w3.org/2000/01/rdf-schema#label> ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
Note that http://www.w3.org/2000/01/rdf-schema#label is often shortened to rdfs:label by defining prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
So you could also do:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?label ?oLabel
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p rdfs:label ?label .
?o rdfs:label ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
and get exactly the same result but possibly easier to read.

DBpedia Sparql by page template

I am trying to run a query on dbpedia using SPARQL syntax, to look for all pages of a certain template. Doesn't seem to be work, I am looking for all pages with dbpprop:wikiPageUsesTemplate. Does anyone know how to correct this to properly look for templates?
SELECT ?name ?member_Of ?country ?lat ?lng ?link
WHERE {
?x dbpprop:wikiPageUsesTemplate "dbpedia:Template:Infobox_settlement" .
?x a <http://dbpedia.org/ontology/Settlement> .
?x foaf:name ?name .
?x dbpedia-owl:isPartOf ?member_Of.
?x dbpedia-owl:country ?country.
?x geo:lat ?lat .
?x geo:long ?lng .
?x foaf:isPrimaryTopicOf ?link .
}
LIMIT 2500 OFFSET 0
I've also attempted to run it just by the dbprop to no avail.
SELECT * WHERE { ?page dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> . ?page dbpedia:name ?name .}
If anyone is trying to do a similar thing, it is also possible via the wiki api, where you can pagananate over all results. http://en.wikipedia.org/w/api.php?action=query&list=embeddedin&eititle=Template:Infobox_settlement
There are at least two problems: (i) you need to use IRIs in places, and not strings; and (ii) you need to use properties that DBpedia uses.
Use IRIs
In
?x a <http://dbpedia.org/ontology/Settlement> .
and
?x foaf:isPrimaryTopicOf ?link .
you've demonstrated that you know that URIs need to be written in full with < and >, or abbreviated with a prefix. However,
?x dbpprop:wikiPageUsesTemplate "dbpedia:Template:Infobox_settlement" .
certainly isn't going to work. It's legal SPARQL, because a string can be the object of a triple, but you almost certainly want an IRI.
Use DBpedia's vocabulary
A query with dbpprop:wikiPageUsesTemplate like this returns no results:
select distinct ?template where {
[] dbpprop:wikiPageUsesTemplate ?template
}
SPARQL results
That's easy enough to check, and quickly confirms that there's no data that can possibly match your query. Where did you find this property? Have you seen it used somewhere? I'm not confident that you can query DBpedia based on infobox templates. DBpedia is not the same as Wikipedia, and even if the Wikipedia API supports it, it doesn't mean that DBpedia has a counterpart. There is a note on DBpedia Data Set Properties that says:
http://xx.dbpedia.org/property/wikiPageUsesTemplate (will be changed to http://dbpedia.org/ontology/wikiPageUsesTemplate)
but that latter property doesn't seem to be in use on the endpoints either. See my answer to Syntax for Sparql query for pages with specific infobox for more details.

How to query an RDF individual for its data properties?

I have an ontology where arc_cfp is an individual of class Arc. I would like to know how could I get all the data properties of the individual, given that I have the individual's URI?
Basically, I am doing this:
SELECT ?idRef ?name ?src ?dst ?perf
WHERE
{
?x rdf:type http://www.semanticweb.org/ontologies/2012/1/graph.owl#arc_cfp .
?x graph:idRef_arc ?idRef .
?x graph:name_arc ?name .
?x graph:hasSource ?src .
?x graph:hasDestination ?dst .
?x graph:hasPerformatif ?perf .
}
I am pretty sure, using rdf:type is the problem. But, I have no idea what I need to use.
Thanks.
~Codera
Assuming you want a purely exploratory query of the form "give me all the triples about a subject" it should look the following:
SELECT *
WHERE
{
<http://example.org/SomeThing> ?p ?o
}
This will give you all predicate object pairs associated with the constant URI you pass in. If you are interesting in incoming as well as outgoing properties you could do the following instead:
SELECT *
WHERE
{
{ <http://example.org/SomeThing> ?p ?o }
UNION
{ ?s ?p <http://example.org/SomeThing> }
}
You can also use a DESCRIBE query to grab all the RDF data about a Resource.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
DESCRIBE ?x
WHERE
{
?x rdf:type http://www.semanticweb.org/ontologies/2012/1/graph.owl#arc_cfp .
}
P.S. Don't forget to put prefixes in your queries.