我希望分析一个图的结构,我想要尝试的一个特别的查询是提取图中的主题类型-边缘类型-对象类型的不同组合。
这是我之前提出的几个问题的后续:
How to generate all triples that fit a particular node type or/and edge type using SPARQL query?
例如:如果有一个具有边缘类型的语义图(属性/谓词类型)
如果节点类型类似于:
那我应该得到:
等等..。
注意:对象字段中没有文字,因为我想要单元子图模式拟合(主题类型、边缘类型、对象类型)。
概括地说:我想我应该这样做:
a)在图b中计算不同的主题类型( b)在图中计算不同的边缘类型( c)在图中计算不同的对象类型(a/b/c已在我以前的问题中得到回答)
现在d)生成所有可能的组合(主题类型、->边缘类型、->对象类型(没有文字)并计数(如直方图)这些模式。
希望这个问题能表达得相当清楚。
编辑:从整个数据集中添加几行示例数据--这是公开可用的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> .发布于 2014-06-19 19:06:16
假设您有这样的数据:
@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 ?type2https://stackoverflow.com/questions/24313367
复制相似问题