57 lines
2.4 KiB
Python
57 lines
2.4 KiB
Python
"""Deterministic context grouping; descriptive text is not execution proof."""
|
|
import copy
|
|
|
|
|
|
def consolidate_entities(context):
|
|
sources = context['sources']
|
|
identities = {}
|
|
for sid, source in list(sources.items()):
|
|
if source['kind'] != 'entity':
|
|
continue
|
|
data = source['data']
|
|
identity = (data['type'], str(data['id']))
|
|
if identity not in identities:
|
|
identities[identity] = sid
|
|
target_id = identities[identity]
|
|
target = sources[target_id]['data']
|
|
if data.get('citation_policy') != 'grouped_capture_v3':
|
|
continue
|
|
# The canonical entity keeps its original provenance and aliases. Lookup
|
|
# provenance is separate and always points to exact captured rows.
|
|
provenance = copy.deepcopy(data['provenance'])
|
|
association = {'name':data['name'], 'provenance':provenance,
|
|
'roles':copy.deepcopy(data['roles']), 'reason':data['reason'],
|
|
'mention_source_ids':copy.deepcopy(data['mention_source_ids']),
|
|
'assessment_ref':data['assessment_ref']}
|
|
locale = data.get('locale')
|
|
if locale is not None:
|
|
association['locale'] = locale
|
|
target.setdefault('lookup_associations', []).append(association)
|
|
target['citation_policy'] = 'grouped_capture_v3'
|
|
target['association'] = 'model_contextual_selection_not_causation'
|
|
for field in ('roles','mention_source_ids'):
|
|
target.setdefault(field, [])
|
|
target[field] = list(dict.fromkeys([*target[field], *data[field]]))
|
|
if target_id != sid:
|
|
del sources[sid]
|
|
return context
|
|
|
|
|
|
def quality_context(context):
|
|
return any(isinstance(s['data'],dict) and s['data'].get('citation_policy') == 'grouped_capture_v3'
|
|
for s in context['sources'].values())
|
|
|
|
|
|
def description_sources(context):
|
|
result = set()
|
|
for sid, source in context['sources'].items():
|
|
data = source['data']
|
|
if source['kind'] != 'fact' or data.get('name') != 'project_description':
|
|
continue
|
|
value = data.get('value')
|
|
texts = value.get('texts',[]) if isinstance(value,dict) else []
|
|
if isinstance(texts,list) and any(isinstance(t,dict) and isinstance(t.get('text'),str)
|
|
and t['text'].strip() for t in texts):
|
|
result.add(sid)
|
|
return result
|