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

江枫思渺然 提交于 2019-12-31 03:02:52

问题


I'm looking to provide some summary stats for a data set and I want to list the different types of edge entities and node(vertex) entities in the graph.

For example:

-> In Twitter Social network graph of users and following relationship (Homogeneous graph), there is only one type of vertex entity (user), but in heterogeneous graphs such as ConceptNet data, it will have multiple values.

-> The edge entities can be computed by just counting the different number of predicates I believe using the query :

SELECT DISTINCT (?p AS ?DistinctEdges)  { ?s ?p ?o }

But I am not sure how to do so for vertices. The vertex type can be from a subject or object field of the triple and the object in turn can be either a value(literal) or another resource itself.

Please excuse me if I have gone wrong with the vocabulary anywhere. I have just started working on building a semantic web application.


回答1:


You can use the UNION clause to combine two patterns in conjunction with a FILTER clause using the IsLiteral() function to omit literals e.g.

SELECT DISTINCT ?vertex
WHERE
{
  { 
    ?vertex ?p [] 
  }
  UNION
  { 
    [] ?p ?vertex 
    FILTER(!IsLiteral(?vertex))
  }
}

The [] is an anonymous variable because you don't care about the some of the positions on either side of the UNION so by giving them an anonymous variable we match any value but don't carry those values out in the query.

The FILTER clause in the RHS of the union is used to filter out objects which are literals. It is not necessary to have this in the LHS because RDF forbids literal subjects so any ?vertex value from the LHS must be a resource i.e. a URI/blank node



来源:https://stackoverflow.com/questions/24187351/how-to-list-and-count-the-different-types-of-node-and-edge-entities-in-the-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!