Offer kinship names in learning review when Detect skips them.
Frau/Sohn mentions in the user line must still reach the popup if OpenRouter only tags the food reading or misses the person. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
fa1c11c33e
commit
cc43fc6409
5 files changed
+137
-4
No files matched your search
+102
-1
@@ -11,7 +11,7 @@ import uuid
|
||||
from typing import Any
|
||||
|
||||
from db import get_db, row_to_dict
|
||||
from identity_store import ENTITY_TYPES, confirm_identity, is_maskable_label, normalize_label
|
||||
from identity_store import KINSHIP, ENTITY_TYPES, confirm_identity, is_maskable_label, normalize_label
|
||||
|
||||
MODE_SEMANTIC = "semantic"
|
||||
MODE_LEARNING = "learning"
|
||||
@@ -163,6 +163,107 @@ def needs_review(profile_id: str, mapping: dict, user_body: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def _word_matches(text: str):
|
||||
return list(re.finditer(rf"[{_LETTER}]+", text or ""))
|
||||
|
||||
|
||||
def _prev_word(text: str, index: int) -> str:
|
||||
words = re.findall(rf"[{_LETTER}]+", text[:index])
|
||||
return (words[-1].lower() if words else "")
|
||||
|
||||
|
||||
def kinship_governed_labels(user_body: str) -> set[str]:
|
||||
"""Labels after Frau/Sohn/… in the current user line. Not a food word list."""
|
||||
found: set[str] = set()
|
||||
for match in _word_matches(user_body):
|
||||
label = match.group(0)
|
||||
if not is_maskable_label(label):
|
||||
continue
|
||||
if _prev_word(user_body, match.start()) in KINSHIP:
|
||||
found.add(label.casefold())
|
||||
return found
|
||||
|
||||
|
||||
def _align_user_span(rendered: str, user_body: str, start: int, end: int) -> tuple[int, int]:
|
||||
if not user_body:
|
||||
return start, end
|
||||
pos = (rendered or "").rfind(user_body)
|
||||
if pos < 0:
|
||||
return start, end
|
||||
return pos + start, pos + end
|
||||
|
||||
|
||||
def _token_for_label(mappings: list[dict], label: str, entity_type: str = "PERSON") -> str:
|
||||
needle = label.casefold()
|
||||
for mapping in mappings:
|
||||
if (mapping.get("local_label") or "").casefold() == needle and mapping.get("token"):
|
||||
return mapping["token"]
|
||||
return f"{entity_type}:L{uuid.uuid4().hex[:6].upper()}"
|
||||
|
||||
|
||||
def supplement_kinship_candidates(
|
||||
profile_id: str,
|
||||
candidates: list[dict],
|
||||
mappings: list[dict],
|
||||
user_body: str,
|
||||
rendered: str = "",
|
||||
) -> tuple[list[dict], list[dict]]:
|
||||
"""If Detect misses Frau X / Sohn X, still offer those user-line mentions for review."""
|
||||
wanted = kinship_governed_labels(user_body)
|
||||
if not wanted:
|
||||
return candidates, mappings
|
||||
occupied: set[tuple[int, int, str]] = set()
|
||||
for item in candidates:
|
||||
label = (item.get("text") or "").casefold()
|
||||
excerpt = item.get("excerpt") or ""
|
||||
for match in _word_matches(user_body):
|
||||
if match.group(0).casefold() != label:
|
||||
continue
|
||||
snippet = _excerpt(user_body, match.start(), match.end(), match.group(0))
|
||||
if snippet == excerpt or match.group(0) in excerpt:
|
||||
occupied.add((match.start(), match.end(), label))
|
||||
break
|
||||
extra_mappings = list(mappings)
|
||||
extra_candidates = list(candidates)
|
||||
for match in _word_matches(user_body):
|
||||
label = match.group(0)
|
||||
if label.casefold() not in wanted:
|
||||
continue
|
||||
user_key = (match.start(), match.end(), label.casefold())
|
||||
if user_key in occupied:
|
||||
continue
|
||||
occupied.add(user_key)
|
||||
start, end = _align_user_span(rendered, user_body, match.start(), match.end())
|
||||
identity_shaped = _prev_word(user_body, match.start()) in KINSHIP
|
||||
extra_candidates.append(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"text": label,
|
||||
"entity_type": "PERSON",
|
||||
"start": start,
|
||||
"end": end,
|
||||
"excerpt": _excerpt(user_body, match.start(), match.end(), label),
|
||||
"ambiguous": get_sense(profile_id, label)["ambiguous"],
|
||||
"suggested": _DECISION_IDENTITY if identity_shaped else _DECISION_NOT,
|
||||
}
|
||||
)
|
||||
extra_mappings.append(
|
||||
{
|
||||
"token": _token_for_label(extra_mappings, label),
|
||||
"local_label": label,
|
||||
"canonical_label": label,
|
||||
"demask_label": label,
|
||||
"entity_type": "PERSON",
|
||||
"source": "request_local",
|
||||
"start": start,
|
||||
"end": end,
|
||||
"aliases": [],
|
||||
"labels": [label],
|
||||
}
|
||||
)
|
||||
return extra_candidates, extra_mappings
|
||||
|
||||
|
||||
def build_candidates(profile_id: str, mappings: list[dict], user_body: str) -> list[dict]:
|
||||
seen: set[tuple[int | None, int | None, str]] = set()
|
||||
items: list[dict] = []
|
||||
|
||||
Reference in new issue
Block a user