refactor(scoring): 重构评分系统使用统一API替代插件内部实现
将原有的兴趣度评分系统重构为使用统一的评分API,移除了插件内部实现并更新了所有相关引用。主要变更包括: - 替换 chatter_interest_scoring_system 为 scoring_api - 移除 interest_scoring.py 文件,统一使用 src/plugin_system/apis/scoring_api - 更新关系追踪器以使用统一API,保持向后兼容性 - 简化了多个模块中的关系分获取逻辑
This commit is contained in:
108
src/plugin_system/services/interest_service.py
Normal file
108
src/plugin_system/services/interest_service.py
Normal file
@@ -0,0 +1,108 @@
|
||||
"""
|
||||
兴趣系统服务
|
||||
提供独立的兴趣管理功能,不依赖任何插件
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from src.chat.interest_system import bot_interest_manager
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger("interest_service")
|
||||
|
||||
|
||||
class InterestService:
|
||||
"""兴趣系统服务 - 独立于插件的兴趣管理"""
|
||||
|
||||
def __init__(self):
|
||||
self.is_initialized = bot_interest_manager.is_initialized
|
||||
|
||||
async def initialize_smart_interests(self, personality_description: str, personality_id: str = "default"):
|
||||
"""
|
||||
初始化智能兴趣系统
|
||||
|
||||
Args:
|
||||
personality_description: 机器人性格描述
|
||||
personality_id: 性格ID
|
||||
"""
|
||||
try:
|
||||
logger.info("开始初始化智能兴趣系统...")
|
||||
logger.info(f"人设ID: {personality_id}, 描述长度: {len(personality_description)}")
|
||||
|
||||
await bot_interest_manager.initialize(personality_description, personality_id)
|
||||
self.is_initialized = True
|
||||
logger.info("智能兴趣系统初始化完成。")
|
||||
|
||||
# 显示初始化后的统计信息
|
||||
stats = bot_interest_manager.get_interest_stats()
|
||||
logger.info(f"兴趣系统统计: {stats}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"初始化智能兴趣系统失败: {e}")
|
||||
self.is_initialized = False
|
||||
|
||||
async def calculate_interest_match(self, content: str, keywords: Optional[list[str]] = None):
|
||||
"""
|
||||
计算内容与兴趣的匹配度
|
||||
|
||||
Args:
|
||||
content: 消息内容
|
||||
keywords: 关键词列表
|
||||
|
||||
Returns:
|
||||
匹配结果
|
||||
"""
|
||||
if not self.is_initialized:
|
||||
logger.warning("兴趣系统未初始化,无法计算匹配度")
|
||||
return None
|
||||
|
||||
try:
|
||||
if not keywords:
|
||||
# 如果没有关键词,尝试从内容提取
|
||||
keywords = self._extract_keywords_from_content(content)
|
||||
|
||||
return await bot_interest_manager.calculate_interest_match(content, keywords)
|
||||
except Exception as e:
|
||||
logger.error(f"计算兴趣匹配度失败: {e}")
|
||||
return None
|
||||
|
||||
def _extract_keywords_from_content(self, content: str) -> list[str]:
|
||||
"""从内容中提取关键词"""
|
||||
import re
|
||||
|
||||
# 清理文本
|
||||
content = re.sub(r"[^\w\s\u4e00-\u9fff]", " ", content) # 保留中文、英文、数字
|
||||
words = content.split()
|
||||
|
||||
# 过滤和关键词提取
|
||||
keywords = []
|
||||
for word in words:
|
||||
word = word.strip()
|
||||
if (
|
||||
len(word) >= 2 # 至少2个字符
|
||||
and word.isalnum() # 字母数字
|
||||
and not word.isdigit()
|
||||
): # 不是纯数字
|
||||
keywords.append(word.lower())
|
||||
|
||||
# 去重并限制数量
|
||||
unique_keywords = list(set(keywords))
|
||||
return unique_keywords[:10] # 返回前10个唯一关键词
|
||||
|
||||
def get_interest_stats(self) -> dict:
|
||||
"""获取兴趣系统统计信息"""
|
||||
if not self.is_initialized:
|
||||
return {"initialized": False}
|
||||
|
||||
try:
|
||||
return {
|
||||
"initialized": True,
|
||||
**bot_interest_manager.get_interest_stats()
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"获取兴趣系统统计失败: {e}")
|
||||
return {"initialized": True, "error": str(e)}
|
||||
|
||||
|
||||
# 创建全局实例
|
||||
interest_service = InterestService()
|
||||
Reference in New Issue
Block a user