简体   繁体   English

如何在SPARQL中选择谓词及其各自的标签?

[英]How to select predicates and their respective labels in SPARQL?

I am trying to list all predicates of an ontology ( NIF ) and their labels. 我正在尝试列出所有本体( NIF )谓词及其标签。 When not querying the label, it produces 80 results. 不查询标签时,将产生80个结果。 So, I assume there are 80 predicates with the term 'nif' in them. 因此,我假设其中有80个谓词为“ nif”的谓词。

Then I added the line containing rdfs:label to the code and it produced no results. 然后,将包含rdfs:label的行添加到代码中,但未产生任何结果。 So, I wrote the code below to first filter the URI's containing 'nif': 因此,我编写了以下代码,首先过滤了包含“ nif”的URI:

SELECT DISTINCT ?p ?label WHERE{ ?s ?p ?o . FILTER (REGEX(STR(?p), "nif", "i")) . ?p rdfs:label ?label . } ORDER BY ?p

But it did not work. 但这没有用。 I tried using ?pa rdf:Property instead of the ?s ?p ?o and that did not work either. 我尝试使用?pa rdf:Property而不是?s ?p ?o ,但这也不起作用。 I then tried the Exist and Values ?p {"nif"} but I was unsuccessful with those two too! 然后,我尝试了Exist and Values ?p {"nif"}但是我也没有成功!

Where am I making mistake? 我在哪里犯错?

Used vs. declared properties: In RDF, there is a difference between using a predicate and declaring a predicate. 使用的属性与声明的属性:在RDF中, 使用谓词和声明谓词之间是有区别的。 It is possible to use a predicate without declaring it, and it is possible to declare a predicate without using it. 可以不声明而使用谓词,也可以不使用而声明谓词。

(It is also possible—and common—to declare a predicate in one file, and use it in a different file. This is how RDF enables re-use of a single ontology in different datasets. There may or may not be an owl:imports statement that links the two files.) (在一个文件中声明谓词,然后在另一个文件中使用谓词也是可能且很常见的。这就是RDF如何在不同数据集中重用单个本体的方法。可能有也可能没有owl:imports链接两个文件的owl:imports语句。)

To list all predicates used in the default graph: 列出默认图中使用的所有谓词:

SELECT DISTINCT ?predicate {
    ?s ?predicate ?o
}
ORDER BY ?predicate

To list all predicates declared in the default graph, we need to consider which schema language is used to declare it. 要列出默认图中声明的所有谓词,我们需要考虑使用哪种架构语言对其进行声明。 To list predicates declared with RDF Schema : 列出使用RDF Schema声明的谓词:

SELECT ?predicate {
    ?predicate a rdf:Property
}
ORDER BY ?predicate

To list predicates declared with OWL : 列出用OWL声明的谓词:

SELECT ?predicate ?type {
    VALUES ?type { owl:ObjectProperty owl:DatatypeProperty owl:AnnotationProperty }
    ?predicate a ?type
}
ORDER BY ?predicate

The query above takes into account that OWL has three different types of predicates: object properties, datatype properties, and annotation properties. 上面的查询考虑到OWL具有三种不同类型的谓词:对象属性,数据类型属性和注释属性。 So we basically query for each of the three. 所以我们基本上查询这三个。

With this knowledge, it should be possible to find out what predicates are used in the ontology, and what predicates are declared in the ontology. 有了这些知识,应该可以发现本体中使用了哪些谓词,以及本体中声明了哪些谓词。

Now, about labels. 现在,关于标签。 The queries above all return the URI—a machine-readable identifier—for the predicates. 最重要的查询返回谓词的URI(机器可读的标识符)。 To also retrieve labels, add ?label to the list of variables in the SELECT clause, and add this to the WHERE { ... } block: 要还检索标签,请将?label添加到SELECT子句中的变量列表中,然后将其添加到WHERE { ... }块中:

OPTIONAL { ?predicate rdfs:label ?label }

For example: 例如:

SELECT ?predicate ?label {
    ?predicate a rdf:Property
    OPTIONAL { ?predicate rdfs:label ?label }
}
ORDER BY ?predicate

We make the pattern that retrieves the label optional, so if no label is provided in the default graph, the predicate is still returned but without a value for the ?label variable. 我们将检索标签的模式设为可选,因此,如果默认图中未提供标签,则仍返回谓词,但没有?label变量的值。 This way, one can identify cases where a predicate exists (that is, it is either used or declared) but no label is provided. 这样,就可以确定存在谓词(即使用或声明谓词)但不提供标签的情况。

If a predicate is declared but no label provided, then I would assume it's a low-quality ontology where insufficient care has been taken in its creation. 如果声明了谓词但未提供标签,那么我认为这是一种低质量的本体,在创建时未充分注意。

If a predicate is used but no label provided, I would not be surprised at all. 如果使用谓词但未提供标签,我将不会感到惊讶。 It might just mean that the declaration and label is provided in a different file, and one needs to find that file and add it to the dataset in order to query labels. 这可能仅意味着声明和标签是在另一个文件中提供的,并且需要查找该文件并将其添加到数据集中以查询标签。

Constructing labels from the URI: If the problem is a lack of labels in the ontology, and the labels also cannot be found elsewhere, then here is a version that constructs a best-effort label from the last part of the URI in case no label is declared: 从URI构造标签:如果问题是本体中缺少标签,并且在其他地方也找不到标签,那么这里是一个从URI的最后部分构造尽力而为标签的版本,以防没有标签声明:

OPTIONAL {
    ?predicate rdfs:label ?tmpl
}
BIND (coalesce(?tmpl, replace(replace(replace(str(?predicate), '.*[#/:]', ''), '_', ' '), '([a-z])([A-Z])', '$1 $2')) AS ?label)

This takes everything after the last hash, slash or colon in the URI, replaces underscores with spaces, and inserts spaces between words in CamelCase notation. 这将占用URI中最后一个哈希,斜杠或冒号之后的所有内容,用空格替换下划线,并以CamelCase表示法在单词之间插入空格。

Lastly, filtering by URI. 最后,按URI进行过滤。 Here it is important to be aware that filtering will only happen on the “raw” URI, and not on the prefix-abbreviated form. 在此重要的是要注意,过滤只会在“原始” URI上发生,而不是在前缀缩写形式上发生。 For example, the following filter accepts only predicates with rdfs in the URI: 例如,以下过滤器仅接受URI中带有rdfs谓词:

FILTER regex(str(?predicate), 'rdfs', 'i')

But it would actually reject rdfs:label , rdfs:comment and any other properties in the rdfs namespace, because their full URIs are of the form 但这实际上会拒绝rdfs:labelrdfs:comment以及rdfs命名空间中的任何其他属性,因为它们的完整URI的格式为

<http://www.w3.org/2000/01/rdf-schema#label>

so the URI actually doesn't contain the string rdfs . 因此URI实际上不包含字符串rdfs Something to keep in mind. 要记住的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM