"""Absent wake phrases come from real positive prefixes in OTHER matched groups.""" from .common import tokens,uid def finalize(groups): bank={} for group in groups: for target,phrase in group['scenes'][0]['keywords'].items(): if target.startswith('H'):bank.setdefault(phrase,[]).append(dict(group_id=group['group_id'],target_id=target)) for group in groups: scene=group['scenes'][0];emissions=scene['humans']+scene['playback_sources'] candidates=[phrase for phrase in bank if any(d['group_id']!=group['group_id'] for d in bank[phrase]) and not any(tokens(e['text'])[:len(tokens(phrase))]==tokens(phrase) for source in emissions for e in source['events'])] if not candidates:raise ValueError('No real held-out absent prefix exists for this group') phrase=min(candidates,key=lambda p:uid(group['seed'],'absent_wake_phrase',p)) donor=next(d for d in bank[phrase] if d['group_id']!=group['group_id']) for scene in group['scenes']: scene['keywords']['no_match']=phrase scene['absent_keyword_provenance']=dict(policy='positive real-speech prefix from another matched group; absent from all current human/robot utterance beginnings',**donor) return groups