88 lines
3.1 KiB
Python
88 lines
3.1 KiB
Python
"""Compare recommendations from two mood presets.
|
|
|
|
python tools/compare_presets.py --a dark-fantasy --b story-heavy
|
|
python tools/compare_presets.py --a colony-management --b city-builder --limit 15 --json
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
for stream in (sys.stdout, sys.stderr):
|
|
try:
|
|
stream.reconfigure(encoding="utf-8")
|
|
except Exception:
|
|
pass
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import db
|
|
import reco
|
|
|
|
|
|
def compare_lists(first, second):
|
|
ids_a = [game["appid"] for game in first]
|
|
ids_b = [game["appid"] for game in second]
|
|
set_a, set_b = set(ids_a), set(ids_b)
|
|
common = set_a & set_b
|
|
union = set_a | set_b
|
|
return {
|
|
"common": [appid for appid in ids_a if appid in common],
|
|
"only_a": [appid for appid in ids_a if appid not in set_b],
|
|
"only_b": [appid for appid in ids_b if appid not in set_a],
|
|
"jaccard": round(len(common) / len(union), 4) if union else 0.0,
|
|
}
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser(description="Compare two mood presets")
|
|
ap.add_argument("--db", default=db.DEFAULT_DB)
|
|
ap.add_argument("--a", required=True)
|
|
ap.add_argument("--b", required=True)
|
|
ap.add_argument("--limit", type=int, default=20)
|
|
ap.add_argument("--json", action="store_true")
|
|
args = ap.parse_args()
|
|
|
|
con = db.connect(args.db)
|
|
db.init(con)
|
|
try:
|
|
mood_a = reco.load_mood(con, preset=args.a)
|
|
mood_b = reco.load_mood(con, preset=args.b)
|
|
_, results_a = reco.recommend(con, mood_a, limit=args.limit)
|
|
_, results_b = reco.recommend(con, mood_b, limit=args.limit)
|
|
except KeyError as exc:
|
|
raise SystemExit(f"unknown preset: {exc.args[0]}")
|
|
|
|
names = {row["appid"]: row["name"] for row in con.execute("SELECT appid, name FROM games")}
|
|
con.close()
|
|
comparison = compare_lists(results_a, results_b)
|
|
|
|
if args.json:
|
|
print(json.dumps({
|
|
"a": args.a, "b": args.b, "limit": args.limit,
|
|
"jaccard": comparison["jaccard"],
|
|
"common": [{"appid": appid, "name": names.get(appid)} for appid in comparison["common"]],
|
|
"only_a": [{"appid": appid, "name": names.get(appid)} for appid in comparison["only_a"]],
|
|
"only_b": [{"appid": appid, "name": names.get(appid)} for appid in comparison["only_b"]],
|
|
"results_a": [{"appid": g["appid"], "name": g["name"], "score": g["score"]} for g in results_a],
|
|
"results_b": [{"appid": g["appid"], "name": g["name"], "score": g["score"]} for g in results_b],
|
|
}, ensure_ascii=False, indent=2))
|
|
return
|
|
|
|
print(f"{args.a} vs {args.b} (top {args.limit})")
|
|
print(f"common: {len(comparison['common'])} | Jaccard: {comparison['jaccard']:.3f}")
|
|
print(f"\nТолько {args.a}:")
|
|
for appid in comparison["only_a"]:
|
|
print(f" {names.get(appid, appid)}")
|
|
print(f"\nТолько {args.b}:")
|
|
for appid in comparison["only_b"]:
|
|
print(f" {names.get(appid, appid)}")
|
|
print("\nОбщие:")
|
|
for appid in comparison["common"]:
|
|
print(f" {names.get(appid, appid)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|