在 dbpedia 上使用 SPARQL 计算 wikiPageLength 的方差

Calculate variance of wikiPageLength with SPARQL on dbpedia

我想计算维基百科页面长度历史的方差(或标准差),对于轻小说书籍(Light_novels),与 dbpedia(http://live.dbpedia.org/sparql)。

SPARQL 在这里:

PREFIX dbpedia0: <http://dbpedia.org/ontology/> 
PREFIX term: <http://purl.org/dc/terms/>

SELECT DISTINCT ?item ((SUM((?n - ?mean)*(?n - ?mean)))/(COUNT(?n) - 1) AS ?variance) 
WHERE
{
    
?item term:subject dbc:Light_novels . 

  ?item dbpedia0:wikiPageLength ?n
  {
      SELECT ?item (AVG(?n) AS ?mean) WHERE {
        ?item dbpedia0:wikiPageLength ?n . 
      } 
  }

}        

这给出了一个错误 Virtuoso 37000 Error SP031: SPARQL compiler: The name ?n is used in multiple clauses, without any logical connection 我无数次盲目地修改代码,但最终不知道如何实现。

非常感谢您提供有用的评论。谢谢

通常这应该有效

PREFIX dbpedia0: <http://dbpedia.org/ontology/> 
PREFIX term: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>


SELECT ?item IF(?count=1,0,(SUM(?x)/(?count-1))) as ?var
{
SELECT ?item ?n xsd:integer(?n) as ?ni ?mean xsd:integer(?ni-?mean) as ?nmean ((?nmean)*(?nmean)) as ?x ?count
{
  ?item dbpedia0:wikiPageLength ?n 
  {
     SELECT ?item AVG(?n2) as ?mean (COUNT(?item)) as $count
     {
       ?item term:subject dbc:Light_novels . 
       ?item dbpedia0:wikiPageLength ?n2
     }
     GROUP BY ?item
  }
}
} GROUP BY ?item ?count

但是它抛出这个错误

Internal Optimized compiler error : Bad dfe in sqlo_place_exp in sqldf.c:2491

所以我修改成了这个:

PREFIX dbpedia0: <http://dbpedia.org/ontology/> 
PREFIX term: <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?item IF(?count=1,-1,(?sx/(?count-1))) as ?var
{
SELECT ?item SUM(?x) as ?sx ?count
{
SELECT ?item ?n xsd:integer(?n) as ?ni ?mean xsd:integer(?ni-?mean) as ?nmean ((?nmean)*(?nmean)) as ?x ?count
{
  ?item dbpedia0:wikiPageLength ?n 
  {
     SELECT ?item AVG(?n2) as ?mean (COUNT(?item)) as $count
     {
       ?item term:subject dbc:Light_novels . 
       ?item dbpedia0:wikiPageLength ?n2
     }
     GROUP BY ?item
  }
}
} GROUP BY ?item ?count
}
  • 我们需要聚合一个。用于计算平均值和 b。计算每个 n 与平均值的差异之和。

  • 我需要将变量xsd:integer(?ni-?mean)类型转换为整型,否则不显示乘

    的结果
  • 使用运行第二个内部子查询你可以看到变量是如何被修改的