Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.2 kB
3
Indexable
Never
def getallfrombabel(linktordf, broader = True, narrower = True, category = True, match=True):  
    g2 = Graph()
    #print(linktordf)
    linktordf = linktordf.replace("/rdf/", "/rdf/data/") + "?output=ttl"
    #print(linktordf)
    try:
        g2.parse(linktordf, format="ttl")
    except:
        return {}
    normal = linktordf.split("data/")[1]
    normal = normal.split("?")[0]
    normal = "bn:" + normal

    querycate = '''
  prefix skos:  <http://www.w3.org/2004/02/skos/core#>
  prefix bn: <http://babelnet.org/rdf/>
  prefix bn-lemon: <http://babelnet.org/model/babelnet#>
  select distinct ?category where {
  ''' + normal + ''' bn-lemon:dbpediaCategory ?category;
  }'''

    querybroad = '''
  prefix skos:  <http://www.w3.org/2004/02/skos/core#>
  prefix bn: <http://babelnet.org/rdf/>
  prefix bn-lemon: <http://babelnet.org/model/babelnet#>
  select distinct ?broader where {
  ''' + normal + ''' skos:broader ?broader
  }'''

    querynar = '''
  prefix skos:  <http://www.w3.org/2004/02/skos/core#>
  prefix bn: <http://babelnet.org/rdf/>
  prefix bn-lemon: <http://babelnet.org/model/babelnet#>
  select distinct ?narrower where {
  ''' + normal + ''' skos:narrower ?narrower
  }'''

    querymatch = '''
  prefix skos:  <http://www.w3.org/2004/02/skos/core#>
  prefix bn: <http://babelnet.org/rdf/>
  prefix bn-lemon: <http://babelnet.org/model/babelnet#>
  select distinct ?match where {
  ''' + normal + ''' skos:exactMatch ?match
  }'''
    result = dict()
    if broader is True:
        result["broader"] = []
        r1 = g2.query(querybroad)
        for el in r1:
            result["broader"].append(el["broader"])
    if narrower is True:
        result["narrower"] = []
        r2 = g2.query(querynar)
        for el in r2:
            result["narrower"].append(el["narrower"])
    if category is True:
        result["category"] = []
        r3 = g2.query(querycate)
        for el in r3:
            result["category"].append(el["category"])
    if match is True:
        result["match"] = []
        r4 = g2.query(querymatch)
        for el in r4:
            result["match"].append(el["match"])
    return result