forked from echen/restricted-boltzmann-machines
-
Notifications
You must be signed in to change notification settings - Fork 1
/
querier.py
43 lines (40 loc) · 1.36 KB
/
querier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from pymantic import sparql
class Querier():
"""Wrapper class for blazegraph-python"""
def __init__(self, url):
self.server = sparql.SPARQLServer(url)
self.prefixes = {}
def query(self, text):
finalQuery = ""
args = []
csv = ""
lines = text.split("\n")
for line in lines:
if "PREFIX" in line:
blocks = line.split(" ")
prefix = blocks[1]
uri = blocks[2].replace('<', '').replace('>', '')
self.prefixes[uri] = prefix
finalQuery = finalQuery + "\n" + line
if "SELECT" in line:
for word in line.split(' '):
if word[0] == '?':
args.append(word[1:])
for arg in args:
csv = csv + arg + ','
csv = csv[:len(csv)-1] + '\n'
print("Querying server")
result = self.server.query(text)
print("Done")
for b in result['results']['bindings']:
line = ""
for arg in args:
line = line + self.replacePrefix(b[arg]['value']) + ','
line = line[:len(line)-1] + '\n'
csv = csv + line
print csv
return csv
def replacePrefix(self, text):
for key in self.prefixes:
text = text.replace(key, self.prefixes[key])
return text