I am using lookup in dbpedia such as this http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=all%20by%20myself to extract all results containing the text "All by myself" but now I only want to extract those results in which the description contains the words "Eric Carmen" or better if it can do a fuzzy selection such as pick both "Eric Carmen" and "Eric Karmen" in the description. Can anyone build a SPARQL for this? I have attached the result of the above url in this image. I have attached an answer from another query from stackoverflow Work around to query DBpedia for two keywords to help you understand.
You can start with this query which returns all resources that contain your keyword in the abstract property:
SELECT Distinct *
Where
{
?resource dbo:abstract ?abstract.
Filter langMatches(lang(?abstract),"EN").
?abstract bif:contains "Eric_Carmen".
}
You can consider http://dbpedia.org/ontology/abstract as description of any resource in Dbpedia.
Related
I notice that some books on Wikidata have Project Gutenberg ebook IDs. Is it possible to construct a query on the Wikidata Query Service that looks for all books that have this property? I'm new to SPARQL.
As you can see if you hover over the link for the Project Gutenberg e-book Id on https://www.wikidata.org/wiki/Q6511, the actual property code to be used in SPARQL is https://www.wikidata.org/wiki/Property:P2034, or wdt:P2034 for short.
To retrieve all items with e-book IDs through SPARQL is then simply a matter of doing a basic triple match:
SELECT ?work ?bookId
WHERE { ?work wdt:P2034 ?bookId }
Of course you can extend this query how you see fit. For example to retrieve the (English) title of the book instead of just the identifier, you can do something like this:
SELECT ?title ?ebook_id
WHERE {
?work rdfs:label ?title;
wdt:P2034 ?ebook_id.
FILTER(langMatches(lang(?title), "en"))
}
It's all a matter of adding the right combination of triple patterns and filters.
I need some help in a query.
I have a list of names and I want to write a program in python that will send a query for each person on the list and it will look for his information in dbpedia and in wikidata and will return some of it.
Can someone help me with this one?
Thanks
The following working python code takes the (slightly modified) query from #coder and uses it in a small demo program.
It will search for people with "Andreotti" as part of their name, and return one or more URLs each pointing to the page for one person found.
from SPARQLWrapper import SPARQLWrapper, JSON
NAME_FRAGMENT = "Andreotti"
QUERY = f"""
SELECT DISTINCT ?uri
WHERE {{
?uri a foaf:Person.
?uri ?p ?person_full_name.
FILTER(?p IN(dbo:birthName,dbp:birthName ,dbp:fullname,dbp:name)).
?uri rdfs:label ?person_name .
?person_name bif:contains "{NAME_FRAGMENT}" .
FILTER(langMatches(lang(?person_full_name), "en")) .
}}
LIMIT 100
"""
# Specify the DBPedia endpoint
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(QUERY)
sparql.setReturnFormat(JSON)
# Run the query
result = sparql.query().convert()
# The return data contains "bindings" (a list of dictionaries)
for link in result["results"]["bindings"]:
# We want the "value" attribute of the "comment" field
print(link["uri"]["value"])
Please note that as far as I understand the names are always scraped from the English wikipedia pages and therefore you will only find the English rendition (I stand to be corrected if this is not true).
Hi I'm trying to learn how to query DBpedia using SPARQL. I can't find any website/source that shows me how do this and I'm finding it difficult to learn how to use all the properties (like the ones available at http://mappings.dbpedia.org/index.php?title=Special%3AAllPages&from=&to=&namespace=202 ). Any good source I can learn from?
So for example if I want to check if the wikipedia page http://en.wikipedia.org/wiki/Inception is a movie (property film) or not, how do I do that?
The wikipedia URL http://en.wikipedia.org/wiki/Inception maps to the dbpedia URI http://dbpedia.org/resource/Inception. Dbpedia has a SPARQL endpoint at: http://dbpedia.org/sparql, which you may use to run queries either programmatically or via the html interface.
To check if http://dbpedia.org/page/Inception is a "movie", you have many options. To give you an idea:
If you know the URI of "movie" in dbpedia (it is http://schema.org/Movie), then run an ASK query to check against that type. ASK will return true/false based on whether the pattern in the where clause is valid against the data:
ASK where {
<http://dbpedia.org/resource/Inception> a <http://schema.org/Movie>
}
If you don't know the URI of "movie" then you have a number of options. For example:
Execute an ASK query with a filter on whether the resource has a type that contains the word "movie" somewhere in its uri (or its associated rdfs:label, or both). You would use a regular expression for this:
ASK where {
<http://dbpedia.org/resource/Inception> a ?type .
FILTER regex(str(?type), "^.*movie", "i")
}
Same idea, but return all matches and post-process the results (programmatically I pressume) to see if they match your request:
select distinct ?type where {
<http://dbpedia.org/resource/Inception> a ?type .
FILTER regex(str(?type), "^.*movie", "i")
}
Return all the types of the resource without applying a filter and post-process to see if they match your request:
select distinct ?type where {
<http://dbpedia.org/resource/Inception> a ?type
}
Many options. The SPARQL spec is you number one resource.
First I suggest you start reading up on what exactly SPARQL is. There are tons of really good tutorials such as: this.
If you want to write SPARQL queries on dbpedia, there are various endpoints that you can use. They don't always accept all features that are supported by SPARQL, but if you don't want to go through the trouble of installing one locally, they can be a relatively reliable test environment. The queries that I am going to write below, have been tested on Virtuoso endpoint.
Let's say you want to find all the movies in dbpedia. You first need to know what is the URI for a movie type in dbpedia. If you open Inception in dbpedia, you can see that the type dbpedia-owl:Film is associated to it. So if you want to get the first 100 movies, you just need to call:
select distinct *
where {
?s ?o dbpedia-owl:Film
} LIMIT 100
If you want o know more about each of these movies, you just need to expand your queries by expanding the triples.
select distinct *
where {
?s ?p dbpedia-owl:Film.
?s ?x ?y.
} LIMIT 100
I am trying to collect Albums and the list of songs per Album. For example for the Album
River_of_Tuoni, the dbpprop:title property provides the list of the songs but only two of them are in DBpedia.
so when i do the following query , I only get two, (My_Only_car,River_of_Tuoni_(song)).
SELECT ?songs
WHERE {
:River_of_Tuoni dbpprop:title ?songs. ?songs a dbpedia-owl:Song .
}
How can i get all the songs (Lullaby, Sunrise,...)?
dbpprop:title property provides the list of the songs but only two of
them are in DBpedia.
The dbprop:title property doesn't provide any list; there just happen to be a number of triples with that property. All the data is in DBpedia; the values you've shown in the screenshot indicate that two of the values happen to be URIs, while other values happen to be strings. In general, the data with the dbpprop: properties is "dirtier" than the dbpedia-owl: properties, so getting two different types of values is not very surprising.
The simple query
select ?title where {
dbpedia:River_of_Tuoni dbpprop:title ?title
}
SPARQL results
returns all the titles. However, because the dbpedia-owl: properties have cleaner data, I'd usually suggest that you consider using one of those instead of dbpprop:title, but in this case it doesn't look like there's an alternative. You might consider this alternate query to get just strings, though:
select ?title where {
dbpedia:River_of_Tuoni (dbpprop:title/rdfs:label)|dbpprop:title ?title
filter isLiteral(?title)
}
SPARQL results
For the cases where the value is another resource, taking the rdfs:label of the resource provides a string. (I think that this should also be achievable with dbprop:title/(rdfs:label?), but that doesn't work with the DBpedia endpoint.)
Say I need to fetch content from wikipedia about all mountains. My target is to show initial paragraph, and an image from respective article (eg. Monte Rosa and Vincent Pyramid.
I came to know about dbpedia, and with some research got to find that it provides live queries into wiki database directly.
I have 2 questions:
1 - I am finding it difficult how could I formulate my queries. I can't play around iSPARQL. I tried following query but it throws error saying invalid xml.
SELECT DISTINCT ?Mountain FROM <http://dbpedia.org> WHERE {
[] rdf:type ?Mountain
}
2 - My requirement is to show only mountains that have at least 1 image (I need to show this image too). Now the ones I listed above have images, but how could I be sure? Also, looking at both examples I see many fields differ in wiki articles - so for future extension it maybe quite difficult to fetch them.
I just want to reject those which do not have sufficient data or description.
How can I filter out mountains based on pictures present?
UPDATE:
My corrected query, which solves my first problem:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?name ?description
WHERE {
?name rdf:type <http://dbpedia.org/ontology/Mountain>;
dbpedia-owl:abstract ?description .
}
You can also query dbpedia using its SPARQL endpoint (less fancy than iSPARQL). To find out more about what queries to write, take a look at the DBpedia's datasets page. The examples there show how one can select pages based on Wikipedia categories. To select resources in the Wikipedia Mountains category, you can use the following query:
select ?mountain where {
?mountain a dbpedia-owl:Mountain .
}
SPARQL Results
Once you have some of these links in hand, you can look at them in a web browser and see the data associated with them. For instance the page for Mount Everest shows lots of properties. For restricting results to those pages that have an image, you might be interested in the dbpedia-owl:thumbnail property, or perhaps better yet foaf:depiction. For the introductory paragraph, you probably want something like the dbpedia-owl:abstract. Using those, we can enhance the query from before. The following query finds things in the category Stratovolcanoes with an abstract and an depiction. Since StackOverflow is an English language site, I've restricted the abstracts to those in English.
select * where {
?mountain a dbpedia-owl:Mountain ;
dbpedia-owl:abstract ?abstract ;
foaf:depiction ?depiction .
FILTER(langMatches(lang(?abstract),"EN"))
}
LIMIT 10
SPARQL Results