PPP commited on
Commit
40c24a7
·
1 Parent(s): 8fabe72

fix(validation): avoid false positives for valid item-use phrases

Browse files
Files changed (1) hide show
  1. state_manager.py +31 -12
state_manager.py CHANGED
@@ -1465,9 +1465,22 @@ class GameState:
1465
  details = intent.get("details", "") or ""
1466
  raw_input = intent.get("raw_input", "") or ""
1467
 
1468
- inventory = list(self.player.inventory)
1469
- equipped_items = [v for v in self.player.equipment.values() if v]
1470
- all_owned = set(inventory) | set(equipped_items)
 
 
 
 
 
 
 
 
 
 
 
 
 
1471
 
1472
  # --- 检测 1: USE_ITEM / EQUIP: target 必须在背包或装备中 ---
1473
  if action in ("USE_ITEM", "EQUIP") and target:
@@ -1530,15 +1543,21 @@ class GameState:
1530
  for pattern, verb_desc in extraction_patterns:
1531
  match = re.search(pattern, full_text)
1532
  if match:
1533
- mentioned = match.group(1).strip()
1534
- if not mentioned or mentioned in non_item_words:
1535
- continue
1536
- if mentioned not in all_owned:
1537
- # 模糊匹配:"剑" 可能是 "铁剑" 的简称
1538
- fuzzy_match = any(
1539
- mentioned in owned or owned in mentioned
1540
- for owned in all_owned
1541
- )
 
 
 
 
 
 
1542
  if not fuzzy_match:
1543
  return False, f"你并没有「{mentioned}」,请检查你的背包。"
1544
 
 
1465
  details = intent.get("details", "") or ""
1466
  raw_input = intent.get("raw_input", "") or ""
1467
 
1468
+ inventory = list(self.player.inventory)
1469
+ equipped_items = [v for v in self.player.equipment.values() if v]
1470
+ all_owned = set(inventory) | set(equipped_items)
1471
+
1472
+ def normalize_item_phrase(text: str) -> str:
1473
+ cleaned = str(text or "").strip()
1474
+ cleaned = re.sub(
1475
+ r"^(?:喝掉|吃掉|使用|服用|装备|穿上|戴上|拿出|掏出|拔出|举起|喝|吃|用|掉)",
1476
+ "",
1477
+ cleaned,
1478
+ )
1479
+ cleaned = re.sub(r"^(?:一瓶|一杯|一口|一个|一份|一块|一把)", "", cleaned)
1480
+ cleaned = re.sub(r"(?:照明|攻击|挥舞|挥动|一下|试试)$", "", cleaned)
1481
+ return cleaned.strip()
1482
+
1483
+ normalized_target = normalize_item_phrase(target)
1484
 
1485
  # --- 检测 1: USE_ITEM / EQUIP: target 必须在背包或装备中 ---
1486
  if action in ("USE_ITEM", "EQUIP") and target:
 
1543
  for pattern, verb_desc in extraction_patterns:
1544
  match = re.search(pattern, full_text)
1545
  if match:
1546
+ mentioned = normalize_item_phrase(match.group(1).strip())
1547
+ if not mentioned or mentioned in non_item_words:
1548
+ continue
1549
+ if mentioned not in all_owned:
1550
+ if normalized_target and (
1551
+ mentioned in normalized_target
1552
+ or normalized_target in mentioned
1553
+ ):
1554
+ continue
1555
+ # 模糊匹配:"剑" 可能是 "铁剑" 的简称
1556
+ fuzzy_match = any(
1557
+ mentioned in normalize_item_phrase(owned)
1558
+ or normalize_item_phrase(owned) in mentioned
1559
+ for owned in all_owned
1560
+ )
1561
  if not fuzzy_match:
1562
  return False, f"你并没有「{mentioned}」,请检查你的背包。"
1563