203 lines
7.3 KiB
Rust
203 lines
7.3 KiB
Rust
//! 推荐引擎 — 标签推荐、人物推荐、关联推荐
|
|
//!
|
|
//! 推荐策略:
|
|
//! 1. 标签推荐:基于输入前缀匹配 + 热门度排序
|
|
//! 2. 人物推荐:基于名称匹配 + 相册数排序
|
|
//! 3. 关联推荐:基于标签共现关系 + 人物共现关系
|
|
|
|
use crate::db::Database;
|
|
use crate::models::*;
|
|
use sqlx::Row;
|
|
|
|
/// 推荐引擎
|
|
pub struct RecommendEngine;
|
|
|
|
impl RecommendEngine {
|
|
/// 标签自动补全推荐
|
|
pub async fn recommend_tags(
|
|
db: &Database,
|
|
query: &str,
|
|
limit: usize,
|
|
) -> RecommendResponse {
|
|
let limit = limit.min(20);
|
|
let q = query.trim().to_lowercase();
|
|
|
|
let tags = db.get_popular_tags(200).await;
|
|
let mut items: Vec<RecommendItem> = Vec::new();
|
|
|
|
for t in &tags {
|
|
let tag_lower = t.tag.to_lowercase();
|
|
let score = if q.is_empty() {
|
|
// 无查询时按热门度
|
|
t.count as f64
|
|
} else if tag_lower == q {
|
|
// 精确匹配
|
|
1000.0 + t.count as f64
|
|
} else if tag_lower.starts_with(&q) {
|
|
// 前缀匹配
|
|
500.0 + t.count as f64
|
|
} else if tag_lower.contains(&q) {
|
|
// 包含匹配
|
|
100.0 + t.count as f64
|
|
} else {
|
|
continue;
|
|
};
|
|
|
|
items.push(RecommendItem {
|
|
value: t.tag.clone(),
|
|
label: format!("{} ({})", t.tag, t.count),
|
|
score,
|
|
count: Some(t.count),
|
|
source: "tag".to_string(),
|
|
});
|
|
}
|
|
|
|
items.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
|
|
let total = items.len();
|
|
items.truncate(limit);
|
|
|
|
RecommendResponse { items, total, category: "tags".to_string() }
|
|
}
|
|
|
|
/// 人物推荐
|
|
pub async fn recommend_persons(
|
|
db: &Database,
|
|
query: &str,
|
|
limit: usize,
|
|
) -> RecommendResponse {
|
|
let limit = limit.min(20);
|
|
let q = query.trim().to_lowercase();
|
|
|
|
let persons = db.get_all_persons_with_aliases().await;
|
|
let mut items: Vec<RecommendItem> = Vec::new();
|
|
|
|
for p in &persons {
|
|
let name_lower = p.name.to_lowercase();
|
|
let score = if q.is_empty() {
|
|
p.total_albums as f64
|
|
} else if name_lower == q {
|
|
1000.0 + p.total_albums as f64
|
|
} else if name_lower.starts_with(&q) {
|
|
500.0 + p.total_albums as f64
|
|
} else if name_lower.contains(&q) {
|
|
100.0 + p.total_albums as f64
|
|
} else {
|
|
// 检查别名
|
|
let alias_match = p.aliases.iter().any(|a| a.to_lowercase().contains(&q));
|
|
if alias_match { 200.0 + p.total_albums as f64 } else { continue }
|
|
};
|
|
|
|
let label = if p.aliases.is_empty() {
|
|
format!("{} ({}相册)", p.name, p.total_albums)
|
|
} else {
|
|
format!("{} [{}] ({}相册)", p.name, p.aliases.join(", "), p.total_albums)
|
|
};
|
|
|
|
items.push(RecommendItem {
|
|
value: p.name.clone(),
|
|
label,
|
|
score,
|
|
count: Some(p.total_albums),
|
|
source: "person".to_string(),
|
|
});
|
|
}
|
|
|
|
items.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
|
|
let total = items.len();
|
|
items.truncate(limit);
|
|
|
|
RecommendResponse { items, total, category: "persons".to_string() }
|
|
}
|
|
|
|
/// 关联推荐(共现标签 + 共现人物)
|
|
pub async fn recommend_related(
|
|
db: &Database,
|
|
context: &str, // 标签或人名
|
|
limit: usize,
|
|
) -> TagRecommendation {
|
|
let limit = limit.min(15);
|
|
|
|
// 获取共现标签
|
|
let co_tags = db.get_tag_co_occurrences(context, limit as i64).await;
|
|
let related_tags: Vec<RecommendItem> = co_tags.iter().map(|c| {
|
|
RecommendItem {
|
|
value: c.co_tag.clone(),
|
|
label: format!("{} (共现{}次)", c.co_tag, c.count),
|
|
score: c.score,
|
|
count: Some(c.count),
|
|
source: "co-occurrence".to_string(),
|
|
}
|
|
}).collect();
|
|
|
|
// 获取相关人物(通过标签)
|
|
let albums = db.search_albums_by_tag(context).await;
|
|
let mut person_scores: std::collections::HashMap<String, (i64, f64)> = std::collections::HashMap::new();
|
|
let total = albums.len() as f64;
|
|
for album in &albums {
|
|
let meta = db.get_all_albums_full().await;
|
|
for m in &meta {
|
|
if m.id == album.id.to_string() {
|
|
if let Some(ref name) = m.person_name_cleaned {
|
|
let entry = person_scores.entry(name.clone()).or_insert((0, 0.0));
|
|
entry.0 += 1;
|
|
entry.1 = entry.0 as f64 / total;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut similar_persons: Vec<RecommendItem> = person_scores.into_iter()
|
|
.filter(|(_, (cnt, _))| *cnt >= 1)
|
|
.map(|(name, (cnt, score))| RecommendItem {
|
|
value: name.clone(),
|
|
label: format!("{} ({}次)", name, cnt),
|
|
score,
|
|
count: Some(cnt),
|
|
source: "co-occurrence".to_string(),
|
|
}).collect();
|
|
similar_persons.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
|
|
similar_persons.truncate(limit);
|
|
|
|
TagRecommendation {
|
|
tag: context.to_string(),
|
|
related_tags,
|
|
similar_persons,
|
|
}
|
|
}
|
|
|
|
/// 热门人物
|
|
pub async fn popular_persons(db: &Database, limit: usize) -> Vec<PopularPerson> {
|
|
let persons = db.get_all_persons_with_aliases().await;
|
|
let mut results: Vec<PopularPerson> = persons.iter().take(limit).map(|p| {
|
|
PopularPerson {
|
|
name: p.name.clone(),
|
|
display_name: p.display_name.clone(),
|
|
total_albums: p.total_albums,
|
|
total_images: p.total_images,
|
|
top_tags: Vec::new(),
|
|
aliases: p.aliases.clone(),
|
|
}
|
|
}).collect();
|
|
|
|
// 为热门人物获取 top tags
|
|
// (简化:从 person_albums 查标签)
|
|
for rp in &mut results {
|
|
if let Some(person) = db.get_person_by_name(&rp.name).await {
|
|
let albums = db.get_person_albums(person.id).await;
|
|
let mut tag_counts: std::collections::HashMap<String, i64> = std::collections::HashMap::new();
|
|
for album in &albums {
|
|
let aid = album.id.to_string();
|
|
let rows = sqlx::query("SELECT tag FROM album_tags WHERE album_id = ?")
|
|
.bind(&aid).fetch_all(&db.pool).await.unwrap_or_default();
|
|
for row in &rows {
|
|
*tag_counts.entry(row.get::<String, _>("tag")).or_insert(0) += 1;
|
|
}
|
|
}
|
|
let mut tags: Vec<(String, i64)> = tag_counts.into_iter().collect();
|
|
tags.sort_by(|a, b| b.1.cmp(&a.1));
|
|
rp.top_tags = tags.into_iter().take(5).map(|(t, _)| t).collect();
|
|
}
|
|
}
|
|
results
|
|
}
|
|
} |