j'ai entrain d'interroger une ontologie via SPARQL à partir de Python avec la requete suivante:
j'ai consulter les sites webs, les forums, et j'ai ecrit de types de programmes pour cette interogation.Code:
1
2
3
4
5
6 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 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#> SELECT ?subject ?object WHERE { ?subject rdfs:subClassOf ?object }
j'ai utilisé la même reque que ce soit avec les PREFIX ou non, il ne donne aucun résultat.
le premier programme :
le resultat de ce programme :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 my_data ='c:/wamp/www/onto.owl' import rdflib import tempfile import os fd, file_name = tempfile.mkstemp() f = os.fdopen(fd, 'w') dummy = f.write(my_data) # Returns num bytes written on py3 f.close() g = rdflib.Graph() import rdflib import xml.sax from xml.sax.handler import ContentHandler,EntityResolver,DTDHandler subjects= g.query( """ SELECT ?a ?b WHERE{ ?a rdfs:sugcghdbClassOf ?b } """) print("sub",subjects)
donc la requette ne donne aucun rsultat, sachant que la meme requette donne de resultat dans l'outil protégé, où j'ai ajouté les préfixe.Code:sub <rdflib.plugins.sparql.processor.SPARQLResult object at 0x03AB0C70>
le deuxième programme :
the file (onto.owl) in the localhost : http://localhost/j'ai defini le localhost comme suit : c:/pyhon34/python -m http.server dans le repertoire c:/application contenant le serveur http.
the program :
l'execution du programme donne le resultat :Code:
1
2
3
4
5
6
7
8
9
10
11
12 from SPARQLWrapper import SPARQLWrapper, RDF, JSON, POST, SELECT result= "http://localhost:8000"; sparql = SPARQLWrapper(result) sparql.setQuery(""" SELECT ?a ?b WHERE{ ?a rdfs:subClassOf ?b } """) sparql.setReturnFormat(JSON) results = sparql.query().convert() print(results)
===============================================================================Citation:
Warning (from warnings module):
File "C:\Python34\Lib\site-packages\SPARQLWrapper\Wrapper.py", line 762
warnings.warn("unknown response content type, returning raw response...", RuntimeWarning)
RuntimeWarning: unknown response content type, returning raw response...
b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=mbcs">\n<title>Directory listing for /?query=\n++++++++++++++++SELECT+?a+?b+WHERE{+?a+rdfs:subClassOf++?b+}\n++++++++++++++++&results=json&output=json&format=json/</title>\n</head>\n<body>\n<h1>Directory listing for /?query=\n++++++++++++++++SELECT+?a+?b+WHERE{+?a+rdfs:subClassOf++?b+}\n++++++++++++++++&results=json&output=json&format=json/</h1>\n<hr>\n<ul>\n<li><a href="onto-hadith261117.xml">onto-hadith261117.xml</a></li>\n</ul>\n<hr>\n</body>\n</html>\n'
sachant que ce même deuxième programme j'ai utilisé pour intérogie le sit web http:/:dpbedia.org il donne de reultat. ce programme est :
Code:
1
2
3
4
5
6
7
8
9
10
11 from SPARQLWrapper import SPARQLWrapper, JSON, XML sparql = SPARQLWrapper("http://dbpedia.org") sparql.setQuery(""" select distinct ?Concept where {[] a ?Concept} LIMIT 10 } """) sparql.spetReturnFormat(XML) results = sparql.query().convert()