32 lines
1.5 KiB
Python
32 lines
1.5 KiB
Python
"""Deduplicate server-owned notices without removing factual qualifications."""
|
|
from tools.answers.context_policy import normalized
|
|
|
|
LIMIT_NOTICE='Zobrazený je obmedzený výber podkladov. Otázku môžeš zúžiť na osobu, prácu, obdobie alebo konkrétnu oblasť.'
|
|
LIMIT_PREFIXES=('prechod grafom dosiahol limit vyberu','zobrazene su priklady z obmedzeneho vyberu',
|
|
'zobrazeny je vyber podkladov','zobrazeny je obmedzeny vyber')
|
|
|
|
|
|
def unique_notices(notes):
|
|
result=[];seen=set()
|
|
for note in notes:
|
|
note=note.strip()
|
|
if normalized(note).startswith(LIMIT_PREFIXES):note=LIMIT_NOTICE
|
|
key=normalized(note)
|
|
if key and key not in seen:result.append(note);seen.add(key)
|
|
return result
|
|
|
|
|
|
def clean_answer_notices(result):
|
|
if not isinstance(result.get('answer'),str):return result
|
|
paragraphs=result['answer'].split('\n\n')
|
|
# Only standalone, known server notices are interchangeable. Literal quotes,
|
|
# source content, negations, and specific missing-person notes stay intact.
|
|
kept=[];limit_seen=False
|
|
for paragraph in paragraphs:
|
|
if normalized(paragraph).startswith(LIMIT_PREFIXES) and not paragraph.startswith(('-','*','>')):
|
|
if limit_seen:continue
|
|
paragraph=LIMIT_NOTICE;limit_seen=True
|
|
kept.append(paragraph)
|
|
notes=unique_notices(result.get('presentation_notes',[]))
|
|
return {**result,'answer':'\n\n'.join(kept),**({'presentation_notes':notes} if 'presentation_notes' in result else {})}
|