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
/
Copy pathwikidatavalue.py
475 lines (392 loc) · 12 KB
/
wikidatavalue.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
import dateutil.parser
from urllib.parse import urlparse, urlunparse
import math
from .sitelink import SitelinkFetcher
from .utils import to_q, fuzzy_match_strings, match_ints, match_floats
wdvalue_mapping = {}
def register(cls):
wdvalue_mapping[cls.value_type] = cls
return cls
# TODO treat somevalue and novalue differently
class WikidataValue(object):
"""
This class represents any target value of a Wikidata claim.
"""
value_type = None
def __init__(self, **json):
self.json = json
def match_with_str(self, s, item_store):
"""
Given a string s (the target reconciliation value),
return a matching score with the WikidataValue.
An ItemStore is provided to fetch information
about items if needed.
Scores should be returned between 0 and 100
:param s: the string to match with
:param item_store: an ItemStore, to retrieve items if needed
"""
return 0
@classmethod
def from_datavalue(self, wd_repr):
"""
Creates a WikidataValue from the JSON representation
of a Wikibase datavalue.
For now, somevalues are treated just like novalues:
>>> WikidataValue.from_datavalue({'snaktype': 'somevalue', 'datatype': 'wikibase-item', 'property': 'P61'}).is_novalue()
True
"""
typ = wd_repr['datatype']
val = wd_repr.get('datavalue', {}) # not provided for somevalue
cls = wdvalue_mapping.get(typ, UndefinedValue)
return cls.from_datavalue(val)
def is_novalue(self):
return self.json == {}
def as_string():
"""
String representation of the value,
for the old API that only returns strings
"""
raise NotImplemented
def as_openrefine_cell(self, lang, item_store):
"""
Returns a JSON representation for the
OpenRefine extend API.
Subclasses should reimplement _as_cell instead.
:param lang: the language in which the cell should be displayed
:param item_store: an ItemStore, to retrieve items if needed
"""
if self.is_novalue():
return {}
return self._as_cell(lang, item_store)
def _as_cell(self, lang, item_store):
"""
This method can assume that the value is not a novalue
:param lang: the language in which the cell should be displayed
:param item_store: an ItemStore, to retrieve items if needed
"""
raise NotImplemented
def __getattr__(self, key):
"""
For convenience:
"""
return self.json[key]
def __eq__(self, other):
if isinstance(other, WikidataValue):
return (other.value_type == self.value_type and
other.json == self.json)
return False
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return ("%s(%s)" %
(type(self).__name__,
(",".join([key+'='+val.__repr__()
for key, val in self.json.items()]))))
def __hash__(self):
val = (self.value_type,
tuple(sorted(self.json.items(),
key=lambda k:k[0])))
return val.__hash__()
@register
class ItemValue(WikidataValue):
"""
Fields:
- id (string)
"""
value_type = "wikibase-item"
@classmethod
def from_datavalue(self, wd_repr):
v = wd_repr.get('value')
if not v:
return ItemValue()
else:
return ItemValue(id=v.get('id'))
def match_with_str(self, s, item_store):
# First check if the target string looks like a Qid
qid = to_q(s)
if qid:
return 100 if qid == self.id else 0
# Then check for a sitelink
sitelink = SitelinkFetcher.normalize(s)
if sitelink:
target_qid = item_store.sitelink_fetcher.sitelinks_to_qids(
[sitelink]).get(sitelink)
return 100 if target_qid == self.id else 0
# Then check for a novalue match
if not s and self.is_novalue():
return 100
# Otherwise try to match the string to the labels and
# aliases of the item.
item = item_store.get_item(self.id)
labels = list(item.get('labels', {}).values())
aliases = item.get('aliases', [])
matching_scores = [
fuzzy_match_strings(s, name)
for name in labels+aliases
]
if not matching_scores:
return 0
else:
return max(matching_scores)
def as_string():
return self.json.get('id', '')
def _as_cell(self, lang, item_store):
return {
'id': self.id,
'name': item_store.get_label(self.id, lang),
}
@register
class UrlValue(WikidataValue):
"""
Fields:
- value (the URL itself)
- parsed (by urllib)
"""
value_type = "url"
def __init__(self, **kwargs):
super(UrlValue, self).__init__(**kwargs)
val = kwargs.get('value')
self.parsed = None
if val:
try:
self.parsed = urlparse(val)
if not self.parsed.netloc:
self.parsed = None
raise ValueError
self.canonical = self.canonicalize(self.parsed)
except ValueError:
pass
def canonicalize(self, parsed):
"""
Take a parsed URL and returns its
canonicalized form for exact matching
"""
return urlunparse(
('', # no scheme
parsed[1],
parsed[2],
parsed[3],
parsed[4],
parsed[5]))
@classmethod
def from_datavalue(self, wd_repr):
return UrlValue(value=wd_repr.get('value', {}))
def match_with_str(self, s, item_store):
# no value
if self.parsed is None:
return 0
# let's see if the candidate value is a URL
matched_val = s
try:
parsed_s = urlparse(s)
matched_val = self.canonicalize(parsed_s)
except ValueError:
pass
return 100 if matched_val == self.canonical else 0
def as_string(self):
return self.json.get('value', '')
def _as_cell(self, lang, item_store):
return {
'str': self.value
}
@register
class CoordsValue(WikidataValue):
"""
Fields:
- latitude (float)
- longitude (float)
- altitude (float)
- precision (float)
- globe (string)
>>> int(CoordsValue(latitude=53.3175,longitude=-4.6204).match_with_str("53.3175,-4.6204", None))
100
>>> int(CoordsValue(latitude=53.3175,longitude=-4.6204).match_with_str("53.3175,-5.6204", None))
0
"""
value_type = "globe-coordinate"
@classmethod
def from_datavalue(self, wd_repr):
return CoordsValue(**wd_repr.get('value', {}))
def match_with_str(self, s, item_store):
# parse the string as coordinates
parts = s.split(',')
if len(parts) != 2:
return 0.
try:
lat = float(parts[0])
lng = float(parts[1])
except ValueError:
return 0.
# measure the distance with the target coords
# (flat earth approximation)
diflat = lat - self.latitude
diflng = lng - self.longitude
dist = math.sqrt(diflat*diflat + diflng*diflng)
dist_in_km = (dist / 180) * math.pi * 6371 # earth radius
# TODO take the precision into account
return 100*max(0, 1 - dist_in_km)
def as_string(self):
return str(self.json.get('latitude', ''))+','+str(self.json.get('longitude', ''))
def _as_cell(self, lang, item_store):
return {
'str': self.as_string()
}
@register
class StringValue(WikidataValue):
"""
Fields:
- value (string)
"""
value_type = "string"
@classmethod
def from_datavalue(cls, wd_repr):
return cls(
value=wd_repr.get('value', {}))
def match_with_str(self, s, item_store):
ref_val = self.json.get('value')
if not ref_val:
return 0
return fuzzy_match_strings(ref_val, s)
def as_string(self):
return self.json.get('value', '')
def _as_cell(self, lang, item_store):
return {
'str': self.value
}
@register
class IdentifierValue(StringValue):
"""
Fields:
- value (string)
"""
value_type = "external-id"
def match_with_str(self, s, item_store):
return 100 if s.strip() == self.value else 0
@register
class QuantityValue(WikidataValue):
"""
Fields:
- amount (float)
- unit (string)
"""
value_type = "quantity"
def __init__(self, **values):
super(QuantityValue, self).__init__(**values)
self.amount = values.get('amount')
if self.amount is not None:
self.amount = float(self.amount)
@classmethod
def from_datavalue(cls, wd_repr):
return cls(**wd_repr.get('value', {}))
def match_with_str(self, s, item_store):
try:
f = float(s)
if self.amount is not None:
return match_floats(self.amount, f)
except ValueError:
pass
return 0
def as_string(self):
return str(self.json.get('amount', ''))
def is_novalue(self):
return self.amount is None
def _as_cell(self, lang, item_store):
return {
'float': self.amount
}
@register
class MonolingualValue(WikidataValue):
"""
Fields:
- text (string)
- language (string)
"""
value_type = "monolingualtext"
@classmethod
def from_datavalue(cls, wd_repr):
return cls(**wd_repr)
def match_with_str(self, s, item_store):
ref_val = self.json.get('text')
if not ref_val:
return 0
return fuzzy_match_strings(ref_val, s)
def as_string(self):
return self.json.get('text') or ''
def _as_cell(self, lang, item_store):
return {
'str': self.text
}
@register
class TimeValue(WikidataValue):
"""
Fields:
- time (as iso format, with a plus in front)
- parsed (as python datetime object)
- timezone
- before
- after
- precision
- calglobe-coordinateendarmodel
"""
value_type = "time"
def __init__(self, **values):
super(TimeValue, self).__init__(**values)
time = self.time
if time.startswith('+'):
time = time[1:]
try:
self.parsed = dateutil.parser.parse(time)
except ValueError:
self.parsed = None
@classmethod
def from_datavalue(cls, wd_repr):
return cls(**wd_repr.get('value', {}))
def match_with_str(self, s, item_store):
# TODO convert to a timestamp
# TODO compute difference
# TODO convert to a ratio based on the precision
return 0
def as_string(self):
return str(self.json.get('time', ''))
def is_novalue(self):
return self.parsed is None
def _as_cell(self, lang, item_store):
return {
'date': self.parsed.isoformat()
}
@register
class MediaValue(IdentifierValue):
"""
Fields:
- value
"""
value_type = "commonsMedia"
@register
class DataTableValue(IdentifierValue):
"""
Fields:
- value (string)
"""
value_type = "tabular-data"
class UndefinedValue(WikidataValue):
"""
This is different from "novalue" (which explicitely
defines an empty value.
This class is for value filters which want to return
an undefined value. It is purposely not registered
as it does not match any Wikibase value type.
(The equivalent in Wikibase would be not to state
a claim at all).
"""
value_type = "undefined"
@classmethod
def from_datavalue(cls, wd_repr):
return cls()
def match_with_str(self, s, item_store):
return 0
def is_novalue(self):
return False
def as_string(self):
return ""
def _as_cell(self, lang, item_store):
return {}