[英]SPARQL: Figure out high data property values
我有一個問答游戲,學生必須解決三個類別的問題,例如化學,英語,物理。 學生將在這些類別中得分,例如Student1在化學方面的得分為50,在英語方面的得分為70,在物理方面的得分為65。
我可以算出學生在哪個類別中得分最高。 但是,如何獲得某個分數最高的類別呢? 我的意思是,如果一個學生的英語獲得90分(沒有其他學生獲得此分數),那么我們如何才能算出英語的最高得分是90。
請記住:英語分數,化學分數,物理分數是存儲在rdf文件中的數據屬性。 我想是否可以使用Jena規則或SPARQL或純Java代碼。
如果我對您的理解正確,則要求您查找每個類別中的最高分數,然后針對每個類別查找該類別中具有最高分數的學生。 使用數據更容易(將來,請嘗試提供我們可以使用的最少數據),因此這里是一些示例數據:
@prefix : <urn:ex:>
:student1 :hasScore [ :inCategory :category1 ; :value 90 ] ,
[ :inCategory :category2 ; :value 75 ] ,
[ :inCategory :category3 ; :value 85 ] .
:student2 :hasScore [ :inCategory :category2 ; :value 75 ] ,
[ :inCategory :category3 ; :value 90 ] ,
[ :inCategory :category4 ; :value 90 ] .
:student3 :hasScore [ :inCategory :category1 ; :value 85 ] ,
[ :inCategory :category2 ; :value 80 ] ,
[ :inCategory :category4 ; :value 95 ] .
有四個類別,student1在類別1中得分最高,student3在類別2和4中得分最高,student2在類別3中得分最高。我們可以這樣編寫查詢:
prefix : <urn:ex:>
select ?category ?student ?highScore where {
#-- Find the high score in each category
{ select ?category (max(?score) as ?highScore) {
?student :hasScore [ :inCategory ?category ; :value ?score ] .
}
group by ?category
}
#-- Then find the student that had that high
#-- score in the category.
?student :hasScore [ :inCategory ?category ; :value ?highScore ] .
}
--------------------------------------
| category | student | highScore |
======================================
| :category1 | :student1 | 90 |
| :category2 | :student3 | 80 |
| :category3 | :student2 | 90 |
| :category4 | :student3 | 95 |
--------------------------------------
如果您不關心哪個學生得分最高,那么您只需要內部子查詢:
prefix : <urn:ex:>
select ?category (max(?score) as ?highScore) {
?student :hasScore [ :inCategory ?category ; :value ?score ] .
}
group by ?category
--------------------------
| category | highScore |
==========================
| :category1 | 90 |
| :category2 | 80 |
| :category3 | 90 |
| :category4 | 95 |
--------------------------
您在評論中問,
我的本體是這樣的:Student1:Englishscore 90; 物理評分67; ChemScore 78.其他學生也是如此。 我是否應該引入像hasScore這樣的空白節點,它引用Englishscore,PhyscicsScore [sic]和ChemScore?
首先,我建議您標准化命名約定。 首先,請確保使用正確的拼寫(例如Physics )。 然后,縮寫或不縮寫。 您是將化學縮寫為Chem ,而不是英語縮寫為Eng 。 最后,在你的資產(例如,EnglishScore,不Englishscore)一致。
不必使用我使用的那種表示形式。 您沒有提供示例數據(請在將來提供),因此我使用了我認為非常易於使用的數據。 您的表示方式似乎不太靈活,但是您仍然可以獲得所需的信息。 這是一些新的示例數據:
@prefix : <urn:ex:>
:student1 :hasCat1Score 90 ;
:hasCat2Score 75 ;
:hasCat3Score 85 .
:student2 :hasCat2Score 75 ;
:hasCat3Score 90 ;
:hasCat4Score 90 .
:student3 :hasCat1Score 85 ;
:hasCat2Score 80 ;
:hasCat4Score 95 .
然后查詢只需要為屬性使用一個變量,該變量將學生與分數同時關聯,並指示類別。 因此,您仍然只是按該屬性分組並要求獲得最高分數:
prefix : <urn:ex:>
select ?hasScore (max(?score) as ?highScore) {
?student ?hasScore ?score
}
group by ?hasScore
-----------------------------
| hasScore | highScore |
=============================
| :hasCat1Score | 90 |
| :hasCat2Score | 80 |
| :hasCat3Score | 90 |
| :hasCat4Score | 95 |
-----------------------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.