接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
298 lines
16 KiB
Python
298 lines
16 KiB
Python
"""Resolve product mentions against the caller's already-authorized catalogue.
|
||
|
||
This module only identifies catalogue names. It does not construct queries,
|
||
change filters, aggregate products, or infer results from an absent name.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
import unicodedata
|
||
from decimal import Decimal, InvalidOperation
|
||
|
||
_DIMENSIONS = re.compile(r'(?<![\d.])(?:d\d+(?:\.\d+)?(?:x\d+(?:\.\d+)?){1,}|(?<!d)\d+(?:\.\d+)?(?:x\d+(?:\.\d+)?){2,})(?![\d.]|x\d)')
|
||
_WORD = re.compile(r'[a-z0-9.+]')
|
||
_SEPARATORS = str.maketrans({'×': 'x', '✕': 'x', '✖': 'x', '*': 'x', 'φ': 'd', 'ø': 'd', '⌀': 'd'})
|
||
_CUSTOMER_VALUE = re.compile(r'''(?:客户代码|客户编码|客户编号|客户代号|客户号)(?:为|是)?[::=]?["'“‘「『【((]*([a-z0-9][a-z0-9.+_-]{0,127})''')
|
||
|
||
|
||
def _normalized(value):
|
||
return ''.join(unicodedata.normalize('NFKC', str(value)).casefold().translate(_SEPARATORS).split())
|
||
|
||
|
||
def _dimension_key(value):
|
||
try:
|
||
round_spec = value.startswith('d')
|
||
dimensions = value[1:] if round_spec else value
|
||
return ('round' if round_spec else 'block', *(Decimal(part).normalize() for part in dimensions.split('x')))
|
||
except InvalidOperation:
|
||
return ()
|
||
|
||
|
||
def _exact_spans(question, name):
|
||
"""Find a whole catalogue name, not a prefix of another specification."""
|
||
if len(name) < 3 or name.isdigit():
|
||
return []
|
||
spans = []
|
||
pos = 0
|
||
while True:
|
||
start = question.find(name, pos)
|
||
if start < 0:
|
||
break
|
||
end = start + len(name)
|
||
if (not start or not _WORD.fullmatch(question[start - 1])) and (end == len(question) or not _WORD.fullmatch(question[end])):
|
||
spans.append((start, end))
|
||
pos = start + 1
|
||
return spans
|
||
|
||
|
||
def _prefix_right_boundary(text, end):
|
||
"""Require a product-like boundary after a partial catalog name."""
|
||
if end == len(text):
|
||
return True
|
||
char = text[end]
|
||
if _WORD.fullmatch(char):
|
||
return False
|
||
if char.isspace() or unicodedata.category(char)[0] != 'L':
|
||
return True
|
||
# Chinese product names are not separated by spaces. Accept common query
|
||
# particles/quality terms after a mentioned product, while rejecting an
|
||
# arbitrary continuation of the product name itself.
|
||
return bool(re.match(r'(?:的|之|为|是|规格|型号|产品|黑片|亮片|合格率|良率|报废|不良|重量|投入|趋势|排名|变化|质量|情况)', text[end:]))
|
||
|
||
|
||
def identify_products(question, options, *, limit=20, respect_exclusions=False):
|
||
"""Return explicit names and exact-dimension candidates, bounded for planning.
|
||
|
||
``options`` must be the permission-filtered response from /api/options for
|
||
the selected release. Decimal dimensions are compared numerically, never
|
||
as prefixes, so 18x15x4 does not match 18x15x4.1.
|
||
"""
|
||
options = options if isinstance(options, dict) else {}
|
||
products = options.get('products') if isinstance(options.get('products'), list) else []
|
||
names = sorted({name.strip() for name in products
|
||
if isinstance(name, str) and name.strip()})
|
||
text = _normalized(question)
|
||
paths = options.get('organizationPaths', [])
|
||
by_name = {name: {'name': name, 'departments': set(), 'groups': set()} for name in names}
|
||
if isinstance(paths, list):
|
||
for path in paths:
|
||
if not isinstance(path, dict) or path.get('product') not in by_name:
|
||
continue
|
||
entry = by_name[path['product']]
|
||
for field, output in (('department', 'departments'), ('group', 'groups')):
|
||
value = path.get(field)
|
||
if isinstance(value, str) and value.strip():
|
||
entry[output].add(value.strip())
|
||
|
||
def records(selected):
|
||
return [{'name': name, 'departments': sorted(by_name[name]['departments']),
|
||
'groups': sorted(by_name[name]['groups'])} for name in selected[:limit]]
|
||
|
||
normalized_names = {name: _normalized(name) for name in names}
|
||
# A customer code can itself look like a dimensional specification. It
|
||
# must stay a field value rather than accidentally become a product.
|
||
other_field_values = [match.span(1) for match in _CUSTOMER_VALUE.finditer(text)]
|
||
def is_other_field_value(start, end):
|
||
return any(field_start <= start and end <= field_end for field_start, field_end in other_field_values)
|
||
exact = {name: spans for name, value in normalized_names.items()
|
||
if (spans := [(start, end) for start, end in _exact_spans(text, value)
|
||
if not is_other_field_value(start, end)])}
|
||
exact_spans = [span for spans in exact.values() for span in spans]
|
||
dimensional = {}
|
||
for name, value in normalized_names.items():
|
||
for match in _DIMENSIONS.finditer(value):
|
||
dimensional.setdefault(_dimension_key(match.group()), set()).add(name)
|
||
|
||
# A boundary-delimited prefix can still name more than one authorized
|
||
# product (for example, "合成产品0" with catalog entries 01..06). Treat
|
||
# that as a candidate set, never as a product filter. Prefer the longest
|
||
# prefix at each position and keep it inside the caller-provided catalog.
|
||
prefix_names = {}
|
||
max_prefix_length = 0
|
||
for name, value in normalized_names.items():
|
||
for length in range(3, min(len(value), len(text) + 1)):
|
||
prefix = value[:length]
|
||
if prefix.isdigit():
|
||
continue
|
||
prefix_names.setdefault(prefix, set()).add(name)
|
||
max_prefix_length = max(max_prefix_length, length)
|
||
dimension_spans = [match.span() for match in _DIMENSIONS.finditer(text)
|
||
if not is_other_field_value(*match.span())]
|
||
prefix_by_span = {}
|
||
for start in range(len(text)):
|
||
upper = min(len(text), start + max_prefix_length)
|
||
for end in range(upper, start + 2, -1):
|
||
if (start and _WORD.fullmatch(text[start - 1])) or (end < len(text) and _WORD.fullmatch(text[end])):
|
||
continue
|
||
if not _prefix_right_boundary(text, end):
|
||
continue
|
||
if is_other_field_value(start, end):
|
||
continue
|
||
if any(left <= start and end <= right for left, right in exact_spans + dimension_spans):
|
||
continue
|
||
candidates = prefix_names.get(text[start:end])
|
||
if candidates:
|
||
prefix_by_span[(start, end)] = candidates
|
||
break
|
||
|
||
mentions = []
|
||
full_candidates_by_span = {}
|
||
excluded_names = set()
|
||
def exclusion_for_span(start):
|
||
prefix = re.split(r'[,,。;;!?!?]', text[:start])[-1]
|
||
tail = r'(?:的|规格|尺寸|产品|为|是|:|:){0,6}$'
|
||
return bool(respect_exclusions and re.search(
|
||
r'(?:不要包含|不要包括|不包含|不包括|不计入|不要看|不看|排除|剔除|去除)'+tail,prefix)
|
||
and not re.search(r'(?:不要|不用|不再|取消)(?:排除|剔除|去除)'+tail,prefix))
|
||
for match in _DIMENSIONS.finditer(text):
|
||
if is_other_field_value(match.start(), match.end()):
|
||
continue
|
||
# A complete catalogue name around this token is a stronger explicit
|
||
# choice than a bare dimensional shorthand.
|
||
chosen = sorted(name for name, spans in exact.items()
|
||
if any(start <= match.start() and match.end() <= end for start, end in spans))
|
||
# An explicit but unknown suffix is part of the user's specification;
|
||
# never silently replace it with an existing variant of the same size.
|
||
suffix = re.match(r'[+_-][a-z0-9][a-z0-9_.-]*', text[match.end():])
|
||
candidates = chosen or ([] if suffix else sorted(dimensional.get(_dimension_key(match.group()), ())))
|
||
status = ('exact' if len(chosen) == 1 else 'unique' if not chosen and len(candidates) == 1 else
|
||
'ambiguous' if candidates else 'none')
|
||
end = match.end() + (len(suffix.group()) if suffix else 0)
|
||
excluded = exclusion_for_span(match.start())
|
||
if excluded: excluded_names.update(candidates)
|
||
full_candidates_by_span[(match.start(),end)] = candidates
|
||
mentions.append({'query': text[match.start():end], 'normalizedSpan': [match.start(), end],
|
||
'status': status, 'candidates': records(candidates),
|
||
'candidateCount': len(candidates), 'truncated': len(candidates) > limit,
|
||
**({'intent':'exclude'} if excluded else {})})
|
||
|
||
for (start, end), candidate_set in sorted(prefix_by_span.items()):
|
||
candidates = sorted(candidate_set)
|
||
excluded = exclusion_for_span(start)
|
||
if excluded:
|
||
excluded_names.update(candidates)
|
||
full_candidates_by_span[(start, end)] = candidates
|
||
mentions.append({'query': text[start:end], 'normalizedSpan': [start, end],
|
||
'status': 'ambiguous' if len(candidates) > 1 else 'unique',
|
||
'candidates': records(candidates), 'candidateCount': len(candidates),
|
||
'truncated': len(candidates) > limit, 'matchType': 'catalog_prefix',
|
||
**({'intent':'exclude'} if excluded else {})})
|
||
|
||
if respect_exclusions:
|
||
positive = []
|
||
for mention in mentions:
|
||
if mention.get('intent') == 'exclude': continue
|
||
original = full_candidates_by_span[tuple(mention['normalizedSpan'])]
|
||
kept = [name for name in original if name not in excluded_names]
|
||
status = mention['status'] if kept == original else 'unique' if len(kept)==1 else 'ambiguous' if kept else 'none'
|
||
positive.append({**mention,'candidates':records(kept),'candidateCount':len(kept),
|
||
'truncated':len(kept)>limit,'status':status})
|
||
effective_mentions = positive
|
||
else:
|
||
effective_mentions = mentions
|
||
|
||
if not mentions and exact:
|
||
selected = sorted(exact)
|
||
mentions = [{'query': '', 'status': 'exact' if len(selected) == 1 else 'multiple',
|
||
'candidates': records(selected), 'candidateCount': len(selected),
|
||
'truncated': len(selected) > limit}]
|
||
effective_mentions = mentions
|
||
if not effective_mentions:
|
||
match = {'query': '', 'status': 'none', 'candidates': [], 'candidateCount': 0, 'truncated': False}
|
||
elif len(effective_mentions) == 1:
|
||
match = dict(effective_mentions[0])
|
||
else:
|
||
selected = sorted({entry['name'] for mention in effective_mentions for entry in mention['candidates']})
|
||
match = {'query': '', 'status': 'ambiguous' if any(m['status'] == 'ambiguous' for m in effective_mentions) else 'multiple',
|
||
'candidates': records(selected), 'candidateCount': len(selected),
|
||
'truncated': any(m['truncated'] for m in mentions) or len(selected) > limit}
|
||
match['mentions'] = mentions
|
||
return {'mentionedProducts': sorted(exact)[:limit], 'productMatch': match,
|
||
**({'explicitlyExcludedProducts':sorted(excluded_names)} if respect_exclusions else {})}
|
||
|
||
|
||
def authorized_extra_fields(catalog):
|
||
"""Extract only field metadata returned by the actor-scoped API catalog."""
|
||
if not isinstance(catalog, dict) or catalog.get('kind') != 'analysis_catalog' or not isinstance(catalog.get('extraFields'), list):
|
||
return {'extraFieldsStatus': 'unavailable', 'extraFields': []}
|
||
fields = []
|
||
for item in catalog['extraFields']:
|
||
if not isinstance(item, dict) or item.get('enabled') is False:
|
||
continue
|
||
key, label = item.get('key'), item.get('label')
|
||
if (not isinstance(key, str) or not re.fullmatch(r'[A-Za-z][A-Za-z0-9_]{0,63}', key)
|
||
or not isinstance(label, str) or not label.strip() or len(label) > 80):
|
||
continue
|
||
fields.append({'key': key, 'label': label.strip()})
|
||
return {'extraFieldsStatus': 'ready', 'extraFields': fields}
|
||
|
||
|
||
def resolve_clarification_choice(question, options, previous_task):
|
||
"""Resolve a short suffix selection only within the saved pending choices.
|
||
|
||
The caller must supply the prior task from the same authorized session and
|
||
release, and current options must come from the permission-filtered API.
|
||
``None`` means the reply did not unambiguously select a saved candidate.
|
||
"""
|
||
if not isinstance(previous_task, dict):
|
||
return None
|
||
pending = previous_task.get('productResolution')
|
||
if not isinstance(pending, dict) or not isinstance(pending.get('originalQuestion'), str):
|
||
return None
|
||
# Once a choice has been planned, restore it only for the same short reply
|
||
# on retry. Never attach a previously selected product to a new question.
|
||
is_pending = previous_task.get('needsClarification') is True
|
||
if not is_pending:
|
||
if (not isinstance(pending.get('selectedName'), str)
|
||
or not isinstance(pending.get('selectionQuestion'), str)
|
||
or _normalized(question) != _normalized(pending['selectionQuestion'])):
|
||
return None
|
||
previous_match = pending.get('productMatch')
|
||
if (not isinstance(previous_match, dict) or previous_match.get('status') != 'ambiguous'
|
||
or previous_match.get('truncated') is not False):
|
||
return None
|
||
entries = previous_match.get('candidates')
|
||
if not isinstance(entries, list) or type(previous_match.get('candidateCount')) is not int:
|
||
return None
|
||
names = [item.get('name') for item in entries if isinstance(item, dict) and isinstance(item.get('name'), str)]
|
||
if len(names) != len(entries) or len(set(names)) != len(names) or len(names) != previous_match['candidateCount']:
|
||
return None
|
||
# A short explicit selection only; unrelated follow-ups or requests for
|
||
# multiple variants do not silently reuse an old pending clarification.
|
||
reply = str(question).strip()
|
||
ui_prefix = '继续上一轮分析,保留未修改的原目标和条件。\n我的选择:'
|
||
if reply.startswith(ui_prefix):
|
||
reply = reply[len(ui_prefix):].strip()
|
||
text = _normalized(reply)
|
||
full_reply = re.sub(r'^(?:我想选|我选择|我选|我要|选择|查看|分析|选|看|用)', '', text)
|
||
full_reply = re.sub(r'(?:这个|那个|这款|那款|的|规格|产品|型号|即可|吧|[。!?.!?])+$', '', full_reply)
|
||
full_names = [name for name in names if _normalized(name) == full_reply]
|
||
choice = re.fullmatch(r'(?:我想选|我选择|我选|我要|选|选择|查看|分析|看|用)?\+?([a-z0-9][a-z0-9_-]{0,19})(?:这个|那个|这款|那款|的|规格|产品|型号|即可|吧|[。!?.!?])*', text)
|
||
if not choice and len(full_names) != 1:
|
||
return None
|
||
token = choice.group(1) if choice else None
|
||
available = options.get('products') if isinstance(options, dict) else None
|
||
if not isinstance(available, list):
|
||
return None
|
||
authorized = {item for item in available if isinstance(item, str)}
|
||
selected = []
|
||
for name in names:
|
||
if name not in authorized:
|
||
continue
|
||
suffix = re.search(r'\+([a-z0-9][a-z0-9_-]{0,19})$', _normalized(name))
|
||
if name in full_names or suffix and suffix.group(1) == token:
|
||
selected.append(name)
|
||
if len(selected) != 1:
|
||
return None
|
||
if not is_pending and selected[0] != pending['selectedName']:
|
||
return None
|
||
result = identify_products(selected[0], options)
|
||
match = result['productMatch']
|
||
if (match['status'] != 'exact' or match['candidateCount'] != 1
|
||
or match['candidates'][0]['name'] != selected[0]):
|
||
return None
|
||
match['selectedFromClarification'] = True
|
||
match['previousQuestion'] = pending['originalQuestion']
|
||
match['previousQuery'] = previous_match.get('query', '')
|
||
return result
|