简体   繁体   English

dbpedia的snorql sparql按最后一个字符排序

[英]dbpedia's snorql sparql sorting by the last character

I want to sort the ?Artis by its last character. 我想按?Artis的最后一个字符对其进行排序。 I tried to just do order by , but the result is sorted by its first character. 我试图按进行order by ,但结果按其第一个字符排序。

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX xs: <http://www.w3.org/2001/XMLSchema#>

SELECT * WHERE {
 ?Artis onto:birthDate ?Tanggal_Lahir . 
 FILTER (?Tanggal_Lahir= "1990-01-05"^^xs:date)
} ORDER BY DESC (?Artis) LIMIT 10

Since SPARQL 1.1, you can use BIND to bind values to variables. 从SPARQL 1.1开始,您可以使用BIND将值绑定到变量。 The rest is just a matter of string hacks, eg replacement + regex like in this example: 剩下的只是字符串黑客的问题,例如本例中的replace + regex:

replace(strafter(str(?Artis), str(dbr:))

converts the IRI http://dbpedia.org/resource/Some_Example to the string Some_Example 将IRI http://dbpedia.org/resource/Some_Example转换为字符串Some_Example

Then 然后

replace(strafter(str(?Artis), str(dbr:)), ".*(.)$", "$1") as ?lastChar)

picks the last char via a regex, $1 represents the group in the regex. 通过正则表达式选择最后一个字符, $1代表正则表达式中的组。

The final query would be 最终查询将是

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT * WHERE {
 ?Artis dbo:birthDate ?Tanggal_Lahir . 
 FILTER (?Tanggal_Lahir = "1990-01-05"^^xsd:date)
 BIND(replace(strafter(str(?Artis), str(dbr:)), ".*(.)$", "$1") as ?lastChar)
} 
ORDER BY DESC (?lastChar) 
LIMIT 10

Result (sample): 结果(样本):

+------------------------------------------------------+---------------+----------+
|                        Artis                         | Tanggal_Lahir | lastChar |
+------------------------------------------------------+---------------+----------+
| http://dbpedia.org/resource/Barış_Memiş              | 1990-01-05    | ş        |
| http://dbpedia.org/resource/Asha_Roy                 | 1990-01-05    | y        |
| http://dbpedia.org/resource/Gaurav_Pandey            | 1990-01-05    | y        |
| http://dbpedia.org/resource/Eldar_Ragib_Ogly_Mamedov | 1990-01-05 v  |          |
| http://dbpedia.org/resource/Akeem_Thomas             | 1990-01-05    | s        |
| ...                                                  | ...           | ...      |
+------------------------------------------------------+---------------+----------+

Note, this indeed also picks a char like ) the result value, eg for the resource http://dbpedia.org/resource/Stephen_Stirling_(footballer) the result will be 请注意,这的确也选择了一个字符,例如)作为结果值,例如对于资源http://dbpedia.org/resource/Stephen_Stirling_(footballer) ,结果将是

+-----------------------------------------------------------+-------------+---+
| http://dbpedia.org/resource/Stephen_Stirling_(footballer) | 1990-01-05  | ) |
+-----------------------------------------------------------+-------------+---+

In addition, this only works for DBpedia resources beginning with the namespace http://dbpedia.org/resource/ . 另外,这仅适用于以命名空间http://dbpedia.org/resource/开头的DBpedia资源。 For arbitrary datasets, omit the strafter part and just use the given regex. 对于任意数据集,省略strafter部分,而仅使用给定的正则表达式。

As a side note, it would be good if you stick to common namespace declarations, eg dbo instead of onto and xsd instead of xs . 附带说明一下,如果坚持使用通用的名称空间声明,例如dbo而不是ontoxsd而不是xs ,那将是很好的。

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

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