I am having difficulty in printing the SKOS broader and narrower concepts against my URIRef (i.e. the output of the SPAQRL query). I want to print the Broader and Narrowers concepts against the captured URI REF (i.e. Biomass). The file which i am parsing does not contains the Broader and Narrowers concepts. I dont know whether i need to manually add them in file before i run queries on them.
I have already seen the similar questions like skos broader and narrow inverse not working but couldnt find the solution.
import rdflib
g = rdflib.Graph()
result = g.parse("C://Users/support/Documents/Re.txt", format=("turtle"))
qres = g.query(
"""
prefix skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
WHERE { ?s skos:prefLabel "Biomass"}
""")
for row in qres: print(row)
The output of the query is
for row in qres: print(row)
(rdflib.term.URIRef('http://aims.fao.org/aos/agrovoc/c_926'),)
I have tried by nesting the SELECT Queries but it is not working.
My Query
qres = g.query(
"""
prefix skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
WHERE { ?s skos:broader ?o . {
SELECT ?s
WHERE { ?s skos:prefLabel "Biomass" .}
}
""")
for row in qres: print(row)
If you're just struggling with the query, then I think you're overcomplicating it. This should work
qres = g.query(
"""
prefix skos: <http://www.w3.org/2004/02/skos/core#>
SELECT * WHERE {
?s skos:broader ?o ; skos:prefLabel "Biomass" . }
""")
Related
I am trying to run a SPARQL query in Python, however, when trying to use bind & replace, escaping the " is not working resulting in an error.
I tried to find some info on unicoding, but seem to find it hard to include it within this query.
Does anyone have a solution for the problem?
TEXT:
from rdflib import Graph
from rdflib.namespace import RDF, SKOS
g = Graph()
g.parse('\Python\Molenakker.orox.ttl')
len(g)
print(len(g))
gmls = g.query('''
PREFIX gwsw: <http://data.gwsw.nl/1.5/totaal/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sparql: <http://sparql.gwsw.nl/bim/juinen#>
SELECT ?XYZ
WHERE {
{
?uri rdf:type gwsw:GemengdRiool;
gwsw:hasAspect ?ori.
?ori gwsw:hasAspect ?lijn.
?lijn gwsw:hasValue ?GML.
BIND(REPLACE(STR(?GML),"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">","") AS ?gml)
BIND(REPLACE(STR(?gml),"</gml:posList></gml:LineString>","")AS ?XYZ)
}
UNION
{
?uri rdf:type gwsw:Hemelwaterriool;
gwsw:hasAspect ?ori.
?ori gwsw:hasAspect ?lijn.
?lijn gwsw:hasValue ?GML.
BIND(REPLACE(STR(?GML),"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">","") AS ?gml)
BIND(REPLACE(STR(?gml),"</gml:posList></gml:LineString>","")AS ?XYZ)
}
UNION
{
?uri rdf:type gwsw:Vuilwaterriool;
gwsw:hasAspect ?ori.
?ori gwsw:hasAspect ?lijn.
?lijn gwsw:hasValue ?GML.
BIND(REPLACE(STR(?GML),"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">","") AS ?gml)
BIND(REPLACE(STR(?gml),"</gml:posList></gml:LineString>","")AS ?XYZ)
}
}''')
for gml in gmls:
print(f"{gml.XYZ}")
Since you are using a triple quote for constructing a multi-line string, the backslash-doublequote becomes simply a doublequote.
'''
"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">"
'''
becomes
'''
"<gml:LineString xmlns:gml="http://www.opengis.net/gml"><gml:posList srsDimension="3">"
'''
which is probably not what you want.
Try to rewrite it as
'''
'<gml:LineString xmlns:gml="http://www.opengis.net/gml"><gml:posList srsDimension="3">'
'''
I have some SPARQL queries to run on wikidata in python and I need to get the name/label of the entity returned instead of URI. For example, given the python snippet below:
from qwikidata.sparql import return_sparql_query_results
query_string = """
select ?ent where { ?ent wdt:P31 wd:Q2637056 . ?ent wdt:P2244 ?obj } ORDER BY DESC(?obj)LIMIT 5
"""
res = return_sparql_query_results(query_string)
for row in res["results"]["bindings"]:
print(row["ent"]["value"])
The queries in the original form return URIs, but I need to get the entity label/name. How can I do that in python?
The current output of the query:
http://www.wikidata.org/entity/Q841796
http://www.wikidata.org/entity/Q780047
NOTE: I don't have real access to the queries, therefore I can't rewrite the queries.
My comment was too long so i am posting an answer.
You'll need to rewrite the queries. Please find below an example how to get labels without using the label service.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?country ?countryLabel
WHERE
{
# instance of country
?country wdt:P31 wd:Q3624078.
OPTIONAL {
?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
}
}
ORDER BY ?countryLabel
try it!
Adapted for your Soyuz-T example:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?ent ?entLabel
WHERE
{
# instance of Soyuz-T https://www.wikidata.org/wiki/Q2637056
?ent wdt:P31 wd:Q2637056 .
# https://www.wikidata.org/wiki/Property:P2244 periapsis
?ent wdt:P2244 ?obj
OPTIONAL {
?ent rdfs:label ?entLabel filter (lang(?entLabel) = "en").
}
} ORDER BY DESC(?obj)LIMIT 5
try it!
Result:
ent entLabel
wd:Q841796 Soyuz T-15
wd:Q780047 Soyuz T-8
I am new to Turtle format files and querying them with SPARQL. So I have many questions to be solved, I hope you can help me!
I have a file called equipamentsCURT3.ttl and contains the following:
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix v: <http://www.w3.org/2006/vcard/ns#> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://opendata.gencat.cat/recursos/equipaments/30883> a v:VCard ;
v:adr [ a v:Work ;
v:country-name "Spain" ;
v:locality "Sabadell" ;
v:postal-code "08202" ;
v:region "Vallès Occidental" ;
v:street-address " c. Sant Josep" ] ;
v:category "2. Parvulari"#ca,
"3. Educació primària"#ca,
"4. Educació secundària obligatòria"#ca,
"Educació. Formació"#ca,
"Ensenyaments de règim general"#ca ;
v:fn "Escolàpies Sabadell"#ca ;
v:geo [ v:latitude 4.154826e+01 ;
v:longitude 2.111243e+00 ] ;
v:nickname "Escolàpies Sabadell"#ca ;
v:tel [ a v:Pref,
v:Tel,
v:Work ;
rdf:value "937255348" ] .
<http://opendata.gencat.cat/recursos/equipaments/31264> a v:VCard ;
v:adr [ a v:Work ;
v:country-name "Spain" ;
v:locality "Molins de Rei" ;
v:postal-code "08750" ;
v:region "Baix Llobregat" ;
v:street-address " c. Ntra. Sra. de Lourdes" ] ;
v:category "4. Educació secundària obligatòria"#ca,
"7. Batxillerat"#ca,
"8. Cicles formatius d'FP de grau mitjà (CFPM)"#ca,
"9. Cicles formatius d'FP de grau superior (CFPS)"#ca,
"Educació. Formació"#ca,
"Ensenyaments de règim general"#ca ;
v:fn "Institut Bernat el Ferrer"#ca ;
v:geo [ v:latitude 4.14105e+01 ;
v:longitude 2.02704e+00 ] ;
v:nickname "Institut Bernat el Ferrer"#ca ;
v:tel [ a v:Pref,
v:Tel,
v:Work ;
rdf:value "936683762" ] .
<http://opendata.gencat.cat/recursos/equipaments/31265> a v:VCard ;
v:adr [ a v:Work ;
v:country-name "Spain" ;
v:locality "Castellar del Vallès" ;
v:postal-code "08211" ;
v:region "Vallès Occidental" ;
v:street-address " NC Bonavista" ] ;
v:category "2. Parvulari"#ca,
"3. Educació primària"#ca,
"Educació. Formació"#ca,
"Ensenyaments de règim general"#ca ;
v:fn "Escola Bonavista"#ca ;
v:geo [ v:latitude 4.161903e+01 ;
v:longitude 2.091745e+00 ] ;
v:nickname "Escola Bonavista"#ca ;
v:tel [ a v:Pref,
v:Tel,
v:Work ;
rdf:value "937144195" ] .
I am using Python3.5 and a library called RDFLib (https://github.com/RDFLib/rdflib). I need to read from a file called equipamentsCURT.rdf, serialize it into equipamentsCURT3.ttl and then retrieve all information related to an equipment. For example, for the equipment 30883 (http://opendata.gencat.cat/recursos/equipaments/30883), I want v:adr,v:category,v:fn,v:geo and v:tel. To obtain this data, I use SPARQL but I don't know why the query doesn't work. I'm very confused in how to query the information.
Here is my code:
import rdflib , pprint
from rdflib import URIRef, Graph
from rdflib.plugins import sparql
g = Graph()
g.load("equipamentsCURT3.ttl", format='turtle')
queryTest = 'prefix v: <http://www.w3.org/2006/vcard/ns#> ' \
'select ?y where {?x a <http://opendata.gencat.cat/recursos/equipaments 30883>; ?y v:VCard .}'
qresult = g.query(queryTest)
for st in qresult:
print rdflib.term.Literal(st).value
The whole query doesn't make any sense nor does it match the data.
I'd suggest reading a SPARQL tutorial first. The whole query looks like copy-paste from something else + some random stuff from your side.
the URI http://opendata.gencat.cat/recursos/equipaments 30883 contains a white space which is wrong
http://opendata.gencat.cat/recursos/equipaments/30883 is not a class. Thus, a triple pattern
?x a <http://opendata.gencat.cat/recursos/equipaments/30883>, which means to all resources that belong to the class http://opendata.gencat.cat/recursos/equipaments/30883 doesn't match your data.
The second triple pattern is ?x ?y v:VCard. And you're selecting the predicate ?y as the final result of your query. But you want the objects for a given subject and a given set of predicates. Syntax of a triple /resp. triple pattern) is subject-predicate-object. Thus, for example for v:category it should be
PREFIX v: <http://www.w3.org/2006/vcard/ns#>
SELECT ?o WHERE {
<http://opendata.gencat.cat/recursos/equipaments/30883> v:category ?o
}
For the other properties it will be more complicated since the values itself are blank nodes that have attached multiple values via additional properties. E.g. for v:adr it would be
PREFIX v: <http://www.w3.org/2006/vcard/ns#>
SELECT ?p ?o WHERE {
<http://opendata.gencat.cat/recursos/equipaments/30883> v:adr ?adr .
?adr ?p ?o
}
Update
If you don't want the values but the properties it's correct to have the variable in predicate position. But it's wrong to restrict it to those properties that occur only in triples with the object v:VCard because there is no such property besides rdf:type (a is just a synonym for it). In that case it should be
PREFIX v: <http://www.w3.org/2006/vcard/ns#>
SELECT DISTINCT ?p WHERE {
<http://opendata.gencat.cat/recursos/equipaments/30883> ?p ?o
}
I want to retrieve the wikipageID for the same query name in different languages. For example:
select * where { <http://dbpedia.org/resource/Mike_Quigley_(footballer)> dbpedia-owl:wikiPageID ?wikiID }
====>Mike_Quigley_(footballer) 17237449 en
select * where { <http://dbpedia.org/resource/Theodore_Roberts> dbpedia-owl:wikiPageID ?wikiID }
====>Theodore_Roberts 6831454 en
select * where { <http://de.dbpedia.org/resource/Theodore_Roberts> dbpedia-owl:wikiPageID ?wikiID }
====>Theodore_Roberts de
select * where { <http://fr.dbpedia.org/resource/Theodore_Roberts> dbpedia-owl:wikiPageID ?wikiID }
====>Theodore_Roberts fr
select * where { <http://it.dbpedia.org/resource/Theodore_Roberts> dbpedia-owl:wikiPageID ?wikiID }
====>Theodore_Roberts it
select * where { <http://ja.dbpedia.org/resource/セオドア・ロバーツ> dbpedia-owl:wikiPageID ?wikiID }
====>セオドア・ロバーツ ja
In the first query Mike_Quigley_(footballer)which is in english I was able to retrieve its ID = 17237449but when the language changes as you can see I cannot retrieve the wikipageIDs.
How can I retrieve the ids of these pages
The more complex part is that the following link German language Theodore_Roberts will lead me to a page where the property wikipageID is not dbpedia-owl It gets really complicated.
Do you have any idea on how to solve it?
Amazing, when I try this query
SELECT ?uri ?id
WHERE {
?uri <http://dbpedia.org/ontology/wikiPageID> ?id.
FILTER (?uri = <http://dbpedia.org/resource/Lyon>)
}
I get the following result:
http://dbpedia.org/resource/Lyon 863863
But When I change the uri to <it.dbpedia.org/resource/Lyon> I get nothing.
So if I understand correctly you want to get the Italian version of your URI. But the problem is you are looking for the wrong URI. The Italian version of Lyon URI in English DBpedia is http://it.dbpedia.org/resource/Lione and I am assuming you are using that. I found this out by:
SELECT *
WHERE {
?uri owl:sameAs ?b.
FILTER (?uri = <http://dbpedia.org/resource/Lyon>)
}
And I couldn't get the Italian pageID from the English DBPedia.
When I tried in on the Italian DBpedia, it worked :
SELECT ?uri ?id
WHERE {
?uri <http://dbpedia.org/ontology/wikiPageID> ?id.
FILTER (?uri = <http://it.dbpedia.org/resource/Lyon>)
}
However, if you look at the Italian page http://it.dbpedia.org/resource/Lyon, you can see that it has a property dbpedia-owl:wikiPageRedirects which is equal to http://it.dbpedia.org/resource/Lione that the owl:sameAs gives you. Maybe you can work your way through that.
I'm using the same sparql statement using two different clients but both are not returning the same results. The owl file is in rdf syntax and can be accessed here.
This is the sparql statement:
PREFIX wo:<http://purl.org/ontology/wo/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?individual where { ?individual rdf:type wo:Class }
I'm using it using top braid and the following python program:
>>> import rdflib
>>> import rdfextras
>>> rdfextras.registerplugins()
>>> g=rdflib.Graph()
>>> g.parse("index.owl")
<Graph identifier=N39ccd52985014f15b2fea90c3ffaedca (<class 'rdflib.graph.Graph'>)>
>>> PREFIX = "PREFIX wo:<http://purl.org/ontology/wo/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
>>> query = "select ?individual where { ?individual rdf:type wo:Class }"
>>> query = PREFIX + query
>>> result_set = g.query(query)
>>> len(result_set)
0
Which is returning 0
This query constructs a graph containing all the triples in which wo:Class is used as a subject, predicate, or object:
PREFIX wo: <http://purl.org/ontology/wo/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
construct { ?s ?p ?o }
where {
{ ?s ?p wo:Class . bind( wo:Class as ?o ) } union
{ ?s wo:Class ?o . bind( wo:Class as ?p ) } union
{ wo:Class ?p ?o . bind( wo:Class as ?s ) }
}
I made a local copy of your data and the results I get are (in Turtle):
#prefix vs: <http://www.w3.org/2003/06/sw-vocab-status/ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix wo: <http://purl.org/ontology/wo/> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
wo:Class a owl:Class ;
rdfs:comment "A class is a scientific way to group related organisms together, some examples of classes being jellyfish, reptiles and sea urchins. Classes are big groups and contain within them smaller groupings called orders, families, genera and species."#en ;
rdfs:label "Class"#en ;
rdfs:seeAlso <http://www.bbc.co.uk/nature/class> , <http://en.wikipedia.org/wiki/Class_%28biology%29> ;
rdfs:subClassOf wo:TaxonRank ;
vs:term_status "testing" .
wo:class rdfs:range wo:Class .
There are no individuals of type wo:Class in your data. The result set ought to be empty.