[英]Sparql query for a model made of classes and properties, like freebase topics
[英]converting freebase MQL to SPARQL
以下freebase MQL為每位藝術家找到5位藝術家和50張專輯。
[{
"type" : "/music/artist",
"name":null,
"album" : [{
"name" : null,
"count":null,
"limit":50
}],
"limit":5
}]
我可以像這樣編寫SPARQL:
SELECT ?artist ?album
WHERE
{
?artist :type :/music/artist .
?artist :album ?album
}
LIMIT n
但是,我不知道應該指定多少n
因為據我所知,SPARQL沒有層次結構。
以下子查詢看起來像工作。
SELECT ?artist ?album
WHERE
{
?artist :album ?album .
{
SELECT ?artist
WHERE
{
?artist :type :/music/artist
}
LIMIT k
}
}
LIMIT n
但我不知道如何指定k
, n
來獲得50張專輯,前5位藝術家。
誰能寫出SPARQL
,為每位藝術家印刷5位藝術家和他們的5幅畫作?
下面的查詢打印藝術家及其繪畫,沒有LIMIT
結果。
PREFIX dbpedia-owl:<http://dbpedia.org/ontology/>
PREFIX prop:<http://dbpedia.org/property/>
SELECT ?painting ?artist
WHERE
{
?painting prop:artist ?artist .
{
SELECT ?artist
{
?artist rdf:type dbpedia-owl:Artist.
}
}
}
謝謝。
Max和我在聊天中進行了一些討論,這最終可能與Max采用的方法相同。 不過,我認為它更具可讀性。 它有15位藝術家的專輯,每張專輯最多5張專輯。 如果您希望能夠包含沒有任何相冊的藝術家,您需要選擇一些部分。
select ?artist ?album {
#-- select 15 bands that have albums (i.e.,
#-- such that they are the artist *of* something).
{
select distinct ?artist {
?artist a dbpedia-owl:Band ;
^dbpedia-owl:artist []
}
limit 15
}
#-- grab ordered pairs (x,y) (where y > x) of their
#-- albums. By asking how many x's for each y, we
#-- get just the first n y's.
?artist ^dbpedia-owl:artist ?album, ?album_
filter ( ?album_ <= ?album )
}
group by ?artist ?album
having count(?album_) <= 5 #-- take up 5 albums for each artist
order by ?artist ?album
基於您想要獲得的結果,這涉及某種嵌套的共同相關的子查詢處理,這在單個SPARQL查詢中是不可行的(至少根據我的理解,但如果有可能,我完全在;)):
由於SPARQL查詢評估的自下而上的性質,首先對邏輯上的子查詢進行評估,並將結果投影到外部查詢。
在使用子查詢進行連接評估之后應用的第二個限制子句,它將僅限制外部查詢的結果數。
在第二個try的子查詢中使用LIMIT k(k = 5)子句將有效地返回您需要的5位藝術家,但是將n限制為50只會強制將所有這5位藝術家的專輯結果(外部查詢)全局50個結果而不是你想要的50 /藝術家。 從里到外轉動查詢會產生類似的效果。
編輯:一個可能的解決方案是為所有藝術家/專輯構建一個子查詢,並限制子查詢(某種程度上)訂購的專輯數量低於50的位置(這里使用專輯
標題
IRI排序)
PREFIX dbpedia-owl:<http://dbpedia.org/ontology/>
PREFIX prop:<http://dbpedia.org/property/>
SELECT ?artist ?outputAlbum
WHERE
{
{
SELECT ?artist (MAX(str(?album1)) as ?maxedAlbum)
WHERE {
?album1 prop:artist ?artist .
?album2 prop:artist ?artist .
FILTER (str(?album2) < str(?album1))
}
GROUP BY ?artist
HAVING count(?album2)<= 50
LIMIT 5
}
?outputAlbum prop:artist ?artist .
FILTER (str(?outputAlbum) < str(?maxedAlbum))
}
編輯2 :最后一個查詢將是天真的方法,但似乎在dbpedia端點上有一些推斷(未知的“gime”)(如下所示)。更精確的查詢需要更多的過濾器和不同的子句 - 我添加輸出中的不同和全局計數,以顯示某處仍有一些推論):
PREFIX dbpedia-owl:<http://dbpedia.org/ontology/>
PREFIX prop:<http://dbpedia.org/property/>
SELECT ?artist ?outputAlbum ?maxedCount ?inferredCrossJoinCount
WHERE
{
{
SELECT ?artist (MAX(str(?album1)) as ?maxedAlbum) (count(distinct ?album2) as ?maxedCount) (count(?album2) as ?inferredCrossJoinCount)
WHERE {
?artist rdf:type dbpedia-owl:Artist .
?album1 ?p ?artist .
?album2 ?p ?artist .
FILTER (sameTerm(?p, prop:artist))
FILTER (str(?album1) < str(?album2))
}
GROUP BY ?artist
#HAVING count(?album2)<= 50
LIMIT 5
}
?outputAlbum ?p ?artist .
FILTER (sameTerm(?p, prop:artist))
FILTER (str(?outputAlbum) < str(?maxedAlbum))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.