本文整理汇总了Python中rdflib.graph.ConjunctiveGraph.serialize方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.serialize方法的具体用法?Python ConjunctiveGraph.serialize怎么用?Python ConjunctiveGraph.serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: locationtoturtle
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def locationtoturtle(ellist, meta):
rdf=Graph();
cs = Namespace("http://cs.unibo.it/ontology/")
colon=Namespace("http://www.essepuntato.it/resource/")
dcterms=Namespace("http://purl.org/dc/terms/")
xsd=Namespace("http://www.w3.org/2001/XMLSchema#")
this=Namespace("http://vitali.web.cs.unibo.it/twiki/pub/TechWeb12/DataSource2/posteBO2011.ttl#")
vcard = Namespace("http://www.w3.org/2006/vcard/ns#")
rdf.bind("vcard", vcard)
rdf.bind("cs", cs)
rdf.bind("", colon)
rdf.bind("dcterms", dcterms)
rdf.bind("xsd", xsd)
rdf.bind("this", this)
rdf.add((this["metadata"], dcterms["creator"], Literal(meta.creator)))
rdf.add((this["metadata"], dcterms["created"], Literal(meta.created,datatype=XSD.date)))
rdf.add((this["metadata"], dcterms["description"], Literal(meta.version)))
rdf.add((this["metadata"], dcterms["valid"], Literal(meta.valid,datatype=XSD.date)))
rdf.add((this["metadata"], dcterms["source"], Literal(meta.source)))
for location in ellist:
rdf.add((colon[location.id], vcard["fn"], Literal(location.name)))
rdf.add((colon[location.id], vcard["extended-address"], Literal(location.address)))
rdf.add((colon[location.id], vcard["category"], Literal(location.category)))
rdf.add((colon[location.id], vcard["latitude"], Literal(location.lat)))
rdf.add((colon[location.id], vcard["longitude"], Literal(location.long)))
if(location.tel):
rdf.add((colon[location.id], vcard["tel"], Literal(location.tel)))
if(location.note):
rdf.add((colon[location.id], vcard["note"], Literal(location.note)))
rdf.add((colon[location.id], cs["opening"], Literal(location.opening)))
rdf.add((colon[location.id], cs["closing"], Literal(location.closing)))
print("Content-type: text/turtle; charset=UTF-8\n")
print rdf.serialize(format="n3")
示例2: parse_and_serialize
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def parse_and_serialize(input_files, input_format, guess,
outfile, output_format, ns_bindings,
store_conn="", store_type=None):
if store_type:
store = plugin.get(store_type, Store)()
store.open(store_conn)
graph = ConjunctiveGraph(store)
else:
store = None
graph = ConjunctiveGraph()
for prefix, uri in list(ns_bindings.items()):
graph.namespace_manager.bind(prefix, uri, override=False)
for fpath in input_files:
use_format, kws = _format_and_kws(input_format)
if fpath == '-':
fpath = sys.stdin
elif not input_format and guess:
use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT
graph.parse(fpath, format=use_format, **kws)
if outfile:
output_format, kws = _format_and_kws(output_format)
kws.setdefault('base', None)
graph.serialize(destination=outfile, format=output_format, **kws)
if store:
store.rollback()
示例3: test_turtle_namespace_prefixes
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def test_turtle_namespace_prefixes(self):
g = ConjunctiveGraph()
n3 = \
"""
@prefix _9: <http://data.linkedmdb.org/resource/movie/> .
@prefix p_9: <urn:test:> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
p_9:a p_9:b p_9:c .
<http://data.linkedmdb.org/resource/director/1> a
<http://data.linkedmdb.org/resource/movie/director>;
rdfs:label "Cecil B. DeMille (Director)";
_9:director_name "Cecil B. DeMille" ."""
g.parse(data=n3, format='n3')
turtle = g.serialize(format="turtle")
# Check round-tripping, just for kicks.
g = ConjunctiveGraph()
g.parse(data=turtle, format='turtle')
# Shouldn't have got to here
s = g.serialize(format="turtle")
self.assertTrue(b('@prefix _9') not in s)
示例4: create_ontology
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def create_ontology(self,tr,predicate,subClass,address,booktitle):
LDT= Namespace("http://www.JceFinalProjectOntology.com/")
ut=Namespace("http://www.JceFinalProjectOntology.com/subject/#")
usubClass=URIRef("http://www.JceFinalProjectOntology.com/subject/"+subClass.strip()+'#')
#LDT.subClass=LDT[subClass]
print(ut)
print(usubClass)
store=IOMemory()
sty=LDT[predicate]
g = rdflib.Graph(store=store,identifier=LDT)
t = ConjunctiveGraph(store=store,identifier=ut)
print ('Triples in graph before add: ', len(t))
#g.add((LDT,RDF.type,RDFS.Class))
g.add((URIRef(LDT),RDF.type,RDFS.Class))
g.add((URIRef(LDT),RDFS.label,Literal("JFPO")))
g.add((URIRef(LDT),RDFS.comment,Literal('class of all properties')))
for v in self.symbols.values():
if self.if_compoTerm(v)==True:
vs=self.splitTerms(v)[0]
else:
vs =v
g.add((LDT[vs],RDF.type,RDF.Property))
g.add((LDT[vs],RDFS.label,Literal('has'+vs)))
g.add((LDT[vs],RDFS.comment,Literal(v)))
g.add((LDT[vs],RDFS.range,OWL.Class))
g.add((LDT[vs],RDFS.domain,Literal(vs)))
g.bind('JFPO',LDT)
#g.commit()
g.serialize('trtst.rdf',format='turtle')
t.add( (ut[tr], RDF.type,OWL.Class) )
t.add((ut[tr],RDFS.subClassOf,OWL.Thing))
t.add((ut[tr],RDFS.label,Literal(tr)))
t.add((ut[tr],DC.title,Literal(booktitle)))
t.add((ut[tr],DC.source,Literal(address)))
t.add((ut[tr],DC[predicate],URIRef(usubClass)))
t.add((ut[tr],LDT[predicate],RDF.Property))
t.add((ut[tr],DC[predicate],URIRef(usubClass)))
t.add((ut[tr],DC[predicate],URIRef(usubClass)))
relation='has'+predicate
t.add((ut[tr],LDT.term(predicate),URIRef(usubClass)))
t.add( (usubClass,RDF.type,OWL.Class))
t.add((usubClass,RDFS.subClassOf,OWL.Thing))
t.add((usubClass,RDFS.subClassOf,URIRef(sty)))
t.add((usubClass,RDFS.label,Literal(subClass)))
#tc=Graph(store=store,identifier=usubClass)
t.bind("dc", "http://http://purl.org/dc/elements/1.1/")
t.bind('JFPO',LDT)
t.commit()
#print(t.serialize(format='pretty-xml'))
t.serialize('test2.owl',format='turtle')
示例5: serialize_store
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def serialize_store(db_conn, filename):
createdb = False
rdfstore = rdflib.plugin.get('Sleepycat', rdflib.store.Store)()
# rdflib can create necessary structures if the store is empty
rt = rdfstore.open(db_conn, create=False)
cg = ConjunctiveGraph(store=rdfstore)
f = open(filename, 'w')
cg.serialize(f)
f.close()
return True
示例6: extract_rdfa
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def extract_rdfa(url, outfile=sys.stdout, parser="rdfa", serializer="n3"):
"""
Extract RDFa from a given URL
Parsers are listed at https://rdflib.readthedocs.org/en/4.1.0/plugin_parsers.html
Serializers are listed at https://rdflib.readthedocs.org/en/4.1.0/plugin_serializers.html
"""
store = None
graph = ConjunctiveGraph()
graph.parse(url, format=parser)
graph.serialize(destination=outfile, format=serializer)
示例7: get
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def get(self):
g = ConjunctiveGraph()
ns = Namespace('http://purl.org/NET/mediatype#')
for mt in models.MediaType.all():
g.add((URIRef(mt.uri), RDF.type, ns['MediaType']))
g.add((URIRef(mt.uri), RDFS.label, Literal(mt.name)))
if mt.rfc_url:
g.add((URIRef(mt.uri), RDFS.seeAlso, URIRef(mt.rfc_url)))
if mt.application_url:
g.add((URIRef(mt.uri), RDFS.seeAlso, URIRef(mt.application_url)))
self.response.headers['Content-Type'] = 'application/rdf+xml'
g.serialize(self.response.out)
示例8: SiocWiki
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
class SiocWiki(object):
def __init__(self, uri, title=None, created=None):
self.graph = Graph()
self.graph.bind('sioc', SIOC)
self.graph.bind('dc', DC)
self.graph.bind('dcterms', DCTERMS)
self.graph.bind('rdf', RDF)
self._add_site(uri, title)
def _add_site(self, uri, title):
node = URIRef(uri)
self.graph.add((node, RDF.type, SIOC['Site']))
self.graph.add((node, DC['title'], Literal(title)))
return node
def add_page(self, content, title, uri, updated):
node = URIRef(uri)
self.graph.add((node, RDF.type, SIOC['Wiki']))
self.graph.add((node, SIOC['link'], URIRef(uri)))
self.graph.add((node, DC['title'], Literal(title)))
self.graph.add((node, DC['content'], Literal(content)))
self.graph.add((node, DCTERMS['updated'], Literal(updated)))
def to_str(self):
return self.graph.serialize(format="pretty-xml")
示例9: check_nt_serialize
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def check_nt_serialize(fpath, fmt, verbose=False):
g = ConjunctiveGraph()
_parse_or_report(verbose, g, fpath, format=fmt)
if verbose:
for t in g:
print t
print "========================================"
print "Parsed OK!"
s = g.serialize(format='nt')
if verbose:
print "Serialized to: ", s
g2 = ConjunctiveGraph()
_parse_or_report(verbose, g2, data=s, format='nt')
if verbose:
print g2.serialize()
crapCompare(g,g2)
示例10: test_issue_250
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def test_issue_250(self):
"""
https://github.com/RDFLib/rdflib/issues/250
When I have a ConjunctiveGraph with the default namespace set,
for example
import rdflib
g = rdflib.ConjunctiveGraph()
g.bind(None, "http://defaultnamespace")
then the Trix serializer binds the default namespace twice in its XML
output, once for the Trix namespace and once for the namespace I used:
print(g.serialize(format='trix').decode('UTF-8'))
<?xml version="1.0" encoding="utf-8"?>
<TriX
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns="http://defaultnamespace"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns="http://www.w3.org/2004/03/trix/trix-1/"
/>
"""
graph = ConjunctiveGraph()
graph.bind(None, "http://defaultnamespace")
sg = graph.serialize(format='trix').decode('UTF-8')
self.assertTrue(
'xmlns="http://defaultnamespace"' not in sg, sg)
self.assertTrue(
'xmlns="http://www.w3.org/2004/03/trix/trix-1/' in sg, sg)
示例11: test_pretty_broken_xmlliteral
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def test_pretty_broken_xmlliteral(self):
# given:
g = ConjunctiveGraph()
g.add((BNode(), RDF.value, Literal(u'''<p ''', datatype=RDF.XMLLiteral)))
# when:
xmlrepr = g.serialize(format='pretty-xml')
# then:
assert u'''<rdf:value rdf:datatype="http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"><p '''.encode('utf-8') in xmlrepr
示例12: test_pretty_xmlliteral
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def test_pretty_xmlliteral(self):
# given:
g = ConjunctiveGraph()
g.add((BNode(), RDF.value, Literal(u'''<p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p>''', datatype=RDF.XMLLiteral)))
# when:
xmlrepr = g.serialize(format='pretty-xml')
# then:
assert u'''<rdf:value rdf:parseType="Literal"><p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p></rdf:value>'''.encode('utf-8') in xmlrepr
示例13: post
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def post(self):
query = self.request.get("content")
nrOfResults = self.request.get("amount")
try:
number = int(nrOfResults)
except ValueError:
number = 0
literals = re.findall(r'"(.+?)"',query)
urls = processLiterals(literals, number)
graph = ConjunctiveGraph()
for url in urls:
# Original URL fetch
xmlresult = urlfetch.fetch(url,deadline=60,method=urlfetch.GET)
if xmlresult.status_code == 200:
iwa = Namespace('http://iwa2012-18-2.appspot.com/#')
idns = Namespace('http://iwa2012-18-2.appspot.com/id/#')
venuens = Namespace('http://iwa2012-18-2.appspot.com/venueid/#')
tree = etree.fromstring(xmlresult.content)
for event in tree.findall('events/event'):
id = event.attrib['id']
title = event.find('title')
url = event.find('url')
venueid = event.find('venue_id')
venueurl = event.find('venue_url')
venuename = event.find('venue_name')
graph.add((idns[id], iwa['hasTitle'], Literal(title.text)))
graph.add((idns[id], iwa['hasUrl'], Literal(url.text)))
graph.add((venuens[id], iwa['hasVenueName'], Literal(venuename.text)))
graph.add((venuens[id], iwa['hasUrl'], Literal(venueurl.text)))
graph.add((idns[id], iwa['atVenue'], venuens[id])))
else:
print "Something went wrong with the connection to the Eventful server. Status code: " + xml.status_code
print graph.serialize()
示例14: rdf_description
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def rdf_description(name, notation='xml' ):
"""
Funtion takes title of node, and rdf notation.
"""
valid_formats = ["xml", "n3", "ntriples", "trix"]
default_graph_uri = "http://gstudio.gnowledge.org/rdfstore"
configString = "/var/tmp/rdfstore"
# Get the Sleepycat plugin.
store = plugin.get('Sleepycat', Store)('rdfstore')
# Open previously created store, or create it if it doesn't exist yet
graph = Graph(store="Sleepycat",
identifier = URIRef(default_graph_uri))
path = mkdtemp()
rt = graph.open(path, create=False)
if rt == NO_STORE:
#There is no underlying Sleepycat infrastructure, create it
graph.open(path, create=True)
else:
assert rt == VALID_STORE, "The underlying store is corrupt"
# Now we'll add some triples to the graph & commit the changes
rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
graph.bind("gstudio", "http://gnowledge.org/")
exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"]
node=Objecttype.objects.get(title=name)
node_dict=node.__dict__
subject=str(node_dict['id'])
for key in node_dict:
if key not in exclusion_fields:
predicate=str(key)
pobject=str(node_dict[predicate])
graph.add((rdflib[subject], rdflib[predicate], Literal(pobject)))
graph.commit()
print graph.serialize(format=notation)
graph.close()
示例15: writeFile
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import serialize [as 别名]
def writeFile(self, stmts, ctx, fileWords):
g = ConjunctiveGraph()
doc = {'ctx' : ctx}
for s in stmts:
g.add(s)
if s[1] == SIOC.has_reply:
doc['topic'] = s[0]
if s[1] == DCTERMS.created: # expecting 2 of these, but same value
doc['created'] = parse(s[2])
doc['n3'] = g.serialize(format="n3")
self.mongo['comment'].insert(doc, safe=True)