Search for wikibase for unused depicts.

This commit is contained in:
Edward Betts 2019-10-07 21:45:21 +01:00
parent 888610f5fe
commit 96a03a0abe

24
app.py
View file

@ -943,6 +943,7 @@ def depicts_lookup():
item_ids = [] item_ids = []
hits = [] hits = []
q1 = DepictsItem.query.filter(DepictsItem.label.ilike(terms + '%')) q1 = DepictsItem.query.filter(DepictsItem.label.ilike(terms + '%'))
seen = set()
for item in q1: for item in q1:
hit = { hit = {
'label': item.label, 'label': item.label,
@ -952,6 +953,7 @@ def depicts_lookup():
} }
item_ids.append(item.item_id) item_ids.append(item.item_id)
hits.append(hit) hits.append(hit)
seen.add(item.qid)
cls = DepictsItemAltLabel cls = DepictsItemAltLabel
q2 = cls.query.filter(cls.alt_label.ilike(terms + '%'), q2 = cls.query.filter(cls.alt_label.ilike(terms + '%'),
@ -967,9 +969,31 @@ def depicts_lookup():
'alt_label': alt.alt_label, 'alt_label': alt.alt_label,
} }
hits.append(hit) hits.append(hit)
seen.add(item.qid)
r = mediawiki.api_call({
'action': 'wbsearchentities',
'search': terms,
'limit': 'max',
'language': 'en'
})
hits.sort(key=lambda hit: hit['count'], reverse=True) hits.sort(key=lambda hit: hit['count'], reverse=True)
for result in r.json()['search']:
if result['id'] in seen:
continue
seen.add(result['id'])
hit = {
'label': result['label'],
'description': result.get('description') or None,
'qid': result['id'],
'count': 0,
}
if result['match']['type'] == 'alias':
hit['alt_label'] = result['match']['text']
hits.append(hit)
ret = { ret = {
'count': q1.count() + q2.count(), 'count': q1.count() + q2.count(),
'hits': hits, 'hits': hits,