Apply learned mask reviews by local cue and highlight the reviewed mention.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3f7c0d71a5
commit
f7ffd28332
10 files changed
+413
-36
No files matched your search
+234
-29
@@ -22,6 +22,8 @@ WINDOW = 80
|
||||
|
||||
_DECISION_IDENTITY = "identity"
|
||||
_DECISION_NOT = "not_identity"
|
||||
_DECISION_ASK = "ask"
|
||||
_CUE_BARE = "_bare"
|
||||
|
||||
|
||||
def get_detect_operating_mode() -> str:
|
||||
@@ -136,31 +138,82 @@ def _in_user_text(label: str, user_body: str) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _excerpt(user_body: str, start: int | None, end: int | None, label: str) -> str:
|
||||
def _resolve_user_span(
|
||||
user_body: str,
|
||||
start: int | None,
|
||||
end: int | None,
|
||||
label: str,
|
||||
rendered: str = "",
|
||||
) -> tuple[int, int] | None:
|
||||
text = user_body or ""
|
||||
if start is None or end is None or start < 0 or end > len(text):
|
||||
match = re.search(rf"(?<![{_LETTER}]){re.escape(label)}(?![{_LETTER}])", text, re.IGNORECASE)
|
||||
if not match:
|
||||
return text[:WINDOW]
|
||||
start, end = match.start(), match.end()
|
||||
left = max(0, int(start) - WINDOW)
|
||||
right = min(len(text), int(end) + WINDOW)
|
||||
return text[left:right]
|
||||
needle = (label or "").casefold()
|
||||
if start is not None and end is not None:
|
||||
left, right = int(start), int(end)
|
||||
if 0 <= left < right <= len(text) and text[left:right].casefold() == needle:
|
||||
return left, right
|
||||
if rendered and text:
|
||||
origin = rendered.rfind(text)
|
||||
if origin >= 0:
|
||||
user_left, user_right = left - origin, right - origin
|
||||
if 0 <= user_left < user_right <= len(text) and text[user_left:user_right].casefold() == needle:
|
||||
return user_left, user_right
|
||||
if not needle:
|
||||
return None
|
||||
match = re.search(rf"(?<![{_LETTER}]){re.escape(label)}(?![{_LETTER}])", text, re.IGNORECASE)
|
||||
if not match:
|
||||
return None
|
||||
return match.start(), match.end()
|
||||
|
||||
|
||||
def needs_review(profile_id: str, mapping: dict, user_body: str) -> bool:
|
||||
def excerpt_view(
|
||||
user_body: str,
|
||||
start: int | None,
|
||||
end: int | None,
|
||||
label: str,
|
||||
rendered: str = "",
|
||||
) -> dict:
|
||||
text = user_body or ""
|
||||
span = _resolve_user_span(text, start, end, label, rendered)
|
||||
if not span:
|
||||
snippet = text[:WINDOW]
|
||||
return {"excerpt": snippet, "highlight_start": None, "highlight_end": None}
|
||||
left_i, right_i = span
|
||||
clip_left = max(0, left_i - WINDOW)
|
||||
clip_right = min(len(text), right_i + WINDOW)
|
||||
prefix = "…" if clip_left > 0 else ""
|
||||
suffix = "…" if clip_right < len(text) else ""
|
||||
snippet = text[clip_left:clip_right]
|
||||
return {
|
||||
"excerpt": f"{prefix}{snippet}{suffix}",
|
||||
"highlight_start": left_i - clip_left + len(prefix),
|
||||
"highlight_end": right_i - clip_left + len(prefix),
|
||||
}
|
||||
|
||||
|
||||
def _excerpt(user_body: str, start: int | None, end: int | None, label: str) -> str:
|
||||
return excerpt_view(user_body, start, end, label)["excerpt"]
|
||||
|
||||
|
||||
def needs_review(profile_id: str, mapping: dict, user_body: str, rendered: str = "") -> bool:
|
||||
label = mapping.get("local_label") or ""
|
||||
if mapping.get("source") == "confirmed_registry":
|
||||
sense = get_sense(profile_id, label)
|
||||
return bool(sense["ambiguous"]) and _in_user_text(label, user_body)
|
||||
return (
|
||||
_in_user_text(label, user_body)
|
||||
and decision_for_mention(
|
||||
profile_id, label, user_body, mapping.get("start"), mapping.get("end"), rendered
|
||||
)
|
||||
== _DECISION_ASK
|
||||
)
|
||||
if mapping.get("source") != "request_local":
|
||||
return False
|
||||
if not is_maskable_label(label) or not _in_user_text(label, user_body):
|
||||
return False
|
||||
sense = get_sense(profile_id, label)
|
||||
if sense["identity_hits"] > 0 and not sense["ambiguous"]:
|
||||
return False
|
||||
return True
|
||||
return (
|
||||
decision_for_mention(
|
||||
profile_id, label, user_body, mapping.get("start"), mapping.get("end"), rendered
|
||||
)
|
||||
== _DECISION_ASK
|
||||
)
|
||||
|
||||
|
||||
def _word_matches(text: str):
|
||||
@@ -172,6 +225,143 @@ def _prev_word(text: str, index: int) -> str:
|
||||
return (words[-1].lower() if words else "")
|
||||
|
||||
|
||||
def cue_for_mention(
|
||||
user_body: str,
|
||||
start: int | None,
|
||||
end: int | None,
|
||||
label: str,
|
||||
rendered: str = "",
|
||||
) -> str:
|
||||
span = _resolve_user_span(user_body, start, end, label, rendered)
|
||||
if not span:
|
||||
return _CUE_BARE
|
||||
return _prev_word(user_body, span[0]) or _CUE_BARE
|
||||
|
||||
|
||||
def _mention_fields(
|
||||
user_body: str,
|
||||
start: int | None,
|
||||
end: int | None,
|
||||
label: str,
|
||||
rendered: str = "",
|
||||
) -> dict:
|
||||
span = _resolve_user_span(user_body, start, end, label, rendered)
|
||||
return {
|
||||
**excerpt_view(user_body, start, end, label, rendered),
|
||||
"user_start": span[0] if span else None,
|
||||
"user_end": span[1] if span else None,
|
||||
"cue": (_prev_word(user_body, span[0]) or _CUE_BARE) if span else _CUE_BARE,
|
||||
}
|
||||
|
||||
|
||||
def get_cue_decision(profile_id: str, label: str, cue: str) -> str | None:
|
||||
key = normalize_label(label) or (label or "").strip()
|
||||
token = (cue or _CUE_BARE).strip().casefold() or _CUE_BARE
|
||||
if not key:
|
||||
return None
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT decision FROM label_sense_cues
|
||||
WHERE profile_id = ? AND lower(normalized_label) = lower(?) AND cue = ?
|
||||
""",
|
||||
(profile_id, key, token),
|
||||
).fetchone()
|
||||
)
|
||||
value = ((row or {}).get("decision") or "").strip()
|
||||
return value if value in {_DECISION_IDENTITY, _DECISION_NOT} else None
|
||||
|
||||
|
||||
def record_cue(profile_id: str, label: str, cue: str, decision: str) -> None:
|
||||
key = normalize_label(label) or (label or "").strip()
|
||||
token = (cue or _CUE_BARE).strip().casefold() or _CUE_BARE
|
||||
if not key or decision not in {_DECISION_IDENTITY, _DECISION_NOT}:
|
||||
return
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO label_sense_cues (
|
||||
profile_id, normalized_label, cue, decision, hits, updated
|
||||
)
|
||||
VALUES (?, ?, ?, ?, 1, datetime('now'))
|
||||
ON CONFLICT(profile_id, normalized_label, cue) DO UPDATE SET
|
||||
decision = excluded.decision,
|
||||
hits = label_sense_cues.hits + 1,
|
||||
updated = datetime('now')
|
||||
""",
|
||||
(profile_id, key, token, decision),
|
||||
)
|
||||
|
||||
|
||||
def decision_for_mention(
|
||||
profile_id: str,
|
||||
label: str,
|
||||
user_body: str,
|
||||
start: int | None,
|
||||
end: int | None,
|
||||
rendered: str = "",
|
||||
) -> str:
|
||||
"""identity / not_identity from prior review, or ask once for a new cue."""
|
||||
if not label:
|
||||
return _DECISION_ASK
|
||||
cue = cue_for_mention(user_body, start, end, label, rendered)
|
||||
learned = get_cue_decision(profile_id, label, cue)
|
||||
if learned:
|
||||
return learned
|
||||
sense = get_sense(profile_id, label)
|
||||
identity_shaped = cue in KINSHIP
|
||||
if sense["ambiguous"]:
|
||||
return _DECISION_ASK
|
||||
if sense["identity_hits"] > 0 and not sense["non_identity_hits"]:
|
||||
return _DECISION_IDENTITY
|
||||
if sense["non_identity_hits"] > 0 and not sense["identity_hits"]:
|
||||
return _DECISION_ASK if identity_shaped else _DECISION_NOT
|
||||
return _DECISION_ASK
|
||||
|
||||
|
||||
def apply_learned_decisions(
|
||||
profile_id: str,
|
||||
candidates: list[dict],
|
||||
mappings: list[dict],
|
||||
user_body: str,
|
||||
rendered: str = "",
|
||||
) -> tuple[list[dict], list[dict]]:
|
||||
remaining = []
|
||||
for item in candidates:
|
||||
verdict = decision_for_mention(
|
||||
profile_id,
|
||||
item.get("text") or "",
|
||||
user_body,
|
||||
item.get("user_start", item.get("start")),
|
||||
item.get("user_end", item.get("end")),
|
||||
rendered,
|
||||
)
|
||||
if verdict == _DECISION_ASK:
|
||||
remaining.append(item)
|
||||
kept = []
|
||||
for mapping in mappings:
|
||||
label = mapping.get("local_label") or ""
|
||||
verdict = decision_for_mention(
|
||||
profile_id,
|
||||
label,
|
||||
user_body,
|
||||
mapping.get("start"),
|
||||
mapping.get("end"),
|
||||
rendered,
|
||||
)
|
||||
if verdict == _DECISION_NOT:
|
||||
continue
|
||||
kept.append(mapping)
|
||||
if verdict == _DECISION_IDENTITY:
|
||||
kind = mapping.get("entity_type") if mapping.get("entity_type") in ENTITY_TYPES else "PERSON"
|
||||
try:
|
||||
confirm_identity(profile_id, label, entity_type=kind)
|
||||
except ValueError:
|
||||
continue
|
||||
return remaining, kept
|
||||
|
||||
|
||||
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()
|
||||
@@ -214,15 +404,15 @@ def supplement_kinship_candidates(
|
||||
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
|
||||
span = _resolve_user_span(
|
||||
user_body,
|
||||
item.get("start"),
|
||||
item.get("end"),
|
||||
item.get("text") or "",
|
||||
rendered,
|
||||
)
|
||||
if span:
|
||||
occupied.add((span[0], span[1], (item.get("text") or "").casefold()))
|
||||
extra_mappings = list(mappings)
|
||||
extra_candidates = list(candidates)
|
||||
for match in _word_matches(user_body):
|
||||
@@ -232,6 +422,11 @@ def supplement_kinship_candidates(
|
||||
user_key = (match.start(), match.end(), label.casefold())
|
||||
if user_key in occupied:
|
||||
continue
|
||||
if (
|
||||
decision_for_mention(profile_id, label, user_body, match.start(), match.end(), rendered)
|
||||
!= _DECISION_ASK
|
||||
):
|
||||
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
|
||||
@@ -242,7 +437,7 @@ def supplement_kinship_candidates(
|
||||
"entity_type": "PERSON",
|
||||
"start": start,
|
||||
"end": end,
|
||||
"excerpt": _excerpt(user_body, match.start(), match.end(), label),
|
||||
**_mention_fields(user_body, match.start(), match.end(), label, rendered),
|
||||
"ambiguous": get_sense(profile_id, label)["ambiguous"],
|
||||
"suggested": _DECISION_IDENTITY if identity_shaped else _DECISION_NOT,
|
||||
}
|
||||
@@ -264,11 +459,11 @@ def supplement_kinship_candidates(
|
||||
return extra_candidates, extra_mappings
|
||||
|
||||
|
||||
def build_candidates(profile_id: str, mappings: list[dict], user_body: str) -> list[dict]:
|
||||
def build_candidates(profile_id: str, mappings: list[dict], user_body: str, rendered: str = "") -> list[dict]:
|
||||
seen: set[tuple[int | None, int | None, str]] = set()
|
||||
items: list[dict] = []
|
||||
for mapping in mappings:
|
||||
if not needs_review(profile_id, mapping, user_body):
|
||||
if not needs_review(profile_id, mapping, user_body, rendered):
|
||||
continue
|
||||
label = mapping.get("local_label") or ""
|
||||
start = mapping.get("start")
|
||||
@@ -284,7 +479,7 @@ def build_candidates(profile_id: str, mappings: list[dict], user_body: str) -> l
|
||||
"entity_type": mapping.get("entity_type") or "PERSON",
|
||||
"start": start,
|
||||
"end": end,
|
||||
"excerpt": _excerpt(user_body, start, end, label),
|
||||
**_mention_fields(user_body, start, end, label, rendered),
|
||||
"ambiguous": get_sense(profile_id, label)["ambiguous"],
|
||||
}
|
||||
)
|
||||
@@ -369,11 +564,19 @@ def apply_review_decisions(profile_id: str, pending: dict, decisions: list[dict]
|
||||
decision = (raw.get("decision") or "").strip()
|
||||
label = candidate.get("text") or ""
|
||||
kind = candidate.get("entity_type") if candidate.get("entity_type") in ENTITY_TYPES else "PERSON"
|
||||
cue = candidate.get("cue") or cue_for_mention(
|
||||
pending.get("user_body") or "",
|
||||
candidate.get("user_start", candidate.get("start")),
|
||||
candidate.get("user_end", candidate.get("end")),
|
||||
label,
|
||||
)
|
||||
if decision == _DECISION_IDENTITY:
|
||||
record_sense(profile_id, label, identity=True)
|
||||
record_cue(profile_id, label, cue, _DECISION_IDENTITY)
|
||||
confirm_identity(profile_id, label, entity_type=kind)
|
||||
elif decision == _DECISION_NOT:
|
||||
record_sense(profile_id, label, identity=False)
|
||||
record_cue(profile_id, label, cue, _DECISION_NOT)
|
||||
drop_keys.add((candidate.get("start"), candidate.get("end"), label.casefold()))
|
||||
mappings = []
|
||||
for mapping in pending.get("mappings") or []:
|
||||
@@ -459,9 +662,11 @@ def auto_resolve_ambiguous(profile_id: str, candidates: list[dict], mappings: li
|
||||
decision = try_local_passage_decision(item.get("excerpt") or "", label)
|
||||
if decision == _DECISION_NOT:
|
||||
record_sense(profile_id, label, identity=False)
|
||||
record_cue(profile_id, label, item.get("cue") or _CUE_BARE, _DECISION_NOT)
|
||||
drop_keys.add((item.get("start"), item.get("end"), label.casefold()))
|
||||
elif decision == _DECISION_IDENTITY:
|
||||
record_sense(profile_id, label, identity=True)
|
||||
record_cue(profile_id, label, item.get("cue") or _CUE_BARE, _DECISION_IDENTITY)
|
||||
else:
|
||||
remaining.append(item)
|
||||
if not drop_keys:
|
||||
|
||||
Reference in new issue
Block a user