File size: 1,932 Bytes
97ce7fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import numpy as np
import pandas as pd

from jsonargparse import ArgumentParser


def parse_args():
    """Parse command-line arguments."""
    parser = ArgumentParser()
    parser.add_argument("--data_dir", type=str, required=True, default="./processed")
    parser.add_argument("--weight_dir", type=str, required=True, default="./weight")
    parser.add_argument("--info_path", type=str, required=True, default="./processed/summary_book.csv")
    parser.add_argument("--user_id", required=True, default="276729")
    parser.add_argument("--num", type=int, required=True, default=10)
    parser.add_argument("--lb", type=int, required=True, default=0)

    return vars(parser.parse_args())

def main(
    data_dir,
    weight_dir,
    info_path,
    user_id,
    num,
    lb,
    **kwargs
):
    # Load R matrix from file
    R = np.load(f'{data_dir}/R.npy', allow_pickle=True)
    # Load prediction
    prediction = np.load(f'{weight_dir}/predicted.npy', allow_pickle=True)
    # Load dictionary from JSON file
    with open(f'{data_dir}/user_id_map.json', 'r') as file:
        user2id = json.load(file)
    with open(f'{data_dir}/book_id_map.json', 'r') as file:
        book2id = json.load(file)

    # Recommend
    user_idx = user2id[str(user_id)]
    predict = prediction[:, user_idx]   # get prediction for user
    predict_dict = {book: np.round(predict[idx], 2) for book, idx in book2id.items()}
    # Load information about book
    book_df = pd.read_csv(info_path)
    book_df = book_df[book_df["Num-Rating"] > lb]
    book_df['predict'] = book_df["ISBN"].map(predict_dict)
    recommendations = book_df.nlargest(num, "predict").reset_index(drop=True)
    recommendations["context"] = recommendations.apply(
        lambda book: f"{book['Book-Title']} ({book['Year-Of-Publication']}) - by {book['Book-Author']}", axis=1
    )
    print(recommendations)


if __name__ == "__main__":
    main(**parse_args())