Skip to content

Instantly share code, notes, and snippets.

@yayamamo
Last active November 29, 2023 00:58
Show Gist options
  • Save yayamamo/8052bd4620c1c58adff8 to your computer and use it in GitHub Desktop.
Save yayamamo/8052bd4620c1c58adff8 to your computer and use it in GitHub Desktop.

Revisions

  1. yayamamo renamed this gist Nov 12, 2014. 1 changed file with 31 additions and 9 deletions.
    40 changes: 31 additions & 9 deletions gistfile1.txt → SPARQL queries to obtain statistics.
    Original file line number Diff line number Diff line change
    @@ -4,26 +4,44 @@ SELECT DISTINCT ?c (STR(?l) AS ?lb)
    ?c a :class1 ;
    <http://www.w3.org/2000/01/rdf-schema#label> ?l .
    }

    # Obtain a list of classes.
    SELECT DISTINCT ?c
    WHERE {
    GRAPH :graph
    {[] a ?c .}
    }

    # Count instances belonging to a given class (:class1).
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    GRAPH :graph
    {?s a :class1 .}
    }
    # Count triples whose subject and object belong to :c1 and :c2, respectively.
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    ?o a :c2 .
    }}

    # Enumerate classes linked from a given class (:class1) and predicates that link instances of the given class and the target classes.
    # below q1 to q3 are indentical to each other (I think)
    ### q1
    SELECT ?p ?c (COUNT(?p) AS ?pc) {
    ?f a :class1 .
    ?t a ?c .
    ?f ?p ?t .
    FILTER(?c != owl:Class)
    } GROUP BY ?p ?c
    ### q2
    SELECT ?p ?c (COUNT(?p) AS ?pc) {
    ?f a :class1 ;
    ?p [ a ?c ].
    FILTER(!sameTerm(?c, owl:Class))
    } GROUP BY ?p ?c
    ### q3
    SELECT ?p ?c (COUNT(?p) AS ?pc) {
    ?f a :class1 ;
    ?p ?t ;
    !rdf:type ?t .
    ?t a ?c .
    } GROUP BY ?p ?c

    # Enumerate all the predicates with their counts under the condition that their subject and object belong to :c1 and :c2, respectively.
    SELECT ?p (COUNT(?p) AS ?rc)
    WHERE {
    @@ -33,6 +51,7 @@ SELECT ?p (COUNT(?p) AS ?rc)
    ?o a :c2 .
    }}
    GROUP BY ?p

    # Count triples whose subject and object belong to :c1 and a locally-undeclared class, respectively.
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    @@ -42,6 +61,7 @@ SELECT (COUNT(?s) AS ?rc)
    MINUS {?o a ?oc}
    FILTER(!isLiteral(?o) && ?p != rdf:type)
    }}

    # Enumerate all the predicates with their counts under the condition that their subject and object belong to :c1 and a locally-undeclared class, respectively.
    SELECT ?p (COUNT(?p) AS ?rc)
    WHERE {
    @@ -52,6 +72,7 @@ SELECT ?p (COUNT(?p) AS ?rc)
    FILTER(!isLiteral(?o) && ?p != rdf:type)
    }}
    GROUP BY ?p

    # Count triples whose subject belongs to :c1 and whose object is literal.
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    @@ -60,6 +81,7 @@ SELECT (COUNT(?s) AS ?rc)
    ?s a :c1 .
    FILTER(isLiteral(?o))
    }}

    # Enumerate all the predicates with their counts under the condition that their subject belongs to :c1 and their object is literal.
    SELECT ?p (COUNT(?p) AS ?rc)
    WHERE {
    @@ -68,4 +90,4 @@ SELECT ?p (COUNT(?p) AS ?rc)
    ?s a :c1 .
    FILTER(isLiteral(?o))
    }}
    GROUP BY ?p
    GROUP BY ?p
  2. yayamamo created this gist Aug 19, 2014.
    71 changes: 71 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    # Obtain the label of a given class (:class1).
    SELECT DISTINCT ?c (STR(?l) AS ?lb)
    WHERE {
    ?c a :class1 ;
    <http://www.w3.org/2000/01/rdf-schema#label> ?l .
    }
    # Obtain a list of classes.
    SELECT DISTINCT ?c
    WHERE {
    GRAPH :graph
    {[] a ?c .}
    }
    # Count instances belonging to a given class (:class1).
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    GRAPH :graph
    {?s a :class1 .}
    }
    # Count triples whose subject and object belong to :c1 and :c2, respectively.
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    ?o a :c2 .
    }}
    # Enumerate all the predicates with their counts under the condition that their subject and object belong to :c1 and :c2, respectively.
    SELECT ?p (COUNT(?p) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    ?o a :c2 .
    }}
    GROUP BY ?p
    # Count triples whose subject and object belong to :c1 and a locally-undeclared class, respectively.
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    MINUS {?o a ?oc}
    FILTER(!isLiteral(?o) && ?p != rdf:type)
    }}
    # Enumerate all the predicates with their counts under the condition that their subject and object belong to :c1 and a locally-undeclared class, respectively.
    SELECT ?p (COUNT(?p) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    MINUS {?o a ?oc}
    FILTER(!isLiteral(?o) && ?p != rdf:type)
    }}
    GROUP BY ?p
    # Count triples whose subject belongs to :c1 and whose object is literal.
    SELECT (COUNT(?s) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    FILTER(isLiteral(?o))
    }}
    # Enumerate all the predicates with their counts under the condition that their subject belongs to :c1 and their object is literal.
    SELECT ?p (COUNT(?p) AS ?rc)
    WHERE {
    GRAPH :graph {
    ?s ?p ?o .
    ?s a :c1 .
    FILTER(isLiteral(?o))
    }}
    GROUP BY ?p