This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
propertypath.py
634 lines (517 loc) · 19.5 KB
/
propertypath.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
from funcparserlib.parser import skip
from funcparserlib.parser import some
from funcparserlib.parser import forward_decl
from funcparserlib.parser import finished
from funcparserlib.parser import NoParseError
from funcparserlib.lexer import make_tokenizer
from funcparserlib.lexer import LexerError
import itertools
from collections import defaultdict
from .utils import to_p
from .utils import to_q
from .sparqlwikidata import sparql_wikidata
from .subfields import subfield_factory
from .wikidatavalue import WikidataValue, ItemValue, IdentifierValue
from config import wdt_prefix
from config import redis_key_prefix
from config import sparql_query_to_fetch_unique_id_properties
from .language import language_fallback
property_lexer_specs = [
('DOT', (r'\.',)),
('PID', (r'P\d+',)),
('TERM', (r'[LDA][a-z\-]+',)),
('SITELINK', (r'S[a-z\-]+',)),
('QID', (r'qid',)),
('SLASH', (r'/',)),
('PIPE', (r'\|',)),
('LBRA', (r'\(',)),
('RBRA', (r'\)',)),
('UNDER', (r'_',)),
('AT', (r'@',)),
('SUBFIELD', (r'[a-z]+',)),
]
tokenize_property = make_tokenizer(property_lexer_specs)
def t(code):
return some(lambda x: x.type == code)
def st(code):
return skip(t(code))
class PropertyFactory(object):
"""
A class to build property paths
"""
def __init__(self, item_store):
self.item_store = item_store
self.r = self.item_store.r # redis client
self.unique_ids_key = redis_key_prefix+'unique_ids'
self.ttl = 1*24*60*60 # 1 day
self.parser = forward_decl()
atomic = forward_decl()
atomic_subfield = forward_decl()
concat_path = forward_decl()
pipe_path = forward_decl()
atomic.define(
(t('PID') + st('UNDER') + t('PID') >> self.make_qualifier) |
(t('PID') >> self.make_leaf) |
(t('QID') >> self.make_qid) |
(t('TERM') >> self.make_term) |
(t('SITELINK') >> self.make_sitelink) |
(t('DOT') >> self.make_empty) |
(st('LBRA') + pipe_path + st('RBRA'))
)
atomic_subfield.define(
(atomic + st('AT') + t('SUBFIELD') >> self.make_subfield) |
atomic
)
concat_path.define(
((atomic_subfield + st('SLASH') + concat_path) >> self.make_slash) |
atomic_subfield
)
pipe_path.define(
((concat_path + st('PIPE') + pipe_path) >> self.make_pipe) |
concat_path
)
self.parser.define(
(
pipe_path
) + finished >> (lambda x: x[0])
)
def make_identity(self, a):
return a
def make_empty(self, dot=None):
return EmptyPropertyPath(self)
def make_leaf(self, pid):
return LeafProperty(self, pid.value)
def make_qid(self, node):
return QidProperty(self)
def make_qualifier(self, pids):
return QualifierProperty(self, pids[0].value, pids[1].value)
def make_term(self, term):
return TermPath(self, term.value[0], term.value[1:])
def make_sitelink(self, sitelink):
return SitelinkPath(self, sitelink.value[1:])
def make_slash(self, lst):
return ConcatenatedPropertyPath(self, lst[0], lst[1])
def make_pipe(self, lst):
return DisjunctedPropertyPath(self, lst[0], lst[1])
def make_subfield(self, lst):
return SubfieldPropertyPath(self, lst[0], lst[1].value)
def parse(self, property_path_string):
"""
Parses a string representing a property path
"""
try:
tokens = list(tokenize_property(property_path_string))
return self.parser.parse(tokens)
except (LexerError, NoParseError) as e:
raise ValueError("Could not parse '{}': {}".format(property_path_string, str(e)))
async def is_identifier_pid(self, pid):
"""
Does this PID represent a unique identifier?
"""
await self.prefetch_unique_ids()
return await self.r.sismember(self.unique_ids_key, pid)
async def prefetch_unique_ids(self):
"""
Prefetches the list of properties that correspond to unique
identifiers
"""
if await self.r.exists(self.unique_ids_key):
return # this list was already fetched
for pid in await self._fetch_unique_ids():
if pid:
await self.r.sadd(self.unique_ids_key, pid)
await self.r.expire(self.unique_ids_key, self.ttl)
async def _fetch_unique_ids(self):
# Q19847637 is "Wikidata property representing a unique
# identifier"
# https://www.wikidata.org/wiki/Q19847637
results = await sparql_wikidata(self.item_store.http_session, sparql_query_to_fetch_unique_id_properties)
return [to_p(result['pid']['value']) for result in results['bindings']]
class PropertyPath(object):
"""
A class representing a SPARQL-like
property path. At the moment it only
supports the "/" and "|" operators.
"""
def __init__(self, factory):
"""
Initializes the property path and
binds it to a given itemstore, for
later evaluation
"""
self.factory = factory
self.item_store = factory.item_store
async def get_item(self, item):
"""
Helper coercing an ItemValue to
the dict representing the item.
"""
if not item.value_type == "wikibase-item":
raise ValueError("get_item expects an ItemValue")
return await self.item_store.get_item(item.id)
async def evaluate(self, item_value, lang=None, fetch_labels=True):
"""
Evaluates the property path on the
given item, and returning strings (either qids or labels).
:param lang: the language to use, if any labels are fetched
:param fetch_labels: should we returns items or labels?
"""
async def fetch_label(v):
if v.value_type != "wikibase-item":
return [v.as_string()]
item = await self.get_item(v)
if not lang:
# return all labels and aliases
labels = list(item.get('labels', {}).values())
aliases = item.get('aliases', [])
return labels+aliases
else:
labels = item.get('labels', {})
return [language_fallback(labels, lang)]
values = await self.step(item_value)
if fetch_labels:
new_values = []
for v in values:
new_values += await fetch_label(v)
values = new_values
else:
values = [
val.json.get('id')
for val in values
]
return list(values)
async def step(self, v, referenced='any', rank='best'):
"""
Evaluates the property path on the
given value (most likely an item).
Returns a list of other values.
This is the method that should be
reimplemented by subclasses.
:param references: either 'any', 'referenced' or 'nonwiki'
to filter which statements should be considered (all statements,
only the ones with references, or only the ones with references
to sources outside wikis)
:param rank: the ranks of the statements to consider: 'any', 'best',
or 'no_deprecated'
"""
raise NotImplementedError()
async def is_unique_identifier(self):
"""
Given a path, does this path represent a unique identifier
for the item it starts from?
This only happens when the path is a disjunction of single
properties which are all unique identifiers.
This is async because we might need to fetch the set of unique
identifiers from the Wikibase instance.
"""
try:
return await self.uniform_depth() == 1
except ValueError: # the depth of the path is not uniform
return False
async def uniform_depth(self):
"""
The uniform depth of a path, if it exists, is the
number of steps from the item to the target, in
any disjunction.
Moreover, all the properties involved in the path
have to be unique identifiers.
If any of these properties is not satisfied, ValueError is
raised.
"""
raise NotImplementedError()
async def fetch_qids_by_values(self, values, lang):
"""
Fetches all the Qids and their labels in the selected language,
which bear any of the given values along this property.
The results are capped to four times the number of given
values, as it is expected that the relevant property has
a uniqueness constraint, so instances should be mostly unique.
"""
values_str = ' '.join('"%s"' % v
for v in values )
limit = 4*len(values)
sparql_query = """
SELECT ?qid ?value
(SAMPLE(COALESCE(?best_label, ?fallback_label)) as ?label)
WHERE {
?qid %s ?value.
VALUES ?value { %s }
OPTIONAL {
?qid rdfs:label ?best_label .
FILTER(LANG(?best_label) = "%s")
}
OPTIONAL { ?qid rdfs:label ?fallback_label }
}
GROUP BY ?qid ?value
LIMIT %d
""" % (
self.__str__(add_prefix=True),
values_str,
lang,
limit)
results = await sparql_wikidata(self.item_store.http_session, sparql_query)
value_to_qid = defaultdict(list)
for results in results['bindings']:
qid = to_q(results['qid']['value'])
label = (results.get('label') or {}).get('value') or qid
primary_id = results['value']['value']
value_to_qid[primary_id].append((qid,label))
return value_to_qid
async def expected_types(self):
"""
Returns a list of possible types expected
as values of this property.
"""
return []
async def readable_name(self, lang):
"""
Returns a readable name of the property in the given
language. By default it is just the string representation.
"""
return self.__str__()
def __hash__(self):
return hash(str(self))
def __eq__(self, other):
return str(self) == str(other)
class EmptyPropertyPath(PropertyPath):
"""
An empty path
"""
async def step(self, v, referenced='any', rank='any'):
return [v]
def __str__(self, add_prefix=False):
return '.'
async def uniform_depth(self):
return 0
async def expected_types(self):
return []
class QualifierProperty(PropertyPath):
"""
Fetches the value of a qualifier of a given claim, like "P31_P642".
"""
def __init__(self, factory, pid_property, pid_qualifier):
super(QualifierProperty, self).__init__(factory)
self.property_pid = pid_property
self.qualifier_pid = pid_qualifier
async def step(self, v, referenced='any', rank='any'):
if v.value_type != 'wikibase-item':
return []
item = await self.get_item(v)
datavalues = []
claims = item.get(self.property_pid, [])
if rank == 'best':
ranks = [claim['rank'] for claim in claims]
best_rank = max(ranks) if ranks else 'deprecated'
rank = best_rank
result = []
for claim in claims:
if claim['rank'] < rank:
continue
references = claim.get('references', [])
if referenced == 'internal' and not references:
continue
for qualifier in (claim.get('qualifiers') or {}).get(self.qualifier_pid) or []:
v = WikidataValue.from_datavalue(qualifier)
result.append(v)
return result
def __str__(self, add_prefix=False):
prefix = wdt_prefix if add_prefix else ''
return prefix+self.property_pid+'_'+self.qualifier_pid
async def uniform_depth(self):
raise ValueError('One property is not an identifier')
async def expected_types(self):
"""
Retrieve the expected type from Wikibase
"""
# TODO
return []
async def readable_name(self, lang):
return await self.item_store.get_label(self.property_pid, lang)+', '+self.item_store.get_label(self.qualifier_pid, lang)
class LeafProperty(PropertyPath):
"""
A node for a leaf, just a simple property like "P31"
"""
def __init__(self, factory, pid):
super(LeafProperty, self).__init__(factory)
self.pid = pid
async def step(self, v, referenced='any', rank='any'):
if v.value_type != 'wikibase-item':
return []
item = await self.get_item(v)
datavalues = []
claims = item.get(self.pid, [])
if rank == 'best':
ranks = [claim['rank'] for claim in claims]
best_rank = max(ranks) if ranks else 'deprecated'
rank = best_rank
result = []
for claim in claims:
if claim['rank'] < rank:
continue
references = claim.get('references', [])
if referenced == 'internal' and not references:
continue
# TODO handle nowiki case
mainsnak = claim.get('mainsnak')
if not mainsnak:
continue
v = WikidataValue.from_datavalue(mainsnak)
result.append(v)
return result
def __str__(self, add_prefix=False):
prefix = wdt_prefix if add_prefix else ''
return prefix+self.pid
async def uniform_depth(self):
if not await self.factory.is_identifier_pid(self.pid):
raise ValueError('One property is not an identifier')
return 1
async def expected_types(self):
"""
Retrieve the expected type from Wikibase
"""
# TODO
return []
async def readable_name(self, lang):
return await self.item_store.get_label(self.pid, lang)
class QidProperty(PropertyPath):
"""
A node to extract the Qid of an item.
"""
def __init__(self, factory):
super(QidProperty, self).__init__(factory)
async def step(self, v, referenced='any', rank='any'):
if v.value_type != 'wikibase-item':
return []
return [IdentifierValue(value=v.id)]
def __str__(self, add_prefix=False):
return 'qid'
async def uniform_depth(self):
return 1
async def expected_type(self):
return []
async def readable_name(self, lang):
# We could potentially look up some 'Qid' item to get translations here…
return 'Qid'
class TermPath(PropertyPath):
"""
A node for accessing the terms (label, description and aliases) of an item
"""
def __init__(self, factory, term_type, lang):
super(TermPath, self).__init__(factory)
self.term_type = term_type
self.lang = lang
async def step(self, v, referenced='any', rank='any'):
if v.value_type != 'wikibase-item':
return []
item = await self.get_item(v)
result = []
if self.term_type == 'L':
dct = item.get('labels') or {}
if self.lang in dct:
result.append(IdentifierValue(value=dct[self.lang]))
elif self.term_type == 'D':
dct = item.get('descriptions') or {}
if self.lang in dct:
result.append(IdentifierValue(value=dct[self.lang]))
elif self.term_type == 'A':
dct = item.get('full_aliases') or {}
for alias in dct.get(self.lang) or []:
result.append(IdentifierValue(value=alias))
return result
def __str__(self, add_prefix=False):
return self.term_type + self.lang
async def uniform_depth(self):
raise ValueError('One property is not an identifier')
async def expected_types(self):
"""
Retrieve the expected type from Wikidata
"""
return []
async def readable_name(self, lang):
return self.term_type + self.lang
class SitelinkPath(PropertyPath):
"""
A node for accessing the sitelinks of an item
"""
def __init__(self, factory, site):
super(SitelinkPath, self).__init__(factory)
self.site = site
async def step(self, v, referenced='any', rank='any'):
if v.value_type != 'wikibase-item':
return []
item = await self.get_item(v)
if not item:
return []
sitelink = (item.get('sitelinks') or {}).get(self.site)
if sitelink:
return [IdentifierValue(value=sitelink)]
def __str__(self, add_prefix=False):
return 'S'+self.site
async def uniform_depth(self):
raise ValueError('One property is not an identifier')
async def expected_types(self):
return []
async def readable_name(self, lang):
return 'Sitelink ' + self.site
class ConcatenatedPropertyPath(PropertyPath):
"""
Executes two property paths one after
the other: this is the / operator
"""
def __init__(self, factory, a, b):
super(ConcatenatedPropertyPath, self).__init__(factory)
self.a = a
self.b = b
async def step(self, v, referenced='any', rank='any'):
intermediate_values = await self.a.step(v, referenced, rank)
final_values = [
(await self.b.step(v2, referenced, rank))
for v2 in intermediate_values
]
return itertools.chain(*final_values)
def __str__(self, add_prefix=False):
return self.a.__str__(add_prefix) + '/' + self.b.__str__(add_prefix)
async def uniform_depth(self):
return await self.a.uniform_depth() + await self.b.uniform_depth()
async def expected_types(self):
return await self.b.expected_types()
class DisjunctedPropertyPath(PropertyPath):
"""
A disjunction of two property paths
"""
def __init__(self, factory, a, b):
super(DisjunctedPropertyPath, self).__init__(factory)
self.a = a
self.b = b
async def step(self, v, referenced='any', rank='any'):
va = await self.a.step(v, referenced, rank)
vb = await self.b.step(v, referenced, rank)
return itertools.chain(*[va,vb])
def __str__(self, add_prefix=False):
return '('+self.a.__str__(add_prefix) + '|' + self.b.__str__(add_prefix)+')'
async def uniform_depth(self):
depth_a = await self.a.uniform_depth()
depth_b = await self.b.uniform_depth()
if depth_a != depth_b:
raise ValueError('The depth is not uniform.')
return depth_a
async def expected_types(self):
return (await self.a.expected_types() + await self.b.expected_types())
class SubfieldPropertyPath(PropertyPath):
"""
A property path that returns a subfield of another property path
"""
def __init__(self, factory, path, subfield):
super(SubfieldPropertyPath, self).__init__(factory)
self.path = path
self.subfield = subfield
async def step(self, v, referenced='any', rank='any'):
orig_values = list(await self.path.step(v, referenced, rank))
images_values = list(map(lambda val: subfield_factory.run(self.subfield, val), orig_values))
return (val for val in images_values if val is not None)
async def uniform_depth(self):
raise ValueError('One property bears a subfield')
async def expected_types(self):
return []
def __str__(self, add_prefix=False):
return self.path.__str__(add_prefix) + '@' + self.subfield