Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(entity): 人物合并只把身份名称留作别名

longmeidao24a777b33b21
diff --git a/src/peach/classification.py b/src/peach/classification.py
index 550dbe466..77cdd8158 100644
--- a/src/peach/classification.py
+++ b/src/peach/classification.py
@@ -99,7 +99,7 @@ def is_repost_creator(name: str | None) -> bool:
_COLLECTION_SUFFIX = re.compile(
- r'(?:\s*(?:4k|6k|8k|去重版|整合用|合集|合辑|全集)'
+ r'(?:\s*(?:4k|6k|8k|2160p|1080p|720p|去重版|整合用|合集|合辑|全集)'
r'|\s*(?:20\d{2}[.年_-](?:0?[1-9]|1[0-2])(?:月)?'
r'|(?:0?[1-9]|1[0-2])月|20\d{2}年[一二三四五六七八九十]+月'
r'|\d{4}\s*[一二三四五六七八九十]+月)'
diff --git a/src/peach/entities.py b/src/peach/entities.py
index ce02bfca6..ab4407164 100644
--- a/src/peach/entities.py
+++ b/src/peach/entities.py
@@ -195,6 +195,21 @@ def strip_zero_width(name: str) -> str:
return str(name or "").translate(ZERO_WIDTH)
+def is_identity_alias(kind: str, name: str | None) -> bool:
+ """人物别名只承载身份名称;画质、月份、合集注记、题材词与番号形态留在文件信息里。"""
+ text = strip_zero_width(name).strip()
+ if not text:
+ return False
+ if kind not in PERSON_ENTITY_KINDS:
+ return True
+ from .catalog_rules import normalise_code_key, release_code_from_text
+ from .classification import creator_collection_base, is_structural_creator
+ if creator_collection_base(text) != text or is_structural_creator(text):
+ return False
+ code = release_code_from_text(text)
+ return not (code and normalise_code_key(text) == normalise_code_key(code))
+
+
def canonicalize_entity_name(kind: str, name: str | None) -> str:
canonical = strip_zero_width(name).strip()
if kind == 'creator':
@@ -263,8 +278,10 @@ def merge_entity(
"""
if source_id == target_id:
raise ValueError("实体不能并入自己")
- if connection.execute("SELECT 1 FROM entity WHERE id=?", (target_id,)).fetchone() is None:
+ target = connection.execute("SELECT kind FROM entity WHERE id=?", (target_id,)).fetchone()
+ if target is None:
raise ValueError(f"合并目标实体 {target_id} 不存在")
+ target_kind = str(target[0])
stamp = now or datetime.now(timezone.utc).isoformat()
moved = {"assets": 0, "aliases": 0, "refs": 0, "links": 0, "terms": 0,
"dropped_refs": 0, "memberships": 0, "members": 0, "labels": 0, "profiles": 0,
@@ -272,18 +289,20 @@ def merge_entity(
from .entity_classification import transfer
transfer(connection, source_id, target_id)
- # 被并入的名字本身留作别名,否则按旧名搜索会落空。
- connection.execute(
- "INSERT OR IGNORE INTO entity_alias(entity_id,alias,normalized_alias,source,confidence)"
- " VALUES(?,?,?,?,1.0)",
- (target_id, source_name, normalize_entity_name(source_name), alias_source),
- )
- connection.execute(
- "INSERT OR IGNORE INTO entity_alias"
- " SELECT ?,alias,normalized_alias,source,confidence FROM entity_alias WHERE entity_id=?",
- (target_id, source_id),
- )
- moved["aliases"] = connection.execute("SELECT changes()").fetchone()[0]
+ # 被并入的名字本身留作别名,否则按旧名搜索会落空;人物的画质、合集与题材写法
+ # 只留墓碑,旧 id 仍按墓碑解析。
+ aliases = [(source_name, normalize_entity_name(source_name), alias_source, 1.0)]
+ aliases += connection.execute(
+ "SELECT alias,normalized_alias,source,confidence FROM entity_alias WHERE entity_id=?",
+ (source_id,)).fetchall()
+ for index, (alias, normalized, source, confidence) in enumerate(aliases):
+ if not is_identity_alias(target_kind, alias):
+ continue
+ connection.execute(
+ "INSERT OR IGNORE INTO entity_alias(entity_id,alias,normalized_alias,source,confidence)"
+ " VALUES(?,?,?,?,?)", (target_id, alias, normalized, source, confidence))
+ if index:
+ moved["aliases"] += connection.execute("SELECT changes()").fetchone()[0]
connection.execute("DELETE FROM entity_alias WHERE entity_id=?", (source_id,))
connection.execute(
diff --git a/tests/test_entity_merge.py b/tests/test_entity_merge.py
index 5627d1a6e..362f73c6e 100644
--- a/tests/test_entity_merge.py
+++ b/tests/test_entity_merge.py
@@ -69,6 +69,29 @@ class EntityMergeTests(unittest.TestCase):
self.con.commit()
self.assertEqual(moved["aliases"], 1, "source 的别名应被迁移并计数")
+ def test_person_merge_keeps_identity_names_and_drops_collection_spellings(self):
+ for name in ("新ありな4K", "新ありな 合集", "黑丝美腿", "ABP-123"):
+ self.con.execute(
+ "INSERT INTO entity_alias(entity_id,alias,normalized_alias,source,confidence)"
+ " VALUES(11,?,?,'r18',1.0)", (name, normalize_entity_name(name)))
+ self.con.execute(
+ "INSERT INTO entity_alias(entity_id,alias,normalized_alias,source,confidence)"
+ " VALUES(11,'ありな','ありな','r18',1.0)")
+ merge_entity(self.con, target_id=10, source_id=11,
+ source_name="新ありな 1080p", alias_source="user:review")
+ aliases = {r[0] for r in self.con.execute("SELECT alias FROM entity_alias WHERE entity_id=10")}
+ self.assertEqual(aliases, {"ありな"})
+ self.assertEqual(self.con.execute("SELECT target_id FROM entity_redirect WHERE old_id=11").fetchone(), (10,))
+
+ def test_tag_merge_keeps_the_retired_name_as_alias(self):
+ for entity_id, name in ((20, "合集"), (21, "混合集")):
+ self.con.execute(
+ "INSERT INTO entity(id,kind,canonical_name,normalized_name,created_at,updated_at)"
+ " VALUES(?,'tag',?,?,'t','t')", (entity_id, name, name))
+ merge_entity(self.con, target_id=20, source_id=21, source_name="混合集", alias_source="retired")
+ self.assertEqual(self.con.execute("SELECT alias FROM entity_alias WHERE entity_id=20").fetchall(),
+ [("混合集",)])
+
def test_two_pages_on_the_same_site_both_follow_the_merge(self):
"""同一个站上的两个页面是两条引用,合并后都归目标实体(0032)。
diff --git a/tests/test_metadata_creator_attributions.py b/tests/test_metadata_creator_attributions.py
index a89579961..42ee67476 100644
--- a/tests/test_metadata_creator_attributions.py
+++ b/tests/test_metadata_creator_attributions.py
@@ -110,7 +110,7 @@ class CreatorAttributionTests(unittest.TestCase):
def test_collection_annotations_reduce_to_the_account_name(self):
for name in ('someone 合集', 'someone合辑', 'someone全集', 'someone最新12v', 'someone 30v 40g',
- 'someone V12 80GB', 'someone(2)', 'someone 去重版 4K', 'someone 120GB'):
+ 'someone V12 80GB', 'someone(2)', 'someone 去重版 4K', 'someone 120GB', 'someone 1080p'):
with self.subTest(name=name):
self.assertEqual(creator_collection_base(name), 'someone')
for name in ('someone 2509', 'someone', 'v12', '合集'):