-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_language_statistics.py
302 lines (258 loc) · 10 KB
/
extract_language_statistics.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from argparse import ArgumentParser
import bz2
import gzip
import json
import sys
class WikidataItemDocument(object):
def __init__(self, json):
self.json = json
def get(self, field, default_value=None):
return self.json.get(field, default_value)
def __repr__(self):
return "<WikidataItemDocument {}>".format(
self.json.get("id") or "(unknown qid)"
)
def __iter__(self):
return self.json.__iter__()
def get_outgoing_edges(self, include_p31=True, numeric=True):
"""
Given a JSON representation of an item,
return the list of outgoing edges,
as integers.
"""
claims = self.get("claims", {})
final_key = "numeric-id" if numeric else "id"
res = []
for pid, pclaims in claims.items():
if pid == "P31" and not include_p31:
continue
for c in pclaims:
try:
res.append(c["mainsnak"]["datavalue"]["value"][final_key])
except (KeyError, TypeError):
pass
qualifiers = c.get("qualifiers", {})
for pid, qs in qualifiers.items():
for q in qs:
try:
res.append(q["datavalue"]["value"][final_key])
except (KeyError, TypeError):
pass
return res
def get_nb_statements(self):
"""
Number of claims on the item
"""
nb_claims = 0
for pclaims in self.get("claims", {}).values():
nb_claims += len(pclaims)
return nb_claims
def get_nb_sitelinks(self):
"""
Number of sitelinks on this item
"""
return len(self.get("sitelinks", []))
def get_types(self, pid="P31"):
"""
Values of P31 claims
"""
type_claims = self.get("claims", {}).get(pid, [])
type_qids = [
claim.get("mainsnak", {}).get("datavalue", {}).get("value", {}).get("id")
for claim in type_claims
]
valid_type_qids = [qid for qid in type_qids if qid]
return valid_type_qids
def get_default_label(self, language):
"""
English label if provided, otherwise any other label
"""
labels = self.get("labels", {})
preferred_label = labels.get(language, {}).get("value")
if preferred_label:
return preferred_label
enlabel = labels.get("en", {}).get("value")
if enlabel:
return enlabel
for other_lang in labels:
return labels.get(other_lang, {}).get("value")
return None
def get_all_terms(self):
"""
All labels and aliases in all languages, made unique
"""
all_labels = {label["value"] for label in self.get("labels", {}).values()}
for aliases in self.get("aliases", {}).values():
all_labels |= {alias["value"] for alias in aliases}
return all_labels
def get_aliases(self, lang):
aliases = [alias["value"] for alias in self.get("aliases", {}).get(lang, [])]
return aliases
def get_identifiers(self, pid):
# Fetch GRID
id_claims = self.get("claims", {}).get(pid, [])
ids = [
claim.get("mainsnak", {}).get("datavalue", {}).get("value", {})
for claim in id_claims
]
valid_ids = [id for id in ids if id]
return valid_ids
class WikidataDumpReader(object):
"""
Generates a stream of `WikidataItemDocument` from
a Wikidata dump.
"""
def __init__(self, fname: str):
self.fname = fname
if fname == "-":
self.f = sys.stdin
elif fname.endswith("bz2"):
self.f = bz2.open(fname, mode="rt", encoding="utf-8")
else:
self.f = gzip.open(fname, "rt")
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
if self.fname != "-":
self.f.close()
def __iter__(self):
for line in self.f:
try:
# remove the trailing comma
if line.rstrip().endswith(","):
line = line[:-2]
item = json.loads(line)
yield WikidataItemDocument(item)
except ValueError as e:
# Happens at the beginning or end of dumps with '[', ']'
continue
def to_triple_object(subject, predicate, object):
return f"<{subject}> <{predicate}> <{object}> .\n"
def to_triple_literal(subject, predicate, literal, lang):
return f'<{subject}> <{predicate}> "{literal}"@{lang} .\n'
def fill_data(data, item):
data["counter"] += 1
labels: dict = item.get("labels", {})
temp_languages = set()
all_labels = []
labels_overall = 0
tmp_labels_found = {}
for key, value in labels.items():
if key not in data["languages_labels"]:
data["languages_labels"][key] = 0
if value:
temp_languages.add(key)
data["languages_labels"][key] += 1
labels_overall += 1
all_labels.append(value["value"])
tmp_labels_found[key] = 1
aliases = item.get("aliases", {})
for key, value in aliases.items():
if key not in data["languages_aliases"]:
data["languages_aliases"][key] = 0
if value:
temp_languages.add(key)
num_aliases = len(value)
data["languages_aliases"][key] += num_aliases
labels_overall += num_aliases
all_labels += [value_["value"] for value_ in value]
if key not in tmp_labels_found:
tmp_labels_found[key] = num_aliases
else:
tmp_labels_found[key] += num_aliases
for key, value in tmp_labels_found.items():
if key not in data["labels_per_entity_per_language"]:
data["labels_per_entity_per_language"][key] = {}
if value not in data["labels_per_entity_per_language"][key]:
data["labels_per_entity_per_language"][key][value] = 0
data["labels_per_entity_per_language"][key][value] += 1
if labels_overall not in data["labels_per_entity"]:
data["labels_per_entity"][labels_overall] = 0
data["labels_per_entity"][labels_overall] += 1
if len(temp_languages) not in data["languages_per_entity_labels"]:
data["languages_per_entity_labels"][len(temp_languages)] = 0
data["languages_per_entity_labels"][len(temp_languages)] += 1
for language in temp_languages:
if language not in data["languages_any_sf"]:
data["languages_any_sf"][language] = 0
data["languages_any_sf"][language] += 1
claims: dict = item.get("claims", None)
if claims:
for value in claims.values():
for claim in value:
qualifiers = claim.get("qualifiers", {})
qualifiers_len = sum([len(content) for content in qualifiers.values()])
if qualifiers_len not in data["claims_counter"]:
data["claims_counter"][qualifiers_len] = 0
data["claims_counter"][qualifiers_len] += 1
data["overlap"]["average_number"] += len(all_labels) - len(set(all_labels))
data["overlap"]["average_percentage"] += (
len(set(all_labels)) / len(all_labels) if all_labels else 1
)
descriptions = item.get("descriptions", {})
for key, value in descriptions.items():
if key not in data["descriptions_per_language"]:
data["descriptions_per_language"][key] = 0
if value:
data["descriptions_per_language"][key] += 1
if len(descriptions) not in data["descriptions_per_entity"]:
data["descriptions_per_entity"][len(descriptions)] = 0
data["descriptions_per_entity"][len(descriptions)] += 1
if len(descriptions.keys()) not in data["languages_per_entity_description"]:
data["languages_per_entity_description"][len(descriptions.keys())] = 0
data["languages_per_entity_description"][len(descriptions.keys())] += 1
all_languages = set(descriptions.keys())
all_languages.update(temp_languages)
if len(all_languages) not in data["languages_per_entity"]:
data["languages_per_entity"][len(all_languages)] = 0
data["languages_per_entity"][len(all_languages)] += 1
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("filename", type=str)
args = parser.parse_args()
item_data = {
"languages_labels": {},
"languages_any_sf": {},
"languages_aliases": {},
"labels_per_entity_per_language": {},
"languages_per_entity": {},
"languages_per_entity_description": {},
"languages_per_entity_labels": {},
"claims_counter": {},
"labels_per_entity": {},
"counter": 0,
"overlap": {"average_number": 0, "average_percentage": 0},
"descriptions_per_language": {},
"descriptions_per_entity": {},
}
property_data = {
"languages_labels": {},
"languages_any_sf": {},
"languages_aliases": {},
"labels_per_entity_per_language": {},
"languages_per_entity": {},
"labels_per_entity": {},
"languages_per_entity_description": {},
"languages_per_entity_labels": {},
"claims_counter": {},
"counter": 0,
"overlap": {"average_number": 0, "average_percentage": 0},
"descriptions_per_language": {},
"descriptions_per_entity": {},
}
# Read through whole JSON entitiy by entity, filter and output to json file
counter = 0
with WikidataDumpReader(args.filename) as reader:
for item in reader:
qid = item.get("id")
if qid[0] == "Q":
fill_data(item_data, item)
elif qid[0] == "P":
fill_data(property_data, item)
else:
continue
counter += 1
if counter % 100000 == 0:
print(counter)
with open("language_statistics.json", "w") as f:
json.dump({"items": item_data, "properties": property_data}, f)