def __init__(self):
     Markdown.setPre("http://schema.org/")
     self.typesCount = self.propsCount = self.namedCount = 0
     self.openFile()
     self.createDom()
     self.setSkips()
     self.getGraphs()
     self.loadGraphs()
     self.cleanup()
     self.closeFile()
Пример #2
0
 def __init__(self):
     Markdown.setPre("http://schema.org/")
     self.typesCount = self.propsCount = self.namedCount = 0
     self.openFile()
     self.createDom()
     self.setSkips()
     self.getGraphs()
     self.loadGraphs()
     self.cleanup()
     self.closeFile()
Пример #3
0
 def getCSVComment(self, term=None, graph=None):
     query = '''select ?com where{
      <%s> rdfs:comment ?com.
     }''' % term
     res = self.doQuery(graph, query)
     ret = ', '.join([x.com for x in res])
     #print "SUBTYPES of %s: '%s'" % (term,ret)
     if self.markdown:
         Markdown.setPre("http://schema.org/")
         ret = Markdown.parse(ret)
         Markdown.setPre()
     return ret
Пример #4
0
 def getCSVComment(self,term=None,graph=None):
     query='''select ?com where{
      <%s> rdfs:comment ?com.
     }''' % term
     res = self.doQuery(graph,query)
     ret = ', '.join([x.com for x in res])
     #print "SUBTYPES of %s: '%s'" % (term,ret)
     if self.markdown:
         Markdown.setPre(api.SdoConfig.vocabUri())
         ret = Markdown.parse(ret)
         Markdown.setPre()
     return ret
Пример #5
0
 def getCSVComment(self, term=None, graph=None):
     query = '''select ?com where{
      <%s> rdfs:comment ?com.
     }''' % term
     res = self.doQuery(graph, query)
     ret = ', '.join([x.com for x in res])
     #print "SUBTYPES of %s: '%s'" % (term,ret)
     if self.markdown:
         Markdown.setPre(api.SdoConfig.vocabUri())
         ret = Markdown.parse(ret)
         Markdown.setPre()
     return ret.replace('\n', '')
Пример #6
0
 def MdComments(self,g):#Process Markdown
     Markdown.setPre("http://schema.org/")
     for s,p,o in list(g.triples( (None, RDFS.comment, None) )):
         no = Markdown.parse(o)        #g.remove((s,p,o))
         g.set((s,p,Literal(no)))
     Markdown.setPre()
