繁体   English   中英

SPARQL:找出高数据属性值

[英]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.

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