Justin Yip | 5fa5bbc | 2014-09-19 04:25:11 -0700 | [diff] [blame] | 1 | """ |
| 2 | itemrank quickstart import data |
| 3 | """ |
| 4 | |
| 5 | import predictionio |
| 6 | |
| 7 | import random |
Justin Yip | fd634ed | 2014-11-11 18:59:31 -0800 | [diff] [blame] | 8 | import sys |
Justin Yip | 5fa5bbc | 2014-09-19 04:25:11 -0700 | [diff] [blame] | 9 | |
Justin Yip | fd634ed | 2014-11-11 18:59:31 -0800 | [diff] [blame] | 10 | def import_itemrank(access_key): |
Justin Yip | 5fa5bbc | 2014-09-19 04:25:11 -0700 | [diff] [blame] | 11 | |
| 12 | random.seed() |
| 13 | |
Justin Yip | fd634ed | 2014-11-11 18:59:31 -0800 | [diff] [blame] | 14 | client = predictionio.EventClient(access_key) |
Justin Yip | 3343dff | 2014-09-19 15:32:07 -0700 | [diff] [blame] | 15 | |
| 16 | print client.get_status() |
Justin Yip | 5fa5bbc | 2014-09-19 04:25:11 -0700 | [diff] [blame] | 17 | |
| 18 | # generate 10 users, with user ids 1,2,....,10 |
| 19 | user_ids = [str(i) for i in range(1, 11)] |
| 20 | for user_id in user_ids: |
| 21 | print "Set user", user_id |
| 22 | client.set_user(user_id) |
| 23 | |
| 24 | # generate 50 items, with item ids 1,2,....,50 |
| 25 | # assign type id 1 to all of them |
| 26 | item_ids = [str(i) for i in range(1, 51)] |
| 27 | for item_id in item_ids: |
| 28 | print "Set item", item_id |
| 29 | client.set_item(item_id, { |
Justin Yip | c5e9024 | 2014-12-12 16:13:15 -0800 | [diff] [blame] | 30 | "itypes" : ['1'] |
Justin Yip | 5fa5bbc | 2014-09-19 04:25:11 -0700 | [diff] [blame] | 31 | }) |
| 32 | |
| 33 | # each user randomly views 10 items |
| 34 | for user_id in user_ids: |
| 35 | for viewed_item in random.sample(item_ids, 10): |
| 36 | print "User", user_id ,"views item", viewed_item |
| 37 | client.record_user_action_on_item("view", user_id, viewed_item) |
| 38 | |
| 39 | client.close() |
| 40 | |
| 41 | |
| 42 | if __name__ == '__main__': |
Justin Yip | fd634ed | 2014-11-11 18:59:31 -0800 | [diff] [blame] | 43 | if len(sys.argv) < 2: |
| 44 | sys.exit("Usage: python -m examples.itemrank_quick_start <access_key>") |
| 45 | import_itemrank(sys.argv[1]) |