Пример #7
0
def buildSingleTermGraph(node, excludeAttic=True, markdown=True):

    g = rdflib.Graph()
    g.bind('owl', 'http://www.w3.org/2002/07/owl#')
    g.bind('rdfa', 'http://www.w3.org/ns/rdfa#')
    g.bind('dct', 'http://purl.org/dc/terms/')
    g.bind('schema', 'http://schema.org/')

    full = "http://schema.org/" + node
    #n = URIRef(full)
    n = SCHEMA.term(node)
    n = n
    full = str(n)
    q = queryGraph()
    ret = None

    #log.info("NAME %s %s"% (n,full))
    atts = None
    try:
        RDFLIBLOCK.acquire()
        atts = list(
            q.triples((n, SCHEMA.isPartOf, URIRef("http://attic.schema.org"))))
    finally:
        RDFLIBLOCK.release()
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False
    #Outgoing triples
    try:
        RDFLIBLOCK.acquire()
        ret = list(q.triples((n, None, None)))
    finally:
        RDFLIBLOCK.release()
    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

    #Incoming triples
    ret = list(q.triples((None, None, n)))
    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

        #super classes
        query = '''select * where {
	?term (^rdfs:subClassOf*) <%s>.
	?term rdfs:subClassOf ?super.
	?super ?pred ?obj.
	}''' % n

        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term, RDFS.subClassOf, row.super))
        g.add((row.super, row.pred, row.obj))

        #poperties with superclasses in domain
        query = '''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
	?prop ?pred ?obj.
	}
	''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        g.add((row.prop, SCHEMA.domainIncludes, row.term))
        g.add((row.prop, row.pred, row.obj))

        #super properties
        query = '''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
	?super ?pred ?obj.
	}''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term, RDFS.subPropertyOf, row.super))
        g.add((row.super, row.pred, row.obj))

        #Enumeration for an enumeration value
        query = '''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type, row.pred, row.obj))

    if excludeAttic:  #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None, None, None)))
        try:
            RDFLIBLOCK.acquire()
            for (s, p, o) in trips:
                atts = list(
                    q.triples((s, SCHEMA.isPartOf,
                               URIRef("http://attic.schema.org"))))
                if isinstance(o, URIRef):
                    atts.extend(
                        q.triples((o, SCHEMA.isPartOf,
                                   URIRef("http://attic.schema.org"))))
                for (rs, rp, ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs, None, None))
                    g.remove((None, None, rs))
        finally:
            RDFLIBLOCK.release()
    if markdown:
        try:
            RDFLIBLOCK.acquire()
            trips = list(g.triples((None, RDFS.comment, None)))
            Markdown.setPre("http://schema.org/")
            for (s, p, com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s, p, com))
                g.add((s, p, Literal(mcom)))
        finally:
            RDFLIBLOCK.release()
            Markdown.setPre()
    return g
Пример #8
0
def buildSingleTermGraph(node,excludeAttic=True,markdown=True):
    
    g = rdflib.Graph()
    g.bind('owl', 'http://www.w3.org/2002/07/owl#')
    g.bind('rdfa', 'http://www.w3.org/ns/rdfa#')
    g.bind('dct', 'http://purl.org/dc/terms/')
    g.bind('schema', 'http://schema.org/')
    
    full = "http://schema.org/" + node
    #n = URIRef(full)
    n = SCHEMA.term(node)
    n = n
    full = str(n)
    q = queryGraph()
    ret = None
    
    #log.info("NAME %s %s"% (n,full))
    atts = None
    try:
        RDFLIBLOCK.acquire()
        atts = list(q.triples((n,SCHEMA.isPartOf,URIRef("http://attic.schema.org"))))
    finally:
        RDFLIBLOCK.release()    
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False
    #Outgoing triples
    try:
        RDFLIBLOCK.acquire()
        ret = list(q.triples((n,None,None)))
    finally:
        RDFLIBLOCK.release()
    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #Incoming triples
    ret = list(q.triples((None,None,n)))
    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #super classes
	query='''select * where {
	?term (^rdfs:subClassOf*) <%s>.
	?term rdfs:subClassOf ?super.
	?super ?pred ?obj.
	}''' % n

	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term,RDFS.subClassOf,row.super))
        g.add((row.super,row.pred,row.obj))
         
    #poperties with superclasses in domain
	query='''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
	?prop ?pred ?obj.
	}
	''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        g.add((row.prop,SCHEMA.domainIncludes,row.term))
        g.add((row.prop,row.pred,row.obj))

    #super properties
	query='''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
	?super ?pred ?obj.
	}''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term,RDFS.subPropertyOf,row.super))
        g.add((row.super,row.pred,row.obj))

    #Enumeration for an enumeration value
	query='''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type,row.pred,row.obj))

    if excludeAttic: #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None,None,None)))
        try:
            RDFLIBLOCK.acquire()
            for (s,p,o) in trips:
                atts = list(q.triples((s,SCHEMA.isPartOf,URIRef("http://attic.schema.org"))))
                if isinstance(o, URIRef):
                    atts.extend(q.triples((o,SCHEMA.isPartOf,URIRef("http://attic.schema.org"))))
                for (rs,rp,ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs,None,None))
                    g.remove((None,None,rs))
        finally:
            RDFLIBLOCK.release()
    if markdown:
        try:
            RDFLIBLOCK.acquire()
            trips = list(g.triples((None,RDFS.comment,None)))
            Markdown.setPre("http://schema.org/")
            for (s,p,com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s,p,com))
                g.add((s,p,Literal(mcom)))
        finally:
            RDFLIBLOCK.release()
            Markdown.setPre()
    return g
