|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import sys |
| 15 | +import os |
| 16 | +import json |
| 17 | +import numpy as np |
| 18 | +import argparse |
| 19 | +import random |
| 20 | + |
| 21 | +parser = argparse.ArgumentParser() |
| 22 | +parser.add_argument( |
| 23 | + "-type", type=str, default="train", help="train|valid|test") |
| 24 | +parser.add_argument("-maxlen", type=int, default=20) |
| 25 | + |
| 26 | + |
| 27 | +def load_graph(source): |
| 28 | + graph = {} |
| 29 | + with open(source) as fr: |
| 30 | + for line in fr: |
| 31 | + conts = line.strip().split(',') |
| 32 | + user_id = int(conts[0]) |
| 33 | + item_id = int(conts[1]) |
| 34 | + time_stamp = int(conts[2]) |
| 35 | + if user_id not in graph: |
| 36 | + graph[user_id] = [] |
| 37 | + graph[user_id].append((item_id, time_stamp)) |
| 38 | + |
| 39 | + for user_id, value in graph.items(): |
| 40 | + value.sort(key=lambda x: x[1]) |
| 41 | + graph[user_id] = [x[0] for x in value] |
| 42 | + return graph |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + args = parser.parse_args() |
| 47 | + filelist = [] |
| 48 | + for i in range(10): |
| 49 | + filelist.append(open(args.type + "/part-%d" % (i), "w")) |
| 50 | + action_graph = load_graph("book_data/book_" + args.type + ".txt") |
| 51 | + if args.type == "train": |
| 52 | + for uid, item_list in action_graph.items(): |
| 53 | + for i in range(4, len(item_list)): |
| 54 | + if i >= args.maxlen: |
| 55 | + hist_item = item_list[i - args.maxlen:i] |
| 56 | + else: |
| 57 | + hist_item = item_list[:i] |
| 58 | + target_item = item_list[i] |
| 59 | + print( |
| 60 | + " ".join(["user_id:" + str(uid)] + [ |
| 61 | + "hist_item:" + str(n) for n in hist_item |
| 62 | + ] + ["target_item:" + str(target_item)]), |
| 63 | + file=random.choice(filelist)) |
| 64 | + else: |
| 65 | + for uid, item_list in action_graph.items(): |
| 66 | + k = int(len(item_list) * 0.8) |
| 67 | + if k >= args.maxlen: |
| 68 | + hist_item = item_list[k - args.maxlen:k] |
| 69 | + else: |
| 70 | + hist_item = item_list[:k] |
| 71 | + target_item = item_list[k:] |
| 72 | + print( |
| 73 | + " ".join(["user_id:" + str(uid), "target_item:0"] + [ |
| 74 | + "hist_item:" + str(n) for n in hist_item |
| 75 | + ] + ["eval_item:" + str(n) for n in target_item]), |
| 76 | + file=random.choice(filelist)) |
0 commit comments