简体   繁体   中英

SPARQL Most Frequent Predicates

I want to find the top k frequent predicates in a graph. That is the predicates that occur in more triples. How can this be done using SPARQL?

You can do this using the group and aggregation features of the query language. You want to group by the predicate and count all the triples that use the predicate eg

SELECT ?p (COUNT(*) AS ?usages)
WHERE
{
  GRAPH <http://your-graph.com> { ?s ?p ?o } 
}
GROUP BY ?p
ORDER BY DESC(?usages)
LIMIT 5

You can then sort by descending number of usages and limit the results to obtain just the top K

See the spec for more examples - https://www.w3.org/TR/sparql11-query/#aggregates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM