1 parent
f25d218756
commit
044d0279b3
17 files changed
+288
-20
No files matched your search
+17
-1
@@ -8,7 +8,7 @@ from datetime import datetime, timezone
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
DEFAULT_DB = os.path.join(BASE_DIR, "steam.db")
|
||||
SCHEMA_PATH = os.path.join(BASE_DIR, "tools", "schema.sql")
|
||||
SCHEMA_VERSION = 2
|
||||
SCHEMA_VERSION = 3
|
||||
|
||||
MOOD_PRESETS = {
|
||||
"adventure-rpg-party": {
|
||||
@@ -329,9 +329,25 @@ def _migration_2(con):
|
||||
refresh_search_index(con)
|
||||
|
||||
|
||||
def _migration_3(con):
|
||||
"""Add per-mood recommendation feedback."""
|
||||
con.execute("""
|
||||
CREATE TABLE IF NOT EXISTS reco_feedback (
|
||||
appid INTEGER NOT NULL REFERENCES games(appid) ON DELETE CASCADE,
|
||||
mood_key TEXT NOT NULL,
|
||||
verdict TEXT NOT NULL CHECK (verdict IN ('like','dislike','played','ignore')),
|
||||
created_at TEXT NOT NULL,
|
||||
PRIMARY KEY (appid, mood_key)
|
||||
)
|
||||
""")
|
||||
con.execute("CREATE INDEX IF NOT EXISTS idx_reco_feedback_mood "
|
||||
"ON reco_feedback(mood_key, verdict)")
|
||||
|
||||
|
||||
MIGRATIONS = {
|
||||
1: _migration_1,
|
||||
2: _migration_2,
|
||||
3: _migration_3,
|
||||
}
|
||||
|
||||
|
||||
|
||||
+30
-1
@@ -1,5 +1,6 @@
|
||||
"""Mood parsing and scoring shared by the CLI and the web UI."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
|
||||
@@ -78,6 +79,14 @@ def mood_from_text(text):
|
||||
return {"description": text.strip(), "genres": genres, "tags": tags}
|
||||
|
||||
|
||||
def mood_key(mood):
|
||||
"""Stable key for feedback: preset name or normalized text hash."""
|
||||
if mood.get("preset"):
|
||||
return "preset:" + mood["preset"]
|
||||
text = (mood.get("description") or "").strip().lower()
|
||||
return "text:" + hashlib.sha1(text.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def load_mood(con, preset=None, text=None):
|
||||
if preset:
|
||||
row = con.execute("SELECT * FROM moods WHERE name = ?", (preset,)).fetchone()
|
||||
@@ -95,7 +104,7 @@ def load_mood(con, preset=None, text=None):
|
||||
|
||||
|
||||
def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
|
||||
min_review=65, allow_unknown=False, limit=20):
|
||||
min_review=65, allow_unknown=False, limit=20, feedback_key=None):
|
||||
allowed = set(allowed_statuses or {"backlog", "playing", "played"})
|
||||
if not allowed:
|
||||
allowed = {"backlog", "playing", "played"}
|
||||
@@ -124,6 +133,17 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
|
||||
if not allow_unknown:
|
||||
rows = [r for r in rows if r["review_pct"] is not None or r["metacritic"] is not None]
|
||||
|
||||
feedback = {}
|
||||
liked_ids = set()
|
||||
if feedback_key:
|
||||
for r in con.execute(
|
||||
"SELECT appid, verdict FROM reco_feedback WHERE mood_key = ?", (feedback_key,)):
|
||||
feedback[r["appid"]] = r["verdict"]
|
||||
if r["verdict"] == "like":
|
||||
liked_ids.add(r["appid"])
|
||||
rows = [r for r in rows
|
||||
if feedback.get(r["appid"]) not in ("dislike", "ignore", "played")]
|
||||
|
||||
ids = [r["appid"] for r in rows]
|
||||
if not ids:
|
||||
return mood, []
|
||||
@@ -143,6 +163,12 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
|
||||
df[t] = df.get(t, 0) + 1
|
||||
idf = {t: math.log(1 + total / c) for t, c in df.items()}
|
||||
|
||||
liked_tags = set()
|
||||
if liked_ids:
|
||||
liked_ph = ",".join("?" * len(liked_ids))
|
||||
liked_tags = {r["tag_id"] for r in con.execute(
|
||||
f"SELECT tag_id FROM game_tags WHERE appid IN ({liked_ph})", list(liked_ids))}
|
||||
|
||||
mood_tags = {k: float(v) for k, v in mood["tags"].items()}
|
||||
denom = 0.0
|
||||
for name, weight in mood_tags.items():
|
||||
@@ -172,6 +198,8 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
|
||||
score += 2 if r["installed"] else 0
|
||||
score += 3 if r["status"] == "backlog" else 0
|
||||
score += 4 if r["status"] == "playing" else 0
|
||||
if liked_tags:
|
||||
score += min(8.0, 1.5 * len(liked_tags & tags))
|
||||
results.append({
|
||||
"appid": r["appid"],
|
||||
"name": r["name"],
|
||||
@@ -187,6 +215,7 @@ def recommend(con, mood, allowed_statuses=None, unplayed_only=False,
|
||||
"favorite": bool(r["favorite"]),
|
||||
"notes": r["notes"],
|
||||
"release": r["release_date"],
|
||||
"feedback": feedback.get(r["appid"]),
|
||||
})
|
||||
results.sort(key=lambda g: -g["score"])
|
||||
return mood, results[:limit]
|
||||
|
||||
@@ -88,6 +88,15 @@ CREATE TABLE IF NOT EXISTS game_translations (
|
||||
translated_at TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS reco_feedback (
|
||||
appid INTEGER NOT NULL REFERENCES games(appid) ON DELETE CASCADE,
|
||||
mood_key TEXT NOT NULL,
|
||||
verdict TEXT NOT NULL CHECK (verdict IN ('like','dislike','played','ignore')),
|
||||
created_at TEXT NOT NULL,
|
||||
PRIMARY KEY (appid, mood_key)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_reco_feedback_mood ON reco_feedback(mood_key, verdict);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS moods (
|
||||
name TEXT PRIMARY KEY,
|
||||
description TEXT,
|
||||
|
||||
Reference in new issue
Block a user