SPARQL Query to get a company by name if it contains text - sparql

I'm trying to get a company by name if it contains text
Example: honda motor co ltd
Maps to: https://www.wikidata.org/wiki/Q9584
I have this query that finds apple successfully
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
select distinct ?iri ?logo ?description {
?iri a dbpedia-owl:Company ;
dbpedia-owl:abstract ?description ;
rdfs:label ?lbl ;
foaf:depiction|dbpedia-owl:thumbnail ?logo .
?lbl bif:contains "'apple'"#en .
filter( langMatches(lang(?description),"en") )
}
Which returns results since it matches 'Apple Inc'
http://dbpedia.org/page/Apple_Inc.
But this query below doesn't match Accenture
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
select distinct ?iri ?logo ?description {
?iri a dbpedia-owl:Company ;
dbpedia-owl:abstract ?description ;
rdfs:label ?lbl ;
foaf:depiction|dbpedia-owl:thumbnail ?logo .
?lbl bif:contains "'accenture'"#en .
filter( langMatches(lang(?description),"en") )
}
I expect: http://dbpedia.org/page/Accenture
but get nothing

Not every resource in RDF must have every property! In your case, the logo doesn't exist which means you should make it an optional feature:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?iri ?logo ?description {
?iri a dbpedia-owl:Company ;
dbpedia-owl:abstract ?description ;
rdfs:label ?lbl .
?lbl bif:contains "'accenture'"#en .
FILTER( langMatches(lang(?description),"en") )
OPTIONAL {?iri foaf:depiction|dbpedia-owl:thumbnail ?logo }
}

Related

How to access the value of the property from DBPedia using SPARQL

I wanted to access dbt:Cricket_by_country(value) inside of the dbp:wikiPageUsesTemplates (Property), but I am not able to access it. I am very new to the SPARQL
Below is my code
PREFIX cia: <http://www.semanticweb.org/neelc/ontologies/countries#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia-populated-place: <http://dbpedia.org/ontology/PopulatedPlace/>
PREFIX dbcat:<http://dbpedia.org/resource/Category>
select distinct ?name , ?sizeOfTeam, ?equipment, ?TeamCountry, ?Country
where
{
?cricket dct:subject <http://dbpedia.org/resource/Category:Cricket>.
?cricket dbp:name ?name.
?cricket dbo:teamSize ?sizeOfTeam.
?cricket dbo:equipment ?equipment.
?cricket dbp:wikiPageUsesTemplate ?TeamCountry .
?cricket dbp:TeamCountry.dbt:Cricket_by_country ?Country
FILTER (LANG(?name)="en")
FILTER regex(str(?cricket), "/resource/Cricket")
}

Federated queries on dbpedia and linkedmdb

I am writing a federated query to get the books based on films in dbpedia and in turn using the film name from dbpedia to retrieve the corresponding imdblink link for the same. I am getting an empty set when I add the service of linkedmdb.
Here is my code
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT distinct ?name ?author ?filmname ?imdbID WHERE {
SERVICE <http://dbpedia.org/sparql> {
?book rdf:type dbo:Book .
?book foaf:name ?name .
?book dbp:author ?author .
?author foaf:name ?authname .
?book ^dbo:basedOn ?movie .
?movie a dbo:Film .
?movie foaf:name ?filmname
FILTER (str(?name) IN ("Royal Flash","White Oleander", "Possession: A Romance", "Misery", "Intensity", "The War of The Roses", "Momo", "The Sicilian", "Derailed", "Ragtime"))
}
SERVICE <http://data.linkedmdb.org/sparql> {
?filmname foaf:page ?imdbID .
?filmname dc:title ?title .
FILTER(regex(str(?imdbID), "www.imdb.com" ) )
}
}
I am using the following endpoint http://www.sparql.org/query.html
That does not work because of the ?filename variable
in the first SERVICE clause you get
?movie foaf:name ?filmname, thus, those are string literals
in the second SERVICE clause ?filmname are URIs
It will become easy to see if you run both SPARQL queries separately.
What you would have to do is to match on the title, i.e. change
?filmname dc:title ?title .
to
?filmname dc:title ?filmname .
Note, this might not work because of different types of strings, e.g. with and without language tag etc.
In that case you would have to match on the lexical form again, i.e. use temporary variables ?filmname1 and ?filmname2 and use
FILTER(str(?filmname1) = str(?filmname2))
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/terms/>
SELECT DISTINCT ?name ?author ?filmname1 ?imdbID
WHERE
{ SERVICE <http://dbpedia.org/sparql>
{ ?book rdf:type dbo:Book ;
foaf:name ?name ;
dbp:author ?author .
?author foaf:name ?authname .
?book ^dbo:basedOn ?movie .
?movie rdf:type dbo:Film ;
foaf:name ?filmname1
FILTER ( str(?name) IN ("Royal Flash", "White Oleander", "Possession: A Romance", "Misery", "Intensity", "The War of The Roses", "Momo", "The Sicilian", "Derailed", "Ragtime") )
}
SERVICE <http://data.linkedmdb.org/sparql>
{ ?filmname foaf:page ?imdbID ;
dc:title ?filmname2
FILTER regex(str(?imdbID), "www.imdb.com")
}
FILTER ( str(?filmname1) = str(?filmname2) )
}

Sparql syntax error on nested select

I am trying to select City with highest population. My Query looks like this:
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?population ?capital
WHERE {
?x dct:subject dbc:Countries_in_Europe .
?x dbo:capital ?capital .
?capital dbo:populationTotal ?population .
FILTER (?population =
(
SELECT MAX(?popul) AS ?pop
WHERE{
?capital dbo:populationTotal ?popul .
}
)
)
}
LIMIT 200
I haved followed This PDF page 5.
I am using apache-jena-fuseki-2.4.0 and getting a syntax error
Encountered " "select" "SELECT "" at line 18, column 1.
Any ideas where the problem is?
I don't know where you have seen that you can bind the result of a SELECT query to a variable in the FILTER, but that's totally wrong. This might work for SQL but not for SPARQL.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
SELECT ?population ?capital
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?population
FILTER ( ?population = ?pop )
# sub-SELECTs are evaluated first and encapsulated by { }
# in addition (as to TomP's comment), you have to restrict to European captials here
{ SELECT (MAX(?popul) AS ?pop)
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?popul }
}
}
LIMIT 200
But, if you really want to have the "capital in Europe with the highest population", why not using the easier and more obvious way which reflects your task, i.e. ORDER BY + LIMIT 1:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
SELECT ?population ?capital
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?population
}
ORDER BY DESC(?population)
LIMIT 1

How to query DBpedia SPARQL by resource uri?

I'm querying DBpedia types in SPARQL (http://dbpedia.org/sparql) by resource's label
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX : <http://dbpedia.org/resource/>
PREFIX ru: <http://ru.dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?type ?superType WHERE { {
?res rdfs:label "HarryPotter"#en.
} UNION {
?redir dbo:wikiPageRedirects ?res .
?redir rdfs:label "HarryPotter"#en .
}
?res rdf:type ?type .
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
It works fine.
But what if I know the exact resource - http://dbpedia.org/page/Harry_Potter? I tried something like:
?res a :Harry_Potter.
But it does not work.
How to query DBpedia types and supertypes if I know the resource URI? I can't figure out which property or operator I should use (e.g., rdfs:Resource, a, etc., which do not work)
When you write
?res a :Harry_Potter.
It doesn't work, because this means "a resource, which is of type :Harry_Potter". It is equivalent to
?res rdf:type :Harry_Potter.
:Harry_Potter identifies a resource and not the type, thus it should be used in place of ?res.
Also I think you mean Harry_Potter_(character), because that is the actual identifier and not redirect.
You query would be as simple as
SELECT ?type ?superType WHERE
{
# give me ?type of the resource
<http://dbpedia.org/resource/Harry_Potter_(character)> rdf:type ?type .
# give me ?superTypes of ?type
OPTIONAL {
?type rdfs:subClassOf ?superType .
}
}
You can just put the URI as the subject in there WHERE conditions.
SELECT ?title, ?releaseDate
WHERE {
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbp:title ?title .
<http://dbpedia.org/resource/Super_Mario_Bros._3> dbo:releaseDate ?releaseDate .
}

How do get city population by name with SPARQL

I need to get city population by its name. I'm sure SPARQL could provide this, but I'm not sure how to write the query. I have a query which provides COuntry's capital and coordinates by it's name, so I assume it should be something similar. HELP!
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?country ?population ?capital ?lat ?long WHERE {
?country a o:Country ; foaf:name "Germany"#en; o:capital [ geo:lat ?lat ; geo:long ?long ; p:name ?capital ]
}
All you need is:
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?pop WHERE {
?country a o:Country ;
foaf:name ?name ;
p:populationEstimate ?pop .
}
The only tricky part was finding out the name of the property that links Countries and Populations.