简体   繁体   English

SPARQL:找出高数据属性值

[英]SPARQL: Figure out high data property values

I have a quiz game in which students have to solve questions from three categories like Chemistry, English, Physics. 我有一个问答游戏,学生必须解决三个类别的问题,例如化学,英语,物理。 Students will score points in these categories like student1 has 50 in Chemistry, 70 in English and 65 in Physics. 学生将在这些类别中得分,例如Student1在化学方面的得分为50,在英语方面的得分为70,在物理方面的得分为65。

I can figure out in which category a student has highest score. 我可以算出学生在哪个类别中得分最高。 But how can I get something like which one is the highest score category any student have? 但是,如何获得某个分数最高的类别呢? I mean if a student got 90 points in English (No other student got this score), then how can we figure out this the top score of English is 90. 我的意思是,如果一个学生的英语获得90分(没有其他学生获得此分数),那么我们如何才能算出英语的最高得分是90。

Remember: English score, Chemistry score, Physics score are data properties stored in rdf file. 请记住:英语分数,化学分数,物理分数是存储在rdf文件中的数据属性。 I want if it is possible using Jena rules or SPARQL or plain Java code. 我想是否可以使用Jena规则或SPARQL或纯Java代码。

If I understand you correctly, you're asking to find the maximum score in each category, and then to find, for each category, the student with that highest score in that category. 如果我对您的理解正确,则要求您查找每个类别中的最高分数,然后针对每个类别查找该类别中具有最高分数的学生。 It's easier to work with data (in the future, please try to provide minimal data that we can work with), so here's some sample data: 使用数据更容易(将来,请尝试提供我们可以使用的最少数据),因此这里是一些示例数据:

@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 ] .

There are four categories, and student1 has the highest score in category1, student3 has the highest score in categories 2 and 4, and student2 has the highest score in category 3. We can write a query like this: 有四个类别,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        |
--------------------------------------

If you don't care about which student got the highest score, then you just want that inner subquery: 如果您不关心哪个学生得分最高,那么您只需要内部子查询:

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        |
--------------------------

If you're using different properties 如果您使用其他属性

In a comment, you asked, 您在评论中问,

I have my ontology like this: Student1 :Englishscore 90; 我的本体是这样的:Student1:Englishscore 90; PhyscicsScore 67; 物理评分67; ChemScore 78. Similarly for other students. ChemScore 78.其他学生也是如此。 Should I introduce a blank node like hasScore which reference to Englishscore, PhyscicsScore [sic], and ChemScore? 我是否应该引入像hasScore这样的空白节点,它引用Englishscore,PhyscicsScore [sic]和ChemScore?

First, I'd recommend that you standardize your naming convention. 首先,我建议您标准化命名约定。 First, be sure to use correct spelling (eg, Physics ). 首先,请确保使用正确的拼写(例如Physics )。 Then, either abbreviate or don't. 然后,缩写或不缩写。 You're abbreviating Chemistry to Chem , but not English to Eng . 您是将化学缩写为Chem ,而不是英语缩写为Eng Finally, be consistent in your capitalization (eg, EnglishScore , not Englishscore ). 最后,在你的资产(例如,EnglishScore,Englishscore)一致。

It's not necessary to use the kind of representation that I used. 不必使用我使用的那种表示形式。 You didn't provide sample data (please do in the future), so I used what I considered a fairly easy one to use. 您没有提供示例数据(请在将来提供),因此我使用了我认为非常易于使用的数据。 Your representation seems a bit less flexible, but you can still get the information you want. 您的表示方式似乎不太灵活,但是您仍然可以获得所需的信息。 Here's some new sample data: 这是一些新的示例数据:

@prefix : <urn:ex:>

:student1 :hasCat1Score 90 ;
          :hasCat2Score 75 ;
          :hasCat3Score 85 .

:student2 :hasCat2Score 75 ;
          :hasCat3Score 90 ;
          :hasCat4Score 90 .

:student3 :hasCat1Score 85 ;
          :hasCat2Score 80 ;
          :hasCat4Score 95 .

Then the query just needs to use a variable for the property, and that variable simultaneously relates the student to the score, and also indicates the category. 然后查询只需要为属性使用一个变量,该变量将学生与分数同时关联,并指示类别。 So you'd still just group by that property and ask for the highest score: 因此,您仍然只是按该属性分组并要求获得最高分数:

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