Пример #9
0
def buildSingleTermGraph(node, excludeAttic=True, markdown=True):

    q = queryGraph()
    g = rdflib.Graph()
    ns = q.namespaces()
    for n in ns:
        prefix, path = n
        namespaceAdd(g, prefix=prefix, path=path)
    namespaceAdd(g, api.SdoConfig.prefix(), api.SdoConfig.vocabUri())

    full = "%s%s" % (api.SdoConfig.vocabUri(), node)
    n = URIRef(full)
    full = str(n)
    ret = None

    #log.info("NAME %s %s"% (n,full))
    atts = None
    attic = api.SdoConfig.atticUri()
    if attic:
        with RDFLIBLOCK:
            atts = list(q.triples((n, SCHEMA.isPartOf, URIRef(attic))))
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False

    #Outgoing triples
    with RDFLIBLOCK:
        ret = list(q.triples((n, None, None)))

    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

    #Incoming triples
    with RDFLIBLOCK:
        ret = list(q.triples((None, None, n)))
    for (s, p, o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s, p, o))

    #super classes
    query = '''select * where {
    ?term (^rdfs:subClassOf*) <%s>.
    ?term rdfs:subClassOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n, api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)

    ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term, RDFS.subClassOf, row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super, row.pred, row.obj))

    #poperties with superclasses in domain
        query = '''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
        OPTIONAL {
        	?prop ?pred ?obj.
            FILTER (strstarts(str(?prop),'%s'))
        }
	}
    ''' % (n, api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query, q)
    for row in ret:
        g.add((row.prop, SCHEMA.domainIncludes, row.term))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.prop, row.pred, row.obj))

    #super properties
        query = '''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n, api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term, RDFS.subPropertyOf, row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super, row.pred, row.obj))

    #Enumeration for an enumeration value
        query = '''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
        ret = rdfQueryStore(query, q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type, row.pred, row.obj))

    if excludeAttic:  #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None, None, None)))
        with RDFLIBLOCK:
            for (s, p, o) in trips:
                atts = list(q.triples((s, SCHEMA.isPartOf, URIRef(attic))))
                if isinstance(o, URIRef):
                    atts.extend(q.triples((o, SCHEMA.isPartOf, URIRef(attic))))
                for (rs, rp, ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs, None, None))
                    g.remove((None, None, rs))
    if markdown:
        with RDFLIBLOCK:
            trips = list(g.triples((None, RDFS.comment, None)))
            Markdown.setPre(api.SdoConfig.vocabUri())
            for (s, p, com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s, p, com))
                g.add((s, p, Literal(mcom)))
        Markdown.setPre()
    return g
Пример #10
0
def buildSingleTermGraph(node,excludeAttic=True,markdown=True):
    
    q = queryGraph()
    g = rdflib.Graph()
    ns = q.namespaces()
    for n in ns:
        prefix, path = n
        namespaceAdd(g,prefix=prefix,path=path)
    namespaceAdd(g,api.SdoConfig.prefix(),api.SdoConfig.vocabUri())
    
    full = "%s%s" % (api.SdoConfig.vocabUri(), node)
    n = URIRef(full)
    full = str(n)
    ret = None
    
    #log.info("NAME %s %s"% (n,full))
    atts = None
    attic = api.SdoConfig.atticUri()
    if attic:
        with RDFLIBLOCK:
            atts = list(q.triples((n,SCHEMA.isPartOf,URIRef(attic))))
    if len(atts):
        #log.info("ATTIC TERM %s" % n)
        excludeAttic = False

    #Outgoing triples
    with RDFLIBLOCK:
        ret = list(q.triples((n,None,None)))

    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #Incoming triples
    with RDFLIBLOCK:
        ret = list(q.triples((None,None,n)))
    for (s,p,o) in ret:
        #log.info("adding %s %s %s" % (s,p,o))
        g.add((s,p,o))

    #super classes
    query='''select * where {
    ?term (^rdfs:subClassOf*) <%s>.
    ?term rdfs:subClassOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n,api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)

    ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subClassOf,row.super))
        g.add((row.term,RDFS.subClassOf,row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super,row.pred,row.obj))
         
    #poperties with superclasses in domain
	query='''select * where{
	?term (^rdfs:subClassOf*) <%s>.
	?prop <http://schema.org/domainIncludes> ?term.
        OPTIONAL {
        	?prop ?pred ?obj.
            FILTER (strstarts(str(?prop),'%s'))
        }
	}
    ''' % (n,api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query,q)
    for row in ret:
        g.add((row.prop,SCHEMA.domainIncludes,row.term))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.prop,row.pred,row.obj))

    #super properties
	query='''select * where {
	?term (^rdfs:subPropertyOf*) <%s>.
	?term rdfs:subPropertyOf ?super.
        OPTIONAL {
        	?super ?pred ?obj.
            FILTER (strstarts(str(?super),'%s'))
        }
    }
    ''' % (n,api.SdoConfig.vocabUri())
    #log.info("Query: %s" % query)
    ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.term,RDFS.subPropertyOf,row.super))
        g.add((row.term,RDFS.subPropertyOf,row.super))
        pred = row.pred
        obj = row.obj
        if pred and obj:
            g.add((row.super,row.pred,row.obj))

    #Enumeration for an enumeration value
	query='''select * where {
	<%s> a ?type.
	?type ?pred ?obj.
	FILTER NOT EXISTS{?type a rdfs:class}.
	}''' % n
	ret = rdfQueryStore(query,q)
    for row in ret:
        #log.info("adding %s %s %s" % (row.type,row.pred,row.obj))
        g.add((row.type,row.pred,row.obj))

    if excludeAttic: #Remove triples referencing terms part of http://attic.schema.org
        trips = list(g.triples((None,None,None)))
        with RDFLIBLOCK:
            for (s,p,o) in trips:
                atts = list(q.triples((s,SCHEMA.isPartOf,URIRef(attic))))
                if isinstance(o, URIRef):
                    atts.extend(q.triples((o,SCHEMA.isPartOf,URIRef(attic))))
                for (rs,rp,ro) in atts:
                    #log.info("Removing %s" % rs)
                    g.remove((rs,None,None))
                    g.remove((None,None,rs))
    if markdown:
        with RDFLIBLOCK:
            trips = list(g.triples((None,RDFS.comment,None)))
            Markdown.setPre(api.SdoConfig.vocabUri())
            for (s,p,com) in trips:
                mcom = Markdown.parse(com)
                g.remove((s,p,com))
                g.add((s,p,Literal(mcom)))
        Markdown.setPre()
    return g