[英]Using Python rdflib: how to include literals in sparql queries?
I can include URIs and variables in my queries, but I can't include literals in my queries.我可以在查询中包含 URI 和变量,但不能在查询中包含文字。
Here, I have some code which successfully reads an RDF file, finds all the RDF triples with skos:prefLabels, counts them, and then identifies a couple of specific ones from a set of keywords:在这里,我有一些代码可以成功读取 RDF 文件,使用 skos:prefLabels 找到所有 RDF 三元组,对它们进行计数,然后从一组关键字中识别出几个特定的三元组:
import rdflib.plugins.sparql as sparql
import rdflib
import rdflib.graph as g
graph = g.Graph()
# Read the RDF file
graph.parse(
'h:\......SKOSTaxonomy.rdf',
format='xml')
# Build and execute the query
q = sparql.prepareQuery('SELECT ?s ?p ?o WHERE { ?s ?p ?o .}')
p = rdflib.URIRef("http://www.w3.org/2004/02/skos/core#prefLabel")
qres = graph.query(q, initBindings = {'p' : p})
print len(qres)
# Look for keywords among the results
keywords = set([u'Jackknifing', 'Technology-mapping', 'Something random'])
for (subj, pred, obj) in qres:
if obj.value in keywords:
print obj.value
As expected, this code prints:正如预期的那样,此代码打印:
2299
Jackknifing
Technology-mapping
since Jackknifing and Technology-mapping are prefLabels in the file.因为 Jackknifing 和 Technology-mapping 是文件中的 prefLabels。
What I really want to do is to construct and execute a Sparql query to look for each keyword in turn.我真正想做的是构造并执行 Sparql 查询以依次查找每个关键字。 But this is where I come unstuck, because I can't put a string into the query.
但这就是我解脱的地方,因为我无法将字符串放入查询中。 I have tried this, for example:
我试过这个,例如:
o = rdflib.Literal(u'Jackknifing')
qres = graph.query(q, initBindings = {'p' : p, 'o' : o})
but qres is empty.但 qres 是空的。 I have also tried putting a literal explicitly into the query, eg
我还尝试将文字明确地放入查询中,例如
q = sparql.prepareQuery('SELECT ?s ?p WHERE { ?s ?p "Technology-mapping" .}')
qres = graph.query(q, initBindings = {'p' : p})
but that returns an empty result too.但这也会返回一个空结果。
How are literals included in a query?查询中如何包含文字?
If the literals in your data have datatypes, or are strings with language tags, then a plain literal, that is, one without a datatype or language tag, injected into the query won't match.如果数据中的文字具有数据类型,或者是带有语言标签的字符串,那么注入到查询中的纯文字(即没有数据类型或语言标签的文字)将不匹配。
The RDFLib docs on Literals show ways of creating literals with datatypes, but don't have an example of creating one with a language tag.文字上的 RDFLib 文档显示了使用数据类型创建文字的方法,但没有使用语言标签创建文字的示例。 However, the docs also have the source attached and the signature for
Literal
's __new__
is:但是,文档还附上了源代码,并且
Literal
的__new__
签名是:
static __new__(lexical_or_value, lang=None, datatype=None, normalize=None)
Since the literal in your data has a language tag ( 'en'
), you should create your literal as由于数据中的文字有一个语言标签(
'en'
),您应该将文字创建为
o = rdflib.Literal(u'Jackkifing',lang='en')
so that the language tag is associated with the literal.以便语言标签与文字相关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.