-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
170 lines (146 loc) · 4.82 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
"""Main script & CLI entry point."""
import argparse
import logging
import os
import sys
from board_game_recommender.recommend import BGGRecommender
LOGGER = logging.getLogger(__name__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def _parse_args():
parser = argparse.ArgumentParser(description="train board game recommender model")
parser.add_argument("users", nargs="*", help="users to be recommended games")
parser.add_argument("--model", "-m", help="model directory")
parser.add_argument("--train", "-t", action="store_true", help="train a new model")
parser.add_argument(
"--num-factors",
"-n",
type=int,
default=32,
help="number of latent factors",
)
parser.add_argument(
"--similarity",
"-s",
action="store_true",
help="train a new similarity model",
)
parser.add_argument("--games-file", "-G", help="games file")
parser.add_argument("--ratings-file", "-R", help="ratings file")
parser.add_argument(
"--side-data-columns",
"-S",
nargs="+",
help="game features to use in recommender model",
)
parser.add_argument(
"--num-rec",
"-r",
type=int,
default=10,
help="number of games to recommend",
)
parser.add_argument(
"--max-iterations",
"-M",
type=int,
default=100,
help="maximal number of training steps",
)
parser.add_argument(
"--diversity",
"-d",
type=float,
default=0,
help="diversity in recommendations",
)
parser.add_argument(
"--cooperative",
"-c",
action="store_true",
help="recommend cooperative games",
)
parser.add_argument(
"--games",
"-g",
type=int,
nargs="+",
help="restrict to these games",
)
parser.add_argument("--players", "-p", type=int, help="player count")
parser.add_argument("--complexity", "-C", type=float, nargs="+", help="complexity")
parser.add_argument("--time", "-T", type=float, help="max playing time")
parser.add_argument("--worst", "-w", action="store_true", help="show worst games")
parser.add_argument(
"--verbose",
"-v",
action="count",
default=0,
help="log level (repeat for more verbosity)",
)
return parser.parse_args()
def _main():
args = _parse_args()
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG if args.verbose > 0 else logging.INFO,
format="%(asctime)s %(levelname)-8.8s [%(name)s:%(lineno)s] %(message)s",
)
LOGGER.info(args)
model_cls = BGGRecommender
model_dir = args.model or os.path.join(BASE_DIR, ".bgg")
games_file = args.games_file or os.path.join(
BASE_DIR,
"results",
"bgg_GameItem.jl",
)
ratings_file = args.ratings_file or os.path.join(
BASE_DIR,
"results",
"bgg_RatingItem.jl",
)
games_filters = {}
if args.cooperative:
games_filters["cooperative"] = True
if args.players:
# TODO min_players, min_players_rec, or min_players_best?
games_filters["min_players__lte"] = args.players
games_filters["max_players__gte"] = args.players
if args.complexity:
if len(args.complexity) == 1:
games_filters["complexity__lte"] = args.complexity[0]
else:
games_filters["complexity__gte"] = args.complexity[0]
games_filters["complexity__lte"] = args.complexity[1]
if args.time:
games_filters["min_time__lte"] = args.time * 1.1
if args.train:
recommender = model_cls.train_from_files(
games_file=games_file,
ratings_file=ratings_file,
side_data_columns=args.side_data_columns,
similarity_model=args.similarity,
num_factors=args.num_factors,
max_iterations=args.max_iterations,
verbose=bool(args.verbose),
)
recommender.save(model_dir)
else:
recommender = model_cls.load(model_dir)
for user in [None] + args.users:
LOGGER.info("#" * 100)
recommendations = recommender.recommend(
users=user,
games=args.games,
games_filters=games_filters,
similarity_model=args.similarity,
num_games=None if args.worst else args.num_rec,
diversity=0 if args.worst else args.diversity,
)
LOGGER.info("best games for <%s>", user or "everyone")
recommendations.print_rows(num_rows=args.num_rec)
if args.worst:
LOGGER.info("worst games for <%s>", user or "everyone")
recommendations.sort("rank", False).print_rows(num_rows=args.num_rec)
if __name__ == "__main__":
_main()