Sparqlwrapper: Its not returning anything but the query is working with dbpedia.org/sparql - python-3.x

I have also added the snippet below. This query will return the name of the company uri, its name and parent company. It is working with DBpedia.org/sparql but not with sparqlwrapper(not returning anything; )
query1 = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX db: <http://dbpedia.org/resource/>
select distinct ?iri ?name ?parent (concat('[',group_concat(distinct ?location;separator=','),']') as ?location)
{
?iri a dbo:Company ;
rdfs:label ?label ;
foaf:name ?name
OPTIONAL { ?iri dbo:parentCompany ?parent.
filter (!isBlank(?parent)) }
OPTIONAL { ?iri dbo:location ?location.}
filter(regex(?name, "\\btata steel\\b","i" )) .
filter(regex(?label, "\\btata steel\\b","i" ))
}
GROUP BY ?iri ?name ?parent
"""
def get_country_description(query1):
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setReturnFormat('json')
sparql.setQuery(query1) # the previous query as a literal string
return sparql.query().convert()

get rid of \\b in your regex expression, and it works! (i dont know why it is needed as tata steel is a whole word)
Also you need to pass query1! to your function!
and use sparql.setReturnFormat('json') not JSON,
cheers

Related

How to escape brackets in SPARQL string?

I'm trying to make a sparql query to: http://sparql.lynx-project.eu/
The graph: http://sparql.lynx-project.eu/graph/eurovoc
Which contains some entries with brackets in the prefLabel i.e. "sanction (EU)".
I'm trying to retrieve such exact match of such entries with:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?c ?label
WHERE {
GRAPH <http://sparql.lynx-project.eu/graph/eurovoc> {
?c a skos:Concept .
?c ?p ?label.
FILTER regex(?label, "^sanction (EU)$", "i" )
FILTER (lang(?label) = "en")
FILTER (?p IN (skos:prefLabel, skos:altLabel ) )
}
}
It doesn't return anything. Also tried to escape the brackets with backslash but the query breaks. Do you know how to escape brackets in a sparql string?? thanks in advance!

How to obtain Bio2RDF resource using SPARQL query?

I am working with Bio2RDF biological database and would like to get resource URL for Novobiocin which is [http://bio2rdf.org/drugbank:DB01051 ] using SPARQL query.Using yasgui.org I selected http://drugbank.bio2rdf.org/sparql service and executed the following query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT ?resource WHERE { ?resource dcterms:identifier "drugbank:DB01051"}
However, It did not retrieve the result. Could you tell me why it does not work, please ?

I want to get all "is dbo:wikiPageRedirects of" in dbpedia using sparql [duplicate]

This question already has answers here:
Why is dbpedia-owl:wikiPageRedirects not returning the full set of redirect links? (Sparql)
(2 answers)
Closed 7 years ago.
I wanted to get the "is dbo:wikiPageRedirects of" and use it as an alias to the label.
ex:
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 dbo: <http://dbpedia.org/ontology/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
WHERE {
FILTER (?uri = <http://dbpedia.org/resource/Benigno_Aquino_III>)
OPTIONAL{
?uri rdfs:label ?label .
FILTER( LANG(?label) = "" || LANGMATCHES(LANG(?label), "en") )
}
OPTIONAL
{
?uri dbo:birthDate ?birthDate .
}
OPTIONAL
{
?uri rdfs:label "Benigno Aquino III"#en ;
dbo:wikiPageRedirects ?redirectsTo .
}
}
what I get is a blank wikiPageRedirects, But when i use http://dbpedia.org/resource/PNOY i get http://dbpedia.org/resource/Benigno_Aquino_III as redirectsTo. I want to do the opposite.
The relation is "dbo:wikiPageRedirects of" and not "dbo:wikiPageRedirects". In this case, it means that http://dbpedia.org/resource/Benigno_Aquino_III lists the resources which have a connection of the sort
?u dbo:wikiPageRedirects http://dbpedia.org/resource/Benigno_Aquino_III
you should use
?redirectsTo dbo:wikiPageRedirects ?uri .

sparql query - triple

I am a biology trying to understand the meaning of a triple from this SPARQL query. If "a" is a triple, what are the resources for subject, object and predicate in this sample query?
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX sio: <http://semanticscience.org/resource/>
PREFIX efo: <http://www.ebi.ac.uk/efo/>
PREFIX atlas: <http://rdf.ebi.ac.uk/resource/atlas/>
PREFIX atlasterms: <http://rdf.ebi.ac.uk/terms/atlas/>
SELECT DISTINCT ?experiment ?description WHERE
{?experiment
a atlasterms:Experiment ;
dcterms:description ?description ;
atlasterms:hasAssay
[atlasterms:hasSample
[atlasterms:hasSampleCharacteristic
[ atlasterms:propertyType ?propertyType ;
atlasterms:propertyValue ?propertyValue]
]
]
filter regex (?description, "diabetes", "i")
}
I appreciate your help?
Thanks,
AD
?experiment
a atlasterms:Experiment ;
is the same as
?experiment a atlasterms:Experiment ;
is the same as
?experiment rdf:type atlasterms:Experiment ;
so the form in the query is just a whitespace rearrangement and using the built-in abbreviation "a" for property rdf:type.
See http://www.sparql.org/query-validator.html for an online formatter of SPARQL queries.
A simpler query is:
SELECT DISTINCT ?experiment ?description
WHERE
{ ?experiment rdf:type atlasterms:Experiment .
?experiment dcterms:description ?description
FILTER regex(?description, "diabetes", "i")
}
because
SELECT DISTINCT ?experiment ?description
means that the ?propertyType/?propertyValue part is not affecting the outcome.

get dbpedia type/category for any word

I am trying to get type/category to which the word belongs to like if its basketball then its under the category sports and if its tea then beverage is the category.
I tried :
select * where {basketball} LIMIT 100
and
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX dbres: <http://dbpedia.org/resource/>
select ? {
a owl:basketball .
}
limit 10
also
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX dbres: <http://dbpedia.org/resource/>
SELECT ?y WHERE {
?y dbpedia-owl:binomialAuthority dbres:basketball.
}
limit 10
but there is a query error especially if I dont know the property of the word typed.How can I get the name of the category.
May be you can query rdf:type of basketball.
PREFIX dbres: <http://dbpedia.org/resource/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?o where {dbres:Basketball rdf:type ?o} LIMIT 10
Demo

Resources