""" List all subclasses of Organization """ from rdflib import Graph def find_subclass(g, orgs): """ Finds subclasses in g that are listed in orgs Any found subclasses are added to orgs; the key is their identity, and the value is the identity of their parent. """ for s,p,o in g: if p.n3() == '': if o.n3() in orgs.keys(): orgs[s.n3()] = o.n3() if __name__ == '__main__': orgs = {'': 1} g = Graph() g.parse('http://schema.org/docs/schema_org_rdfa.html', format='rdfa1.1') # Dive three layers deep in the hierarchy find_subclass(g, orgs) find_subclass(g, orgs) find_subclass(g, orgs) print("Subclasses of Organization:\n") for x in sorted(orgs): print(x.strip('<>'))