46 lines
2.1 KiB
Python
46 lines
2.1 KiB
Python
"""Small, source-declared query invariants, applied after semantic planning.
|
||
|
||
These helpers do not grant access or select arbitrary data sources. They prevent
|
||
an explicit categorical choice, or a pure date/metric follow-up, from being
|
||
overwritten by model defaults.
|
||
"""
|
||
import re
|
||
|
||
|
||
def literal_filter_values(message, definition):
|
||
result = {}
|
||
for field, spec in definition.get('filters', {}).items():
|
||
if not spec.get('literal_values'):
|
||
continue
|
||
matches = []
|
||
for key, label in spec.get('values', {}).items():
|
||
occurrences = list(re.finditer(re.escape(label), message))
|
||
if any(not re.search(r'(?:不是|不要|不查|不看|排除)\s*$', message[max(0, m.start()-8):m.start()]) for m in occurrences):
|
||
matches.append(key)
|
||
if len(matches) == 1:
|
||
result[field] = matches[0]
|
||
return result
|
||
|
||
|
||
def asks_application_status(message):
|
||
return bool(re.search(r'是否.{0,4}(?:通过|批准)|(?:通过|批准)(?:了)?[吗么没]|审批.{0,6}(?:哪一步|哪里|怎么样)|(?:现在|当前).{0,4}状态', message))
|
||
|
||
|
||
def followup_change(message, relative_period, definition=None):
|
||
"""A pure change cannot silently add a new business predicate."""
|
||
compact = re.sub(r'[\s,,。??]', '', message)
|
||
if relative_period:
|
||
remainder = compact.replace(relative_period[1], '', 1)
|
||
if re.fullmatch(r'(?:那|再|改成|换成|只看|只查|查|看)?(?:的|呢|呀|啊|吗)*', remainder):
|
||
return 'time'
|
||
if re.fullmatch(r'(?:那|这些|这批|它们)?(?:合计|总计|一共|总额|总金额|累计金额)(?:是多少|多少|多少钱|金额|金额是多少)?(?:呢)?', compact):
|
||
return 'metric'
|
||
if definition:
|
||
literals = literal_filter_values(message, definition)
|
||
if len(literals) == 1:
|
||
field, value = next(iter(literals.items()))
|
||
label = definition['filters'][field]['values'][value]
|
||
if re.fullmatch(r'(?:那|改成|换成|再看|看看)' + re.escape(label) + r'(?:的|呢|时间不变|其他条件不变)*', compact):
|
||
return field
|
||
return ''
|