首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用SPARQL计算自定义直方图度量来理解图形结构

使用SPARQL计算自定义直方图度量来理解图形结构
EN

Stack Overflow用户
提问于 2014-06-19 17:59:55
回答 1查看 197关注 0票数 2

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

这是我之前提出的几个问题的后续:

How to generate all triples that fit a particular node type or/and edge type using SPARQL query?

How to list and count the different types of node and edge entities in the graph data using SPARQL query?

例如:如果有一个具有边缘类型的语义图(属性/谓词类型)

  1. IsCapitalOf
  2. IsCityOf
  3. HasPopulation等

如果节点类型类似于:

  1. 城市
  2. 国家/地区
  3. 河文
  4. 山等

那我应该得到:

  1. 城市->首都->国家4元组
  2. 城市->IsCityOf->国家21元组
  3. 河流->IsPartOf->国家3
  4. 河->通道穿过->城市11

等等..。

注意:对象字段中没有文字,因为我想要单元子图模式拟合(主题类型、边缘类型、对象类型)。

概括地说:我想我应该这样做:

a)在图b中计算不同的主题类型( b)在图中计算不同的边缘类型( c)在图中计算不同的对象类型(a/b/c已在我以前的问题中得到回答)

现在d)生成所有可能的组合(主题类型、->边缘类型、->对象类型(没有文字)并计数(如直方图)这些模式。

希望这个问题能表达得相当清楚。

编辑:从整个数据集中添加几行示例数据--这是公开可用的yago数据集。

代码语言:javascript
运行
复制
<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> .
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-19 19:06:16

假设您有这样的数据:

代码语言:javascript
运行
复制
@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 .

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

代码语言:javascript
运行
复制
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 
代码语言:javascript
运行
复制
----------------------------------------------
| type1  | p              | type2    | count |
==============================================
| :River | :passesThrough | :City    | 3     |
| :City  | :isCapitalOf   | :Country | 1     |
| :River | :isPartOf      | :Country | 4     |
----------------------------------------------

如果您对[ … ]空白节点语法不太满意,那么查看扩展表单可能会有所帮助:

代码语言:javascript
运行
复制
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的东西,那么您应该去做

代码语言:javascript
运行
复制
SELECT  ?type1 ?p ?type2 (count(distinct *) AS ?count) { 
    ?x ?p ?y
    optional { ?x a ?type1 }
    optional { ?y a ?type2 }
}
GROUP BY ?type1 ?p ?type2
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24313367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档