本文整理汇总了Python中rdflib.graph.ConjunctiveGraph.add方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.add方法的具体用法?Python ConjunctiveGraph.add怎么用?Python ConjunctiveGraph.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def main(fd, store_type=None, store_id=None, graph_id=None, gzipped=False):
"""
Converts MARC21 data stored in fd to a RDFlib graph.
"""
from rdflib import plugin
if store_type:
msg = "Need a {} identifier for a disk-based store."
assert store_id, msg.format('store')
assert graph_id, msg.format('graph')
store = plugin.get(store_type, Store)(store_id)
else:
store = 'default'
graph = Graph(store=store, identifier=graph_id)
try:
records = MARCReader(open(fd))
for i, triple in enumerate(process_records(records)):
graph.add(triple)
if i % 100 == 0:
graph.commit()
if i % 10000 == 0:
print i
finally:
graph.commit()
return graph
示例2: make_graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def make_graph():
g = Graph()
# add namespaces
g.bind("inpho", "http://inpho.cogs.indiana.edu/")
g.bind("thinker", "http://inpho.cogs.indiana.edu/thinker/")
g.bind("journal", "http://inpho.cogs.indiana.edu/journal/")
g.bind("foaf", "http://xmlns.com/foaf/0.1/")
g.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
g.bind("rdfs", "http://www.w3.org/TR/rdf-schema/#")
g.bind("owl", "http://www.w3.org/2002/07/owl#")
g.bind("idea", "http://inpho.cogs.indiana.edu/idea/")
g.bind("skos", "http://www.w3.org/2004/02/skos/core#")
g.bind ("db", "http://dbpedia.org/")
g.bind ("dc", "http://purl.org/dc/elements/1.1/")
# user namespace currently doesn't exist?
g.bind("user", "http://inpho.cogs.indiana.edu/user/")
# OWL disjoints
disjoint_objects = ["thinker", "journal", "idea", "user"]
for a, b in combinations(disjoint_objects, 2):
g.add((inpho[a], owl['disjointWith'], inpho[b]))
g = populate_thinkers(g)
g = populate_ideas(g)
g = populate_journals(g)
return g
示例3: writeFile
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def writeFile(self, stmts, ctx, fileWords):
outfile = "commentstore/post-%s.nt" % ("-".join(fileWords))
graph = ConjunctiveGraph()
graph.add(*stmts, **{'context' : ctx})
graph.graph.serialize(outfile, format='n3')
log.info("wrote new comment to %s", outfile)
示例4: test_pretty_broken_xmlliteral
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [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
示例5: test_pretty_xmlliteral
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [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
示例6: _mangled_copy
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def _mangled_copy(g):
"Makes a copy of the graph, replacing all bnodes with the bnode ``_blank``."
gcopy = ConjunctiveGraph()
isbnode = lambda v: isinstance(v, BNode)
for s, p, o in g:
if isbnode(s): s = _blank
if isbnode(p): p = _blank
if isbnode(o): o = _blank
gcopy.add((s, p, o))
return gcopy
示例7: test_escaping_of_triple_doublequotes
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def test_escaping_of_triple_doublequotes():
"""
Issue 186 - Check escaping of multiple doublequotes.
A serialization/deserialization roundtrip of a certain class of
Literals fails when there are both, newline characters and multiple subsequent
quotation marks in the lexical form of the Literal. In this case invalid N3
is emitted by the serializer, which in turn cannot be parsed correctly.
"""
g=ConjunctiveGraph()
g.add((URIRef('http://foobar'), URIRef('http://fooprop'), Literal('abc\ndef"""""')))
# assert g.serialize(format='n3') == '@prefix ns1: <http:// .\n\nns1:foobar ns1:fooprop """abc\ndef\\"\\"\\"\\"\\"""" .\n\n'
g2=ConjunctiveGraph()
g2.parse(data=g.serialize(format='n3'), format='n3')
assert g.isomorphic(g2) is True
示例8: write_graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def write_graph(data_handle, out_handle, format='n3'):
graph = Graph()
count = 0
for record in generate_records(data_handle):
count += 1
if count % 1000:
sys.stderr.write(".")
else:
sys.stderr.write(str(count))
for triple in get_triples(record):
graph.add(triple)
graph.commit()
current_site = Site.objects.get_current()
domain = 'https://%s' % current_site.domain
out_handle.write(graph.serialize(format=format, base=domain, include_base=True))
return count
示例9: post
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [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()
示例10: rdf_description
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [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()
示例11: get
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [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)
示例12: testSerialize
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def testSerialize(self):
s1 = URIRef('store:1')
r1 = URIRef('resource:1')
r2 = URIRef('resource:2')
label = URIRef('predicate:label')
g1 = Graph(identifier = s1)
g1.add((r1, label, Literal("label 1", lang="en")))
g1.add((r1, label, Literal("label 2")))
s2 = URIRef('store:2')
g2 = Graph(identifier = s2)
g2.add((r2, label, Literal("label 3")))
g = ConjunctiveGraph()
for s,p,o in g1.triples((None, None, None)):
g.addN([(s,p,o,g1)])
for s,p,o in g2.triples((None, None, None)):
g.addN([(s,p,o,g2)])
r3 = URIRef('resource:3')
g.add((r3, label, Literal(4)))
r = g.serialize(format='trix')
g3 = ConjunctiveGraph()
from StringIO import StringIO
g3.parse(StringIO(r), format='trix')
for q in g3.quads((None,None,None)):
# TODO: Fix once getGraph/getContext is in conjunctive graph
if isinstance(q[3].identifier, URIRef):
tg=Graph(store=g.store, identifier=q[3].identifier)
else:
# BNode, this is a bit ugly
# we cannot match the bnode to the right graph automagically
# here I know there is only one anonymous graph,
# and that is the default one, but this is not always the case
tg=g.default_context
self.assertTrue(q[0:3] in tg)
示例13: track
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
def track(self, resource):
graph = ConjunctiveGraph()
sparql = SPARQLWrapper(self.conf.get_SPARQL())
queue = [resource]
while len(queue) != 0:
target = queue.pop()
query = DESCRIBE_QUERY.replace('__RESOURCE__', target.n3())
query = query.replace('__RELEASE__', self.conf.get_graph_name('release'))
query = query.replace('__RULES__', self.conf.get_graph_name('rules'))
query = query.replace('__RAW_DATA__', self.conf.get_graph_name('raw-data'))
sparql.setQuery(query)
results = sparql.query().convert()
for statement in results:
# Add the statement to the graph
graph.add(statement)
# If the relate to another resource we describe, queue it
(_,p,o) = statement
if p.startswith(PROV):
if o.startswith(self.conf.get_namespace('data')):
queue.append(o)
print graph.serialize(format='turtle')
示例14: Hisco2RDF
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
class Hisco2RDF():
'''
Scrapes the HISCO Web site
The hierarchy goes as "master > minor > rubri > micro"
'''
def __init__(self):
# The graph to store the data
self.graph = ConjunctiveGraph()
self.graph.namespace_manager.bind('skos', SKOS)
self.graph.namespace_manager.bind('hisco', HISCO)
self.graph.namespace_manager.bind('dcterms', DCTERMS)
self.graph.namespace_manager.bind('sdmx-dimension', SDMX_DIMENSION)
self.graph.namespace_manager.bind('sdmx-code', SDMX_CODE)
self.graph.namespace_manager.bind('qb', QB)
# SQLite DB for the cache
self.cache = sqlite3.connect('cache.db')
cursor = self.cache.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS page (url text, html text)")
self.cache.commit()
def __del__(self):
self.cache.close()
def get_page(self, url):
#log.debug("Load %s" % url)
c = self.cache.cursor()
c.execute("SELECT * FROM page WHERE url = ?", (url,))
res = c.fetchone()
doc = None
if res == None:
doc = requests.get(url).content
c.execute("INSERT INTO page VALUES (?,?)", (url, doc))
self.cache.commit()
else:
(_, doc) = res
return BeautifulSoup(doc)
def save_output(self):
# Add more things needed for DataCubes
dimprop = HISCO['occupation']
self.graph.add((dimprop, RDF.type, QB['DimensionProperty']))
self.graph.add((dimprop, RDFS.range, SKOS.Collection))
self.graph.add((dimprop, QB['Concept'], SKOS.Collection))
self.graph.add((dimprop, RDFS.label, Literal('Occupation code', lang='en')))
self.graph.add((dimprop, RDFS.comment, Literal('The HISCO group of the occupation', lang='en')))
# Print to the screen
#outfile = sys.stdout.buffer
#self.graph.serialize(destination=outfile, format='n3')
# Save to the file
outfile = open('../hisco.ttl', "wb")
self.graph.serialize(destination=outfile, format='n3')
outfile.close()
def parse_hisco_tree(self):
'''
Parse the hisco tree
'''
# Load the page
doc = self.get_page(ROOT + HISCO_TREE)
# Find the major groups
major_groups = []
major_group = None
for table in doc.find_all('table', attrs={'border':'0'}):
for row in table.find_all('tr'):
for col in row.find_all('td'):
# Skip empty rows
if len(col.text) == 1:
continue
# We are starting a new group
if col.text.startswith('Majorgroup'):
# Save the one we were building if any
if major_group != None:
major_groups.append(major_group)
m = re.search("Majorgroup ([^ ]*) ", col.text)
major_group = {}
major_group['title'] = col.text
major_group['code'] = m.group(1).replace('/', '-')
# We have a description
if col.text.startswith('Workers'):
major_group['description'] = col.text
# We have links to minor
if col.text.startswith('List Minor'):
link = col.find_all('a')[0]['href']
major_group.setdefault('links', [])
major_group['links'].append(link)
# Add the last group in the making
if major_group != None:
major_groups.append(major_group)
# Add the groups to the graph
for group in major_groups:
major_group_uri = self._get_group_uri(group['code'])
self.graph.add((major_group_uri, RDF.type, SKOS['ConceptScheme']))
self.graph.add((major_group_uri, DCTERMS.title, Literal(group['title'])))
#.........这里部分代码省略.........
示例15: ContextTest
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import add [as 别名]
class ContextTest(test.TestCase):
"""
Testing different contexts.
Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/context_case.py
"""
store_name = "Django"
storetest = True
path = ""
create = True
michel = URIRef(u'michel')
tarek = URIRef(u'tarek')
bob = URIRef(u'bob')
likes = URIRef(u'likes')
hates = URIRef(u'hates')
pizza = URIRef(u'pizza')
cheese = URIRef(u'cheese')
c1 = URIRef(u'context-1')
c2 = URIRef(u'context-2')
def setUp(self):
self.graph = ConjunctiveGraph(store=self.store_name)
self.graph.destroy(self.path)
self.graph.open(self.path, create=self.create)
def tearDown(self):
self.graph.destroy(self.path)
self.graph.close()
def get_context(self, identifier):
assert isinstance(identifier, URIRef) or isinstance(identifier, BNode), type(identifier)
return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self)
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
graph = Graph(self.graph.store, c1)
graph.add((tarek, likes, pizza))
graph.add((tarek, likes, cheese))
graph.add((michel, likes, pizza))
graph.add((michel, likes, cheese))
graph.add((bob, likes, cheese))
graph.add((bob, hates, pizza))
graph.add((bob, hates, michel))
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
graph = Graph(self.graph.store, c1)
graph.remove((tarek, likes, pizza))
graph.remove((tarek, likes, cheese))
graph.remove((michel, likes, pizza))
graph.remove((michel, likes, cheese))
graph.remove((bob, likes, cheese))
graph.remove((bob, hates, pizza))
graph.remove((bob, hates, michel))
def addStuffInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek)
# add to default context
self.graph.add(triple)
# add to context 1
graph = Graph(self.graph.store, c1)
graph.add(triple)
# add to context 2
graph = Graph(self.graph.store, c2)
graph.add(triple)
def testConjunction(self):
self.addStuffInMultipleContexts()
triple = (self.pizza, self.likes, self.pizza)
# add to context 1
graph = Graph(self.graph.store, self.c1)
graph.add(triple)
self.assertEquals(len(graph), 2)
self.assertEquals(len(self.graph), 2)
def testAdd(self):
self.addStuff()
def testRemove(self):
#.........这里部分代码省略.........