update
This commit is contained in:
@@ -6,6 +6,7 @@ from pathlib import Path
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from app.modules.work_assistant.context_text import tokens
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
@@ -19,16 +20,24 @@ def load_contracts() -> dict[str, dict]:
|
||||
|
||||
|
||||
def _tokens(value: str) -> set[str]:
|
||||
words = set(re.findall(r"[a-zA-Z][a-zA-Z0-9_]+", value.lower()))
|
||||
for run in re.findall(r"[\u4e00-\u9fff]+", value):
|
||||
words.update(run[i:i+2] for i in range(len(run)-1))
|
||||
return words
|
||||
return tokens(value)
|
||||
|
||||
|
||||
def _name_match(message: str, resource: dict) -> float:
|
||||
# Menu suffixes describe UI organization, not the user's business entity.
|
||||
# "部门" should match "部门管理" before unrelated fields mentioning a company.
|
||||
name = resource.get("name", "")
|
||||
stem = re.sub(r"(?:管理|列表|查询|统计|记录|信息|基础)+$", "", name)
|
||||
names = {name, stem, *resource.get("aliases", [])}
|
||||
return max((min(len(alias), 6) * 2 for alias in names if len(alias) >= 2 and alias in message), default=0)
|
||||
|
||||
|
||||
def resource_index(resources: list[dict]) -> list[dict]:
|
||||
return [{"id": resource["id"], "name": resource.get("name", resource["id"]),
|
||||
"module": resource.get("module_skill", {}).get("menu_path", "") or "、".join(resource.get("menu_paths", [])),
|
||||
"queryable": bool(resource.get("operations")),
|
||||
"aliases": resource.get("aliases", [])[:6],
|
||||
"related_resources": sorted({r.get("target") for r in resource.get("relations", {}).values() if r.get("target")}),
|
||||
"description": (resource.get("description") or resource.get("module_skill",{}).get("scope", ""))[:220]}
|
||||
for resource in resources]
|
||||
|
||||
@@ -49,12 +58,31 @@ def retrieve_descriptions(message: str, resources: list[dict], previous_spec: di
|
||||
for position, (resource, strong, weak) in enumerate(documents):
|
||||
score = sum(math.log(1+len(resources)/(1+frequency[token]))*(3 if token in strong else 1)
|
||||
for token in query if token in strong or token in weak)
|
||||
score += _name_match(message, resource)
|
||||
# Context is a hint, not a rule overriding a new topic.
|
||||
if resource["id"] in old:
|
||||
score += 1
|
||||
ranked.append((score, -position, resource))
|
||||
ranked.sort(key=lambda item: (item[0], item[1]), reverse=True)
|
||||
selected = [item[2] for item in ranked[:maximum]]
|
||||
# Reserve candidates for each explicit sub-question before filling by global
|
||||
# relevance. A long first clause must not crowd out a second business goal.
|
||||
selected = []
|
||||
clauses = [part.strip() for part in re.split(r"[,,;;。!?!?\n]|以及|同时|另外", message) if part.strip()]
|
||||
if len(clauses) > 1:
|
||||
for clause in clauses[:maximum]:
|
||||
terms = _tokens(clause)
|
||||
def clause_score(document):
|
||||
resource, strong, weak = document
|
||||
return _name_match(clause, resource) + sum(math.log(1 + len(resources) / (1 + frequency.get(term, 0))) * (3 if term in strong else 1)
|
||||
for term in terms if term in strong or term in weak)
|
||||
candidate = max(documents, key=clause_score)
|
||||
if clause_score(candidate) > 0 and candidate[0] not in selected:
|
||||
selected.append(candidate[0])
|
||||
for _, _, resource in ranked:
|
||||
if len(selected) >= maximum:
|
||||
break
|
||||
if resource not in selected:
|
||||
selected.append(resource)
|
||||
for resource in resources:
|
||||
if resource["id"] in old and resource not in selected and len(selected) < maximum+2:
|
||||
selected.append(resource)
|
||||
|
||||
Reference in New Issue
Block a user