feat: Phase 2 交互与纠错工作台完成
- 新增实体编辑 API (名称、类型、定义、别名) - 新增实体删除和合并功能 - 新增关系管理 (创建、删除) - 新增转录文本编辑功能 - 新增划词创建实体功能 - 前端新增实体编辑器模态框 - 前端新增右键菜单和工具栏 - 文本与图谱双向联动优化
This commit is contained in:
@@ -290,6 +290,104 @@ class DatabaseManager:
|
||||
).fetchall()
|
||||
conn.close()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
def update_entity(self, entity_id: str, **kwargs) -> Entity:
|
||||
"""更新实体信息"""
|
||||
conn = self.get_conn()
|
||||
|
||||
# 构建更新字段
|
||||
allowed_fields = ['name', 'type', 'definition', 'canonical_name']
|
||||
updates = []
|
||||
values = []
|
||||
|
||||
for field in allowed_fields:
|
||||
if field in kwargs:
|
||||
updates.append(f"{field} = ?")
|
||||
values.append(kwargs[field])
|
||||
|
||||
# 处理别名
|
||||
if 'aliases' in kwargs:
|
||||
updates.append("aliases = ?")
|
||||
values.append(json.dumps(kwargs['aliases']))
|
||||
|
||||
if not updates:
|
||||
conn.close()
|
||||
return self.get_entity(entity_id)
|
||||
|
||||
updates.append("updated_at = ?")
|
||||
values.append(datetime.now().isoformat())
|
||||
values.append(entity_id)
|
||||
|
||||
query = f"UPDATE entities SET {', '.join(updates)} WHERE id = ?"
|
||||
conn.execute(query, values)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return self.get_entity(entity_id)
|
||||
|
||||
def delete_entity(self, entity_id: str):
|
||||
"""删除实体及其关联数据"""
|
||||
conn = self.get_conn()
|
||||
|
||||
# 删除提及记录
|
||||
conn.execute("DELETE FROM entity_mentions WHERE entity_id = ?", (entity_id,))
|
||||
|
||||
# 删除关系
|
||||
conn.execute("DELETE FROM entity_relations WHERE source_entity_id = ? OR target_entity_id = ?",
|
||||
(entity_id, entity_id))
|
||||
|
||||
# 删除实体
|
||||
conn.execute("DELETE FROM entities WHERE id = ?", (entity_id,))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def delete_relation(self, relation_id: str):
|
||||
"""删除关系"""
|
||||
conn = self.get_conn()
|
||||
conn.execute("DELETE FROM entity_relations WHERE id = ?", (relation_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def update_relation(self, relation_id: str, **kwargs) -> dict:
|
||||
"""更新关系"""
|
||||
conn = self.get_conn()
|
||||
|
||||
allowed_fields = ['relation_type', 'evidence']
|
||||
updates = []
|
||||
values = []
|
||||
|
||||
for field in allowed_fields:
|
||||
if field in kwargs:
|
||||
updates.append(f"{field} = ?")
|
||||
values.append(kwargs[field])
|
||||
|
||||
if updates:
|
||||
query = f"UPDATE entity_relations SET {', '.join(updates)} WHERE id = ?"
|
||||
values.append(relation_id)
|
||||
conn.execute(query, values)
|
||||
conn.commit()
|
||||
|
||||
row = conn.execute("SELECT * FROM entity_relations WHERE id = ?", (relation_id,)).fetchone()
|
||||
conn.close()
|
||||
|
||||
return dict(row) if row else None
|
||||
|
||||
def update_transcript(self, transcript_id: str, full_text: str) -> dict:
|
||||
"""更新转录文本"""
|
||||
conn = self.get_conn()
|
||||
now = datetime.now().isoformat()
|
||||
|
||||
conn.execute(
|
||||
"UPDATE transcripts SET full_text = ?, updated_at = ? WHERE id = ?",
|
||||
(full_text, now, transcript_id)
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
row = conn.execute("SELECT * FROM transcripts WHERE id = ?", (transcript_id,)).fetchone()
|
||||
conn.close()
|
||||
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
# Singleton instance
|
||||
|
||||
Reference in New Issue
Block a user