forked from MiMoText/Wikibase-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikiSQL.py
More file actions
136 lines (122 loc) · 3.77 KB
/
wikiSQL.py
File metadata and controls
136 lines (122 loc) · 3.77 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import sys
import pprint
from enum import Enum
from SPARQLWrapper import SPARQLWrapper, JSON
class SparQL_Mode(Enum):
QID = '1'
PID = '2'
IDforStatement = '3'
label = '?label'
value = '?value'
ID_WithTarget = 4
ID_from_P30 = 5
ID_without_Property_and_instance_of_Item = 6
class SparQL_Properties(Enum):
instance_of = 'P2'
BGRF_ID = 'P13'
def prettyPrint(variable):
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(variable)
def GetEntryOverSPARQL(ENDPOINT, item, mode=SparQL_Mode.QID, prop="P13", lang='en', target=None, instanceOf = None):
'''
Get Entry with Label Name and Language
'''
item = item.replace("'", ".")
item = item.replace("’", ".")
item = item.replace("(", ".")
item = item.replace(")", ".")
item = item.replace("É", ".")
item = item.replace("È", ".")
item = item.replace("*", ".")
item = item.replace("]", ".")
item = item.replace("[", ".")
#SPARQL endpoint
endpoint_url = "http://zora.uni-trier.de:" + ENDPOINT + "/proxy/wdqs/bigdata/namespace/wdq/sparql"
#Dynamic Query
if mode == SparQL_Mode.QID:
query = """
SELECT *
{
?item rdfs:label ?label.
Filter not exists {?item rdf:type wikibase:Property}
Filter (regex(?label, '^""" + item + """$', 'i'))
Filter (lang(?label) = '""" + lang + """')
BIND (str(Replace(str(?item), '\\\\D+\\\\d+\\\\D+\\\\/', '')) as ?Result)
}
"""
elif mode == SparQL_Mode.PID:
query = """
SELECT *
{
?item rdfs:label ?label.
?item rdf:type wikibase:Property.
Filter (regex(?label, '^""" + item + """$', 'i'))
Filter (lang(?label) = '""" + lang + """')
BIND (str(Replace(str(?item), '\\\\D+\\\\d+\\\\D+\\\\/', '')) as ?Result)
}
"""
elif mode == SparQL_Mode.IDforStatement:
query = """
SELECT *
{
?item wdt:"""+prop+""" '"""+item+"""'.
?item rdfs:label ?label.
Filter (lang(?label) = 'en')
BIND (str(Replace(str(?item), "\\\\D+\\\\d+\\\\D+\\\\/", "")) as ?Result)
}
"""
elif mode == SparQL_Mode.value:
query ="""
select *
{
wd:""" + item + """ wdt:""" + prop + """ ?Result.
}
"""
elif mode == SparQL_Mode.ID_WithTarget:
query ="""
select *
{
?item rdfs:label ?label.
?item wdt:P2 wd:""" + target + """.
Filter (regex(?label, '^"""+ item +"""$'))
BIND (str(Replace(str(?item), "\\\\D+\\\\d+\\\\D+\\\\/", "")) as ?Result)
}
"""
elif mode == SparQL_Mode.ID_from_P30:
query="""
select *
{
?item wdt:P30|wdt:P58 ?label.
Filter (regex(str(?label), "^""" + item + """$", "i"))
BIND (str(Replace(str(?item), "\\\\D+\\\\d+\\\\D+\\\\/", "")) as ?Result)
}
"""
elif mode == SparQL_Mode.ID_without_Property_and_instance_of_Item:
query = """
SELECT *
{
?item rdfs:label ?label.
?item wdt:P2 wd:""" + instanceOf + """.
Filter not exists {?item rdf:type wikibase:Property}
Filter not exists {?item """ + prop + """ ?o}
Filter (regex(?label, '^""" + item + """$', 'i'))
Filter (lang(?label) = '""" + lang + """')
BIND (str(Replace(str(?item), '\\\\D+\\\\d+\\\\D+\\\\/', '')) as ?Result)
}
"""
#print(item)
results = get_results(endpoint_url, query)
#prettyPrint(results)
for result in results["results"]["bindings"]:
#print('ITEM: ' + result['item']['value'].split('/')[-1])
return result['Result']['value']
def get_results(endpoint_url, query):
'''
get SPARQL resdults
'''
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
# TODO adjust user agent; see https://w.wiki/CX6
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()