首页 文章

计算自定义直方图度量标准以使用 SPARQL 了解图形结构

提问于
浏览
2

我想分析图的结构,我想尝试的一个特定查询是在图中提取主题类型的不同组合 - 边类型 - 对象类型

这是我之前几个问题的后续跟进:

如何使用 SPARQL 查询生成适合特定节点类型 or/and 边缘类型的所有三元组?

如何使用 SPARQL 查询列出和计算图数据中不同类型的节点和边缘实体?

例如:如果存在边缘 types(property/predicate 类型的语义图)

  • IsCapitalOf

  • IsCityOf

  • 有人口等等

如果节点类型如下:

  • 城市

  • 国家

  • 里弗斯

  • 山等

然后我应该得到:

  • City-> IsCapitalOf-> Country 4 元组

  • City-> IsCityOf-> Country 21 元组

  • 河 - > IsPartOf->国家 3

  • River-> PassesThrough-> City 11

等等...

注意:对象字段中没有文字,因为我希望单位子图模式拟合(subjecttype edgetype objecttype)

总结一下:我认为我接近这个的方式是:

a)在图中计算不同的主题类型 b)在图中计算不同的边类型 c)在图中计算不同的对象类型(在之前的问题中已经回答了 a/b/c)

现在 d)生成所有可能的 combinations(of 主题类型 - >边缘类型 - >对象 type(NO 文字)并对这些模式进行计数(如直方图)

希望这个问题能够得到很好的表达

编辑:添加样本数据[5]这是 yago 数据集,可以公开获取

<Alabama>   rdf:type    <wordnet_country_108544813> .
<Abraham_Lincoln>   rdf:type    <wordnet_president_110467179> .
<Aristotle> rdf:type    <wordnet_writer_110794014> .
<Academy_Award_for_Best_Art_Direction>  rdf:type    <wordnet_award_106696483> .
<Academy_Award> rdf:type    <wordnet_award_106696483> .
<Actrius>   rdf:type    <wordnet_movie_106613686> .
<Animalia_(book)>   rdf:type    <wordnet_book_106410904> .
<Ayn_Rand>  rdf:type    <wordnet_novelist_110363573> .
<Allan_Dwan>    rdf:type    <wikicategory_American_film_directors> .
<Algeria>   rdf:type    <wordnet_country_108544813> .
<Andre_Agassi>  rdf:type    <wordnet_player_110439851> .
<Austro-Asiatic_languages>  rdf:type    <wordnet_language_106282651> .
<Afroasiatic_languages> rdf:type    <wordnet_language_106282651> .
<Andorra>   rdf:type    <wordnet_country_108544813> .
<Animal_Farm>   rdf:type    <wordnet_novelette_106368962> .
<Alaska>    rdf:type    <wordnet_country_108544813> .
<Aldous_Huxley> rdf:type    <wordnet_writer_110794014> .
<Andrei_Tarkovsky>  rdf:type    <wordnet_film_maker_110088390> .

1 回答

  • 4

    假设你有这样的数据:

    @prefix : <http://stackoverflow.com/q/24313367/1281433/> .
    
    :City1 a :City .
    :City2 a :City .
    
    :Country1 a :Country .
    :Country2 a :Country .
    :Country3 a :Country .
    
    :River1 a :River .
    :River2 a :River .
    :River3 a :River .
    
    :City1 :isCapitalOf :Country1 .
    
    :River1 :isPartOf :Country1, :Country2 .
    :River2 :isPartOf :Country2, :Country3 .
    
    :River1 :passesThrough :City1, :City2 .
    :River2 :passesThrough :City2 .
    

    然后,这个查询为您提供了您想要的实物结果,我认为:

    prefix : <http://stackoverflow.com/q/24313367/1281433/>
    
    select ?type1 ?p ?type2 (count(distinct *) as ?count) where {
       [ a ?type1 ; ?p [ a ?type2 ] ] 
    }
    group by ?type1 ?p ?type2
    
    ----------------------------------------------
    | type1  | p              | type2    | count |
    ==============================================
    | :River | :passesThrough | :City    | 3     |
    | :City  | :isCapitalOf   | :Country | 1     |
    | :River | :isPartOf      | :Country | 4     |
    ----------------------------------------------
    

    如果您对[ … ]空白节点语法不太满意,则可能有助于查看扩展形式:

    SELECT  ?type1 ?p ?type2 (count(distinct *) AS ?count)
    WHERE
      { _:b0 rdf:type ?type1 .
        _:b0 ?p _:b1 .
        _:b1 rdf:type ?type2
      }
    GROUP BY ?type1 ?p ?type2
    

    但是,这只能捕获具有类型的东西。如果你想要包含那些没有rdf:type的东西,你可以这样做

    SELECT  ?type1 ?p ?type2 (count(distinct *) AS ?count) { 
        ?x ?p ?y
        optional { ?x a ?type1 }
        optional { ?y a ?type2 }
    }
    GROUP BY ?type1 ?p ?type2
    

相关问题