-
Notifications
You must be signed in to change notification settings - Fork 4
/
load_db.py
157 lines (129 loc) · 4.09 KB
/
load_db.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
import pymongo
import movies
def load_movies(filename):
"""1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0
id, title, release_date, vhs_release_date, imdb_url, """
movie_dict = {}
f = open(filename)
for line in f:
fields = clean_line(line, "|")
if not fields:
continue
movie_id = int(fields[0])
title = fields[1]
release_date = fields[2]
imdb_url = fields[4]
genre_list = fields[5:]
movie = {
"_id": movie_id,
"title": title,
"release_date": release_date,
"imdb_url": imdb_url,
"genre_list": genre_list
}
movie_dict[movie_id] = movie
f.close()
# For loop is ended
return movie_dict
def load_genres(movie_dict, filename):
genre_dict = {}
f = open(filename)
for line in f:
fields = clean_line(line, "|")
if not fields:
continue
key = int(fields[1]) # the key is the second field
genre = fields[0] # genre is the first field
genre_dict[key] = genre
f.close()
for movie_id, movie in movie_dict.items():
genre_ids = movie['genre_list']
genre_list = []
for i in range(len(genre_ids)):
if genre_ids[i] == "1":
genre = genre_dict[i]
genre_list.append(genre)
# genre list is populated
movie['genres'] = genre_list
def clean_line(line, sep=None):
stripped = line.strip().decode("latin-1").encode("utf-8")
if stripped == "":
return None
fields = stripped.split(sep)
#return [ field.decode("ascii").encode("utf-8") for field in stripped.split(sep) ]
return fields
def load_users(filename):
f = open(filename)
user_db = {}
genders = {"M": "Male", "F": "Female"}
for line in f:
fields = clean_line(line, "|")
id, age, gender, occupation, zipcode = fields
id = int(id)
age = int(age)
gender = genders[gender]
user_db[id] = {
"_id": id,
"age": age,
"gender": gender,
"occupation": occupation
}
f.close()
return user_db
def load_ratings(filename):
ratings = []
f = open(filename)
for line in f:
fields = clean_line(line)
if not fields:
continue
user_id, movie_id, rating, timestamp = fields
user_id = int(user_id)
movie_id = int(movie_id)
rating = int(rating)
# movie_ratings = ratings.get(movie_id)
# if not movie_ratings:
# ratings[str(movie_id)] = {str(user_id): rating}
# else:
# movie_ratings[str(user_id)] = rating
ratings.append({"user_id": user_id,
"movie_id": movie_id,
"rating": rating})
f.close()
return ratings
def push_to_mongo(db, collection_name, local_db):
for _id, item in local_db.items():
db[collection_name].insert(item)
def push_ratings(db, ratings):
#db.ratings.update({"by_movie": {"$exists": True}}, {"by_movie":ratings}, upsert=True)
db.ratings.insert(ratings)
def main():
global db
movie_db = load_movies("../ml-100k/u.item")
load_genres(movie_db, "../ml-100k/u.genre")
user_db = load_users("../ml-100k/u.user")
ratings_db = load_ratings("../ml-100k/u.data")
db = movies.connect_db("dbh36.mongolab.com", 27367, "movie_user", "password", "movies")
#db = pymongo.connection.Connection("localhost")
db = db['movies']
#push_to_mongo(db, "movies", movie_db)
#push_to_mongo(db, "users", user_db)
push_ratings(db, ratings_db)
preloaded = [
(71, 5),
(72, 4),
(588, 5),
(1014, 4),
(541, 5),
(543, 5),
(42, 4),
(83, 5),
(82, 5),
(88, 2),
(739, 2)
]
movies.db = db
for movie,rating in preloaded:
movies.rate_movie(movie, rating)
if __name__ == "__main__":
main()