-
Notifications
You must be signed in to change notification settings - Fork 0
/
movielens.py
86 lines (80 loc) · 2.94 KB
/
movielens.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
"""
Scripts to help load the movielens dataset into Python classes
"""
import re
# Read data/README to get more info on these data structures
class User:
def __init__(self, id, age, sex, occupation, zip):
self.id = int(id)
self.age = int(age)
self.sex = sex
self.occupation = occupation
self.zip = zip
self.avg_r = 0.0
# Read data/README to get more info on these data structures
class Item:
def __init__(self, id, title, release_date, video_release_date, imdb_url, \
unknown, action, adventure, animation, childrens, comedy, crime, documentary, \
drama, fantasy, film_noir, horror, musical, mystery ,romance, sci_fi, thriller, war, western):
self.id = int(id)
self.title = title
self.release_date = release_date
self.video_release_date = video_release_date
self.imdb_url = imdb_url
self.unknown = int(unknown)
self.action = int(action)
self.adventure = int(adventure)
self.animation = int(animation)
self.childrens = int(childrens)
self.comedy = int(comedy)
self.crime = int(crime)
self.documentary = int(documentary)
self.drama = int(drama)
self.fantasy = int(fantasy)
self.film_noir = int(film_noir)
self.horror = int(horror)
self.musical = int(musical)
self.mystery = int(mystery)
self.romance = int(romance)
self.sci_fi = int(sci_fi)
self.thriller = int(thriller)
self.war = int(war)
self.western = int(western)
# Read data/README to get more info on these data structures
class Rating:
def __init__(self, user_id, item_id, rating, time):
self.user_id = int(user_id)
self.item_id = int(item_id)
self.rating = int(rating)
self.time = time
# The dataset class helps you to load files and create User, Item and Rating objects
class Dataset:
def load_users(self, file, u):
f = open(file, "r")
text = f.read()
entries = re.split("\n+", text)
for entry in entries:
e = entry.split('|', 5)
if len(e) == 5:
u.append(User(e[0], e[1], e[2], e[3], e[4]))
f.close()
def load_items(self, file, i):
f = open(file, "r")
text = f.read()
entries = re.split("\n+", text)
for entry in entries:
e = entry.split('|', 24)
if len(e) == 24:
i.append(Item(e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], e[8], e[9], e[10], \
e[11], e[12], e[13], e[14], e[15], e[16], e[17], e[18], e[19], e[20], e[21], \
e[22], e[23]))
f.close()
def load_ratings(self, file, r):
f = open(file, "r")
text = f.read()
entries = re.split("\n+", text)
for entry in entries:
e = entry.split('\t', 4)
if len(e) == 4:
r.append(Rating(e[0], e[1], e[2], e[3]))
f.close()