-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_prep.py
More file actions
272 lines (221 loc) · 10 KB
/
Copy pathdata_prep.py
File metadata and controls
272 lines (221 loc) · 10 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""
Steps:
1. Load MovieLens 1M data
2. Fetch director name + production countries from TMDb API
3. Annotate director gender using gender-guesser
4. Classify production country as western / non-western
5. Save enriched CSV for graph building (Day 4+)
Requirements:
pip install requests gender-guesser pandas tqdm
Directory structure expected:
/data/ml-1m/
ratings.dat
movies.dat
users.dat
Download MovieLens 1M from:
https://grouplens.org/datasets/movielens/1m/
"""
import os
import time
import requests
import pandas as pd
import gender_guesser.detector as gender
from tqdm import tqdm
# ─── CONFIG ───────────────────────────────────────────────────────────────────
TMDB_API_KEY = "5782260b08afc35762a551c188457464"
DATA_DIR = "data/ml-25m"
OUTPUT_PATH = "data/movies_enriched.csv"
CACHE_PATH = "data/tmdb_cache.csv"
# TMDb rate limit: ~40 requests/10 seconds. Sleep keeps it safe.
SLEEP_BETWEEN_REQUESTS = 0.26
# ─── WESTERN COUNTRIES ────────────────────────────────────────────────────────
# Production countries classified as "western"
WESTERN_COUNTRIES = {
"US", "GB", "FR", "DE", "IT", "ES", "CA", "AU", "NL", "SE",
"NO", "DK", "FI", "BE", "AT", "CH", "NZ", "IE", "PT", "LU"
}
# ─── LOAD MOVIELENS 1M ────────────────────────────────────────────────────────
def load_movielens(data_dir):
movies = pd.read_csv(
os.path.join(data_dir, "movies.csv"), # .csv not .dat
# no sep, encoding args needed
).rename(columns={"movieId": "movie_id"}) # normalise column name
ratings = pd.read_csv(
os.path.join(data_dir, "ratings.csv"),
).rename(columns={"movieId": "movie_id", "userId": "user_id"})
print(f"Loaded: {len(movies)} movies, {len(ratings)} ratings")
# No users.dat in 25M — return None for users
return movies, ratings, None
# ─── TMDB SEARCH ──────────────────────────────────────────────────────────────
def extract_year(title):
"""Extract year from MovieLens title format: 'Movie Name (1999)'"""
if "(" in title and ")" in title:
try:
return int(title[title.rfind("(")+1:title.rfind(")")])
except:
pass
return None
def clean_title(title):
"""Remove year from title for TMDb search."""
if "(" in title:
return title[:title.rfind("(")].strip()
return title.strip()
def fetch_tmdb_data(title, year, api_key):
"""
Search TMDb for a movie and return:
- director name (first credited director)
- list of production country codes (ISO 3166-1)
Returns (None, []) on failure.
"""
search_url = "https://api.themoviedb.org/3/search/movie"
params = {
"api_key": api_key,
"query": title,
"year": year,
"language": "en-US"
}
try:
r = requests.get(search_url, params=params, timeout=10)
r.raise_for_status()
results = r.json().get("results", [])
if not results:
return None, []
movie_id = results[0]["id"]
# Fetch credits + details together
detail_url = f"https://api.themoviedb.org/3/movie/{movie_id}"
detail_params = {
"api_key": api_key,
"append_to_response": "credits",
"language": "en-US"
}
d = requests.get(detail_url, params=detail_params, timeout=10)
d.raise_for_status()
data = d.json()
# Director
crew = data.get("credits", {}).get("crew", [])
directors = [c["name"] for c in crew if c.get("job") == "Director"]
director = directors[0] if directors else None
# Production countries (ISO codes)
countries = [c["iso_3166_1"] for c in data.get("production_countries", [])]
return director, countries
except Exception as e:
return None, []
# ─── GENDER ANNOTATION ────────────────────────────────────────────────────────
def annotate_gender(name, detector):
"""
Use gender-guesser on first name.
Returns: 'female', 'male', or 'unknown'
"""
if not name:
return "unknown"
first_name = name.strip().split()[0]
result = detector.get_gender(first_name)
if result in ("female", "mostly_female"):
return "female"
elif result in ("male", "mostly_male"):
return "male"
else:
return "unknown"
# ─── REGION CLASSIFICATION ────────────────────────────────────────────────────
def classify_region(country_codes):
"""
Given a list of ISO country codes, classify the movie as:
- 'western' if any production country is western
- 'non-western' if all are non-western
- 'unknown' if no country data
"""
if not country_codes:
return "unknown"
for code in country_codes:
if code in WESTERN_COUNTRIES:
return "western"
return "non-western"
# ─── MAIN ─────────────────────────────────────────────────────────────────────
def main():
os.makedirs("data", exist_ok=True)
# Load MovieLens
movies, ratings, users = load_movielens(DATA_DIR)
# Load cache if it exists (lets you resume after interruption)
if os.path.exists(CACHE_PATH):
cache = pd.read_csv(CACHE_PATH)
cache["movie_id"] = cache["movie_id"].astype(int)
done_ids = set(cache["movie_id"].tolist())
print(f"Resuming from cache: {len(done_ids)} movies already fetched")
else:
cache = pd.DataFrame(columns=["movie_id", "director", "countries"])
done_ids = set()
detector = gender.Detector()
rows = []
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
cache_lock = threading.Lock()
# First, load already-cached movies into rows directly
for _, cached_row in cache.iterrows():
mid = cached_row["movie_id"]
if mid not in movies["movie_id"].values:
continue
movie_row = movies[movies["movie_id"] == mid].iloc[0]
director = cached_row["director"] if pd.notna(cached_row["director"]) else None
country_codes = cached_row["countries"].split("|") if pd.notna(cached_row["countries"]) and cached_row["countries"] else []
rows.append({
"movie_id": mid,
"title": movie_row["title"],
"genres": movie_row["genres"],
"director": director,
"director_gender": annotate_gender(director, detector),
"countries": "|".join(country_codes),
"region": classify_region(country_codes)
})
def process_movie(row):
mid = row["movie_id"]
title = clean_title(row["title"])
year = extract_year(row["title"])
director, country_codes = fetch_tmdb_data(title, year, TMDB_API_KEY)
time.sleep(SLEEP_BETWEEN_REQUESTS)
return mid, row["title"], row["genres"], director, country_codes
movies["movie_id"] = movies["movie_id"].astype(int)
pending = movies[~movies["movie_id"].isin(done_ids)]
print(f"\nFetching TMDb data for {len(pending)} remaining movies (8 threads)...")
with ThreadPoolExecutor(max_workers=8) as executor:
futures = {executor.submit(process_movie, row): row for _, row in pending.iterrows()}
for future in tqdm(as_completed(futures), total=len(futures)):
mid, title, genres, director, country_codes = future.result()
director_gender = annotate_gender(director, detector)
region = classify_region(country_codes)
rows.append({
"movie_id": mid,
"title": title,
"genres": genres,
"director": director,
"director_gender": director_gender,
"countries": "|".join(country_codes),
"region": region
})
with cache_lock:
new_row = pd.DataFrame([{"movie_id": mid, "director": director,
"countries": "|".join(country_codes)}])
cache = pd.concat([cache, new_row], ignore_index=True)
if len(cache) % 50 == 0:
cache.to_csv(CACHE_PATH, index=False)
# Final cache save
cache.to_csv(CACHE_PATH, index=False)
# Build enriched dataframe
enriched = pd.DataFrame(rows)
# Print stats
print("\n--- Enrichment Summary ---")
print(f"Total movies: {len(enriched)}")
print(f"Director found: {enriched['director'].notna().sum()}")
print(f"Gender female: {(enriched['director_gender'] == 'female').sum()}")
print(f"Gender male: {(enriched['director_gender'] == 'male').sum()}")
print(f"Gender unknown: {(enriched['director_gender'] == 'unknown').sum()}")
print(f"Region western: {(enriched['region'] == 'western').sum()}")
print(f"Region non-western: {(enriched['region'] == 'non-western').sum()}")
print(f"Region unknown: {(enriched['region'] == 'unknown').sum()}")
enriched.to_csv(OUTPUT_PATH, index=False)
print(f"\nSaved enriched data to: {OUTPUT_PATH}")
# Also save ratings and users as-is for later steps
ratings.to_csv("data/ratings.csv", index=False)
#users.to_csv("data/users.csv", index=False)
print("Saved ratings.csv and users.csv")
if __name__ == "__main__":
main()