【问题标题】:RDF File count distinct in python queryRDF文件计数在python查询中不同
【发布时间】:2021-11-29 10:23:19
【问题描述】:

我有这个 rdf .ttl 文件示例: @前缀 ns1: http://schema.org/ 。 @prefix xsd: http://www.w3.org/2001/XMLSchema# .

<http://example.org/crime/100010117.0> ns1:beat "308" ;
    ns1:crime "AUTO_THEFT" ;
    ns1:date "1/1/2010" ;
    ns1:lat 3.369307e+01 ;
    ns1:location "960 CONSTITUTION RD SE" ;
    ns1:long -8.435805e+01 ;
    ns1:neighborhood "Norwood Manor" ;
    ns1:npu "Z" ;
    ns1:number 1.000101e+08 .

<http://example.org/crime/100010121.0> ns1:beat "309" ;
    ns1:crime "LARCENY-FROM_VEHICLE" ;
    ns1:date "1/1/2010" ;
    ns1:lat 3.368274e+01 ;
    ns1:location "2685 METROPOLITAN PKWY SW" ;
    ns1:long -8.440902e+01 ;
    ns1:neighborhood "Perkerson" ;
    ns1:npu "X" ;
    ns1:number 1.000101e+08 .

<http://example.org/crime/100010127.0> ns1:beat "208" ;
    ns1:crime "LARCENY-FROM_VEHICLE" ;
    ns1:date "1/1/2010" ;
    ns1:lat 3.385211e+01 ;
    ns1:location "3600 PIEDMONT RD NE" ;
    ns1:long -8.438044e+01 ;
    ns1:neighborhood "Buckhead Forest" ;
    ns1:npu "B" ;
    ns1:number 1.000101e+08 .

<http://example.org/crime/100010147.0> ns1:beat "512" ;
    ns1:crime "ROBBERY-PEDESTRIAN" ;
    ns1:date "1/1/2010" ;
    ns1:lat 3.375104e+01 ;
    ns1:location "FORSYTH ST SW / NELSON ST SW" ;
    ns1:long -8.439479e+01 ;
    ns1:neighborhood "Downtown" ;
    ns1:npu "M" ;
    ns1:number 1.000101e+08 .

<http://example.org/crime/100010149.0> ns1:beat "311" ;
    ns1:crime "BURGLARY-RESIDENCE" ;
    ns1:date "1/1/2010" ;
    ns1:lat 3.367399e+01 ;
    ns1:location "2950 SPRINGDALE RD SW" ;
    ns1:long -8.441557e+01 ;
    ns1:neighborhood "Hammond Park" ;
    ns1:npu "X" ;
    ns1:number 1.000101e+08 .

<http://example.org/crime/100010186.0> ns1:beat "501" ;
    ns1:crime "BURGLARY-RESIDENCE" ;
    ns1:date "1/1/2010" ;
    ns1:lat 3.378988e+01 ;
    ns1:location "288 16TH ST NW" ;
    ns1:long -8.439713e+01 ;
    ns1:neighborhood "Home Park" ;
    ns1:npu "E" ;
    ns1:number 1.000102e+08 .

我正在尝试计算不同类型的犯罪 (ns1:crime)

例如,我想要这样的结果

[
    {
        "crime": "AUTO_THEFT",
        "count": 1
    },
    {
        "crime": "LARCENY-FROM_VEHICLE",
        "count": 2
    },
    {
        "crime": "ROBBERY-PEDESTRIAN",
        "count": 1
    },
    {
        "crime": "ROBBERY-PEDESTRIAN",
        "count": 2
    }
]

因此不同类型的犯罪(不同)及其数量。

我试过这个:

def countTypes(g):
    crimes = []
    q = g.query(
        """
        PREFIX ns1: <http://schema.org/>
            SELECT ?crime (count(distinct ?crime) as ?crimeCount) WHERE {
                ?s ns1:crime ?crime .
            }""")
    for row in q:
        crimes.append(row)
    return crimes

但它不能正常工作。 关于如何做的任何想法? 谢谢

【问题讨论】:

    标签: python sparql rdf


    【解决方案1】:

    您可以使用GROUP BY 子句按犯罪值对结果进行分组,然后使用COUNT(不区分)来计算每个组中有多少个结果:

    PREFIX ns1: <http://schema.org/>
    SELECT ?crime (count(*) as ?crimeCount) WHERE {
        ?s ns1:crime ?crime .
    }
    GROUP BY ?crime
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多