SPARQL:找出高数据属性值 [英] SPARQL: Figure out high data property values

查看:123
本文介绍了SPARQL:找出高数据属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问答游戏,学生必须解决三个类别的问题,例如化学,英语,物理.学生将在这些类别中得分,例如学生1在化学方面的得分为50,英语为70,物理为65.

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.

我可以算出学生在哪个类别中得分最高.但是,如何获得某个分数是所有学生中得分最高的类别呢?我的意思是,如果一个学生的英语获得90分(没有其他学生获得此分数),那么我们如何才能算出英语的最高得分是90.

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.

记住:英语分数,化学分数,物理分数是存储在rdf文件中的数据属性. 我想是否可以使用Jena规则或SPARQL或纯Java代码.

Remember: English score, Chemistry score, Physics score are data properties stored in rdf file. I want if it is possible using Jena rules or SPARQL or plain Java code.

推荐答案

如果我对您的理解正确,您将要求查找每个类别的最高分数,然后针对每个类别查找具有最高分数的学生在该类别中得分.处理数据更容易(以后,请尝试提供我们可以使用的最少数据),因此这里是一些示例数据:

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

有四个类别,student1在类别1中得分最高,student3在类别2和4中得分最高,student2在类别3中得分最高.我们可以这样编写查询:

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:

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,

我的本​​体是这样的:Student1:Englishscore 90; 物理评分67; ChemScore 78.其他学生也是如此.我是不是该 引入一个像hasScore这样的空白节点,它引用了Englishscore, PhyscicsScore [sic]和ChemScore?

I have my ontology like this: Student1 :Englishscore 90; PhyscicsScore 67; ChemScore 78. Similarly for other students. Should I introduce a blank node like hasScore which reference to Englishscore, PhyscicsScore [sic], and ChemScore?

首先,我建议您标准化命名约定.首先,请确保使用正确的拼写(例如,物理).然后,缩写或不缩写.您是将 Chemistry 缩写为 Chem ,而不是将 English 缩写为 Eng .最后,请保持大小写一致(例如, EnglishScore ,而不是 Englishscore ).

First, I'd recommend that you standardize your naming convention. First, be sure to use correct spelling (e.g., Physics). Then, either abbreviate or don't. You're abbreviating Chemistry to Chem, but not English to Eng. Finally, be consistent in your capitalization (e.g., EnglishScore, not 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        |
-----------------------------

这篇关于SPARQL:找出高数据属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