# run this once per session to bring in a required library

!pip --quiet install sparqlwrapper | grep -v 'already satisfied'

from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd
import io
import requests

# This function shows how to use rdflib to query a REMOTE sparql dataset

q1 = """SELECT distinct ?prop ?type1 ?type2 WHERE  {
  ?type1 rdfs:subClassOf* <https://schema.org/Organization> . 
  ?type2 rdfs:subClassOf* <https://schema.org/Person> . 
  ?prop <https://schema.org/domainIncludes> ?type1 .
  ?prop <https://schema.org/domainIncludes> ?type2 .
}"""

pd.set_option('display.max_colwidth', None)

# data
wd_endpoint = 'https://query.wikidata.org/sparql'
sdo_endpoint = "https://dydra.com/danbri/schema-org-v11/sparql"

# utility function
def df_from_query(querystring=q1, endpoint=sdo_endpoint):
  sparql = SPARQLWrapper(endpoint)
  sparql.setQuery(querystring)
  sparql.setReturnFormat(JSON)
  results = sparql.query().convert()
  return( pd.json_normalize(results['results']['bindings']) )

# This shows how to use rdflib to query a LOCAL sparql dataset
# TODO: Need a function that loads https://webschemas.org/version/latest/schemaorg-current-https.nt into a named graph SPARQL store 


import rdflib
import json
from collections import Counter
from rdflib import Graph, plugin, ConjunctiveGraph
from rdflib.serializer import Serializer

def toDF(result):
  return pd.DataFrame(result, columns=result.vars)

# Fetch Schema.org definitions

sdo_current_https_url = "https://webschemas.org/version/latest/schemaorg-current-https.nq"
sdo_all_https_url = "https://webschemas.org/version/latest/schemaorg-all-https.nq"

# TODO - is this the only way to figure out what is in the attic? except both files use same NG URL
g = ConjunctiveGraph(store="IOMemory")
g.parse( sdo_all_https_url,    format="nquads",    publicID="https://schema.org/")
#g.parse( sdo_current_https_url,    format="nquads",    publicID="https://schema.org/")


result = toDF( g.query("select * where { GRAPH ?g { ?article_type rdfs:subClassOf <https://schema.org/NewsArticle> ; rdfs:label ?label }}") )


result

toDF( g.query("select * where { ?attic_term <https://schema.org/isPartOf> <https://attic.schema.org> ; rdfs:label ?label }") )

grandchild_count_query = """SELECT ?child (count(?grandchild) as ?nGrandchildren) where { ?child rdfs:subClassOf <https://schema.org/Thing> . OPTIONAL { ?grandchild rdfs:subClassOf ?child } } GROUP BY ?child order by desc(count(?grandchild))"""
res = g.query (grandchild_count_query)
mydf = toDF( res )
#mydf.plot(kind='bar')

mydf.columns

# https://www.shanelynn.ie/bar-plots-in-python-using-pandas-dataframes/

mydf['nGrandchildren']

print(mydf)
mydf['nGrandchildren'].plot(kind='bar')

result

x = df_from_query(q1)
x

"""
Explanation: <a href="https://colab.research.google.com/github/schemaorg/schemaorg/blob/main/scripts/Schema_org_Dashboard.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
This notebook is part of the Schema.org project codebase at https://github.com/schemaorg/schemaorg and licensed under the same terms. bold text
The purpose of this notebook is to show how to work programmatically with schema.org's definitions. 
See also https://colab.research.google.com/drive/1GVQaP5t8G-NRLAmEvVSp8k5MnsrfttDP for another approach to this, and this 2016 dashboard for some useful SPARQL queries to migrate here.
SPARQL
How to query schema.org schemas using SPARQL
End of explanation
"""


# First we clone the entire schema.org repo, then we collect up the examples from .txt files:

!git clone https://github.com/schemaorg/schemaorg

!find . -name \*example\*.txt -exec ls {} \;

"""
Explanation: Examples
How to access schema.org examples
End of explanation
"""


