This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
megaWikibase.py
executable file
·238 lines (218 loc) · 11.6 KB
/
megaWikibase.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from RaiseWikibase.dbconnection import DBConnection
from RaiseWikibase.datamodel import label, alias, description, snak, claim, entity, namespaces, datatypes
from RaiseWikibase.utils import get_wikidata_properties
from RaiseWikibase.raiser import page, batch
from pathlib import Path
import requests
import time
import numpy as np
import csv
def property_wid(prop=''):
"""Create a simplified JSON represetation (only the label, aliases,
description and one claim) of the first local property 'Wikidata ID'"""
p = entity(labels=label(value='Wikidata ID'),
aliases=alias(value=["WID", 'WikidataID']),
descriptions=description(value="ID of an entity in Wikidata"),
claims=claim(prop='P2',
mainsnak=snak(datatype='string',
value='http://www.wikidata.org/entity/$1',
prop='P2',
snaktype='value')),
etype='property',
datatype='external-id')
p['claims'].update(claim(prop='P3',
mainsnak=snak(datatype='string',
value='http://www.wikidata.org/entity/$1',
prop='P3',
snaktype='value')))
return p
def property_wd(prop=''):
"""For the given PID of a property in Wikidata returns its
simplified JSON represetation (only the label, aliases, description
and one claim)"""
if prop:
r = requests.get('https://www.wikidata.org/entity/' + prop + '.json').json()
po = r.get('entities').get(prop)
p = entity(labels=po.get('labels'),
aliases=po.get('aliases'),
descriptions=po.get('descriptions'),
claims=claim(prop='P1',
mainsnak=snak(datatype='external-id',
value=prop,
prop='P1',
snaktype='value')),
etype='property',
datatype='string')
else:
p = None
return p
def property_hrn(prop=''):
"""Create a simplified JSON represetation (only the label, aliases,
description and one claim) of the first local property 'Native company number'"""
p = entity(labels=label(value='Native company number'),
aliases=alias(value=["Court, code and number", 'Register, type of register and company number',
'Registergericht, Registerart and Handelsregister number ']),
descriptions=description(
value="The registration authority (Amtsgericht), the registration code and the registration number"),
claims={},
etype='property',
datatype='external-id')
return p
def property_wd_all():
""" Send a SPARQL-request to Wikidata, get all properties with labels,
aliases, descriptions and datatypes, and return a list of JSON
representations of the properties"""
props = get_wikidata_properties(language='en')
props = props.replace(np.nan, '').replace("http://www.wikidata.org/entity/", "", regex=True)
props = props.groupby(['propertyWikidata', 'propertyType',
'propertyLabel', 'propertyDescription']
).agg(propertyAlias=('propertyAlias', lambda x: list(x)),
fURL=('fURL', lambda x: list(x)),
cURI=('cURI', lambda x: list(x)),
).reset_index()
props = props[props.propertyWikidata != ("P1630" or "P1921")]
props = props.sort_values(by=['propertyWikidata'],
key=lambda col: col.str.replace('P', '', regex=True).astype(float))
plist = []
for ind, row in props.iterrows():
url, dtype, title, desc, alia, furl, curi = row
p = entity(labels=label(value=title),
aliases=alias(value=alia),
descriptions=description(value=desc),
claims=claim(prop='P1',
mainsnak=snak(datatype='external-id',
value=url,
prop='P1',
snaktype='value')),
etype='property',
datatype=datatypes[dtype])
for f in furl:
if f != '':
p['claims'].update(claim(prop='P2',
mainsnak=snak(datatype='string',
value=f,
prop='P2',
snaktype='value')))
for c in curi:
if c != '':
p['claims'].update(claim(prop='P3',
mainsnak=snak(datatype='string',
value=c,
prop='P3',
snaktype='value')))
plist.append(p)
return plist
def fill_texts():
"""Fill all texts from the texts-folder"""
connection = DBConnection()
cmodelpath = "./texts/"
cmodels = ['wikitext', 'Scribunto']
for cmodel in cmodels:
p = Path(cmodelpath + cmodel)
folders = [x for x in p.iterdir() if x.is_dir()]
for folder in folders:
ns = namespaces[folder.name]
files = [x for x in folder.iterdir() if x.is_file()]
for file in files:
pt = file.name.replace(':', '/')
if pt.endswith('.css'):
cm = 'sanitized-css'
else:
cm = cmodel
text = file.read_text("utf-8")
print(ns, pt, cmodel)
page(connection, cm, ns, text, pt, True)
# Fill the Main page separately
pt = "Main_Page"
p = Path(cmodelpath + pt)
text = p.read_text("utf-8")
page(connection, 'wikitext', 0, text, pt, False)
connection.conn.commit()
connection.conn.close()
if __name__ == "__main__":
time0 = time.time()
# Create first four properties in a local Wikibase
p1 = property_wid()
p2 = property_wd('P1630')
p3 = property_wd('P1921')
p4 = property_hrn()
batch('wikibase-property', [p1, p2, p3, p4])
# Create 8400+ Wikidata properties except of the already created p2 and p3.
plist = property_wd_all()
batch('wikibase-property', plist)
# Fill all texts
fill_texts()
connection = DBConnection()
[opencorporatesid] = connection.search_text_str('OpenCorporates ID', strict=True)
[streetaddress] = connection.search_text_str('street address', strict=True)
[retrieved] = connection.search_text_str('retrieved', strict=True)
[handelnumber] = connection.search_text_str('Native company number', strict=True)
connection.conn.close()
# Process OpenCorporates dataset
# Download https://daten.offeneregister.de/openregister.db.gz
# Unzip and run in shell:
# sqlite3 -header -csv handelsregister.db "select * from company;" > millions_companies.csv
time1 = time.time()
items = []
path_to_ofreg = "./millions_companies.csv"
p = Path(path_to_ofreg)
if p.is_file():
with p.open('r') as f:
reader = csv.reader(f, delimiter=",")
for i, line in enumerate(reader):
if i > 0:
[orid, company_number, current_status, jurisdiction_code, name,
registered_address, retrieved_at, register_flag_AD, register_flag_CD,
register_flag_DK, register_flag_HD, register_flag_SI, register_flag_UT,
register_flag_VOE, federal_state, native_company_number, registered_office,
registrar, register_art, register_nummer, former_registrar, register_flag_,
register_flag_Note, registerNummerSuffix, register_flag_Status_information] = line
e = entity(labels=label(value=name),
aliases={},
descriptions={},
claims=claim(prop=opencorporatesid,
mainsnak=snak(datatype='external-id',
value='de/' + company_number,
prop=opencorporatesid,
snaktype='value'),
qualifiers=[snak("time", ['+' + retrieved_at, 0, 11,
'http://www.wikidata.org/entity/Q1985727'],
retrieved, 'value')],
references=[snak('external-id', 'de/' + company_number,
opencorporatesid, 'value')]),
etype='item')
if registered_address:
e['claims'].update(claim(prop=streetaddress,
mainsnak=snak(datatype='monolingualtext',
value=[registered_address, 'en'],
prop=streetaddress,
snaktype='value'),
qualifiers=[snak("time", ['+' + retrieved_at, 0, 11,
'http://www.wikidata.org/entity/Q1985727'],
retrieved, 'value')],
references=[snak('external-id', 'de/' + company_number,
opencorporatesid, 'value')]))
if native_company_number:
e['claims'].update(claim(prop=handelnumber,
mainsnak=snak(datatype='external-id',
value=native_company_number,
prop=handelnumber,
snaktype='value'),
qualifiers=[snak("time", ['+' + retrieved_at, 0, 11,
'http://www.wikidata.org/entity/Q1985727'],
retrieved, 'value')],
references=[snak('external-id', 'de/' + company_number,
opencorporatesid, 'value')]))
items.append(e)
del e
if i % 1000 == 0:
batch('wikibase-item', items)
print(time.time() - time1)
items = []
if i>=1001:
break
# to make the KG production-ready, execute the following as well
# or run in shell 'docker-compose down' and 'docker-compose up -d' again
# from RaiseWikibase.raiser import building_indexing
# building_indexing()
print(time.time() - time0)