-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
212 lines (174 loc) · 9.63 KB
/
preprocess.py
File metadata and controls
212 lines (174 loc) · 9.63 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
import pandas as pd
import os
import glob
def clear_directory(folder_path):
if os.path.exists(folder_path):
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if os.path.isfile(file_path):
# DO NOT DELETE .txt files, we want to preserve them per user instruction
if not filename.endswith('.txt'):
os.unlink(file_path)
except Exception as e:
pass
else:
os.makedirs(folder_path, exist_ok=True)
def main():
print("=== Dataset Preprocessor ===")
print("Please select an option:")
print("1. Use existing datasets (DEFAULT - Recommended for Reviewers)")
print("2. Upload separate primary and secondary custom datasets")
print("3. Upload a single preprocessed custom dataset containing all columns")
choice = input("\nEnter your choice (1/2/3) [1]: ").strip()
if choice == '' or choice == '1':
print("\n--- Running Option 1: Official Evaluation Track ---")
num_files_to_generate = 15
primary_path = 'dataset/top1000movies.csv'
secondary_path = 'dataset/rotten_tomatoes_movies.csv'
primary_col_title = 'Title'
primary_col_gross = 'Gross'
primary_col_rating = 'rottenRating'
primary_col_year = 'Year'
primary_col_genre = 'Genre'
secondary_col_title = 'movie_title'
secondary_col_review = 'critics_consensus'
try:
primary_df = pd.read_csv(primary_path)
secondary_df = pd.read_csv(secondary_path)
except FileNotFoundError as e:
print(f"Error: Could not find required existing dataset: {e}")
return
secondary_df = secondary_df.dropna(subset=[secondary_col_review, secondary_col_title])
merged = pd.merge(primary_df, secondary_df, left_on=primary_col_title, right_on=secondary_col_title, how='inner')
if merged[primary_col_gross].dtype == object:
merged['Gross_clean'] = merged[primary_col_gross].replace('[\$,]', '', regex=True)
else:
merged['Gross_clean'] = merged[primary_col_gross]
merged = merged.dropna(subset=['Gross_clean', primary_col_rating])
merged['Gross_clean'] = merged['Gross_clean'].astype(float)
merged['computed_budget'] = merged.get('Budget', '')
merged['computed_week_open'] = merged['Gross_clean'] / 60
merged['computed_gross'] = merged['Gross_clean']
merged['computed_rating'] = merged[primary_col_rating]
merged['computed_review'] = merged[secondary_col_review]
merged['computed_title'] = merged[primary_col_title]
merged['computed_year'] = merged[primary_col_year]
merged['computed_genre'] = merged[primary_col_genre]
elif choice == '2':
print("\n--- Custom Separate Datasets ---")
print("\n------------------------------")
num_files_str = input("How many text files should be generated for the unstructured data? [15]: ").strip()
num_files_to_generate = 15 if not num_files_str else int(num_files_str)
print("Note: The PRIMARY dataset MUST contain these columns: 'movie title', 'gross', 'week open', 'budget', 'rotten_rating'")
primary_path = input("Enter path to PRIMARY dataset CSV: ").strip()
print("\nNote: The SECONDARY dataset MUST contain these columns: 'movie title', 'movie_review'")
secondary_path = input("Enter path to SECONDARY dataset CSV: ").strip()
try:
primary_df = pd.read_csv(primary_path)
secondary_df = pd.read_csv(secondary_path)
except FileNotFoundError as e:
print(f"Error: Could not find dataset: {e}")
return
req_prim = ['movie title', 'gross', 'week open', 'budget', 'rotten_rating']
missing_prim = [col for col in req_prim if col not in primary_df.columns]
if missing_prim:
print(f"Error: Primary dataset is missing mandatory columns: {missing_prim}")
return
req_sec = ['movie title', 'movie_review']
missing_sec = [col for col in req_sec if col not in secondary_df.columns]
if missing_sec:
print(f"Error: Secondary dataset is missing mandatory columns: {missing_sec}")
return
secondary_df = secondary_df.dropna(subset=['movie_review', 'movie title'])
merged = pd.merge(primary_df, secondary_df, on='movie title', how='inner')
merged = merged.dropna(subset=['gross', 'rotten_rating'])
merged['computed_budget'] = merged['budget']
merged['computed_week_open'] = merged['week open']
merged['computed_gross'] = merged['gross']
merged['computed_rating'] = merged['rotten_rating']
merged['computed_review'] = merged['movie_review']
merged['computed_title'] = merged['movie title']
merged['computed_year'] = merged.get('Year', '')
merged['computed_genre'] = merged.get('Genre', '')
elif choice == '3':
print("\n--- Single Preprocessed Dataset ---")
print("\n------------------------------")
num_files_str = input("How many text files should be generated for the unstructured data? [15]: ").strip()
num_files_to_generate = 15 if not num_files_str else int(num_files_str)
print("Note: The SINGLE dataset MUST contain all these mandatory columns:")
print("'movie title', 'gross', 'week open', 'budget', 'rotten_rating', 'movie_review'")
single_path = input("Enter path to the single dataset CSV: ").strip()
try:
merged = pd.read_csv(single_path)
except FileNotFoundError as e:
print(f"Error: Could not find dataset: {e}")
return
req_all = ['movie title', 'gross', 'week open', 'budget', 'rotten_rating', 'movie_review']
missing_all = [col for col in req_all if col not in merged.columns]
if missing_all:
print(f"Error: Single dataset is missing mandatory columns: {missing_all}")
return
merged = merged.dropna(subset=['movie_review', 'movie title', 'gross', 'rotten_rating'])
merged['computed_budget'] = merged['budget']
merged['computed_week_open'] = merged['week open']
merged['computed_gross'] = merged['gross']
merged['computed_rating'] = merged['rotten_rating']
merged['computed_review'] = merged['movie_review']
merged['computed_title'] = merged['movie title']
merged['computed_year'] = merged.get('Year', '')
merged['computed_genre'] = merged.get('Genre', '')
else:
print("Invalid choice. Exiting.")
return
# To satisfy user requirements: we must prioritize the ~15 movies that already have txt files in "unstructured_reviews"
unstructured_dir = 'dataset/unstructured_reviews'
existing_txts = []
if os.path.exists(unstructured_dir):
for f in glob.glob(os.path.join(unstructured_dir, '*.txt')):
existing_txts.append(os.path.basename(f).replace('.txt', '').replace('_', ' ').lower())
def get_safe_title(title):
return str(title).replace(':', '').replace('/', '_').replace('?', '').replace('*', '').replace('"', '').replace('<', '').replace('>', '').replace('|', '')
# Create a sort key column: 0 if movie already has a txt file, 1 otherwise
merged['_sort_key'] = merged['computed_title'].apply(lambda t: 0 if get_safe_title(t).lower() in existing_txts else 1)
merged = merged.sort_values(by='_sort_key').drop(columns=['_sort_key'])
print("\nProcessing...")
# Write structured CSV
structured_df = pd.DataFrame()
structured_df['Title'] = merged['computed_title']
structured_df['Year'] = merged['computed_year']
structured_df['Genre'] = merged['computed_genre']
structured_df['budget'] = merged['computed_budget']
structured_df['opening weekend'] = merged['computed_week_open']
structured_df['worldwide gross'] = merged['computed_gross']
structured_df['Rotten Tomatoes score'] = merged['computed_rating']
# STRICT FILTERING: Remove any movie with missing data in ANY of the structured columns
initial_count = len(structured_df)
structured_df = structured_df.replace('', pd.NA).dropna()
final_count = len(structured_df)
structured_path = 'dataset/movies_structured.csv'
try:
structured_df.to_csv(structured_path, index=False)
print(f"Saved {final_count} complete matched rows into {structured_path} (Removed {initial_count - final_count} incomplete rows)")
except PermissionError:
print(f"Skipped saving {structured_path} as it is open or permission is denied (left as is).")
# Generate Unstructured Reviews - Simplified Logic
clear_directory(unstructured_dir)
os.makedirs(unstructured_dir, exist_ok=True)
files_active = 0
for index, row in merged.iterrows():
if files_active >= num_files_to_generate:
break
title = row['computed_title']
cc = row['computed_review']
txt_content = f"movie: {title}\nreview: {cc}\n"
safe_title = get_safe_title(title)
file_path = os.path.join(unstructured_dir, f"{safe_title}.txt")
# We always overwrite or create the new files up to N
with open(file_path, 'w', encoding='utf-8') as f:
f.write(txt_content)
files_active += 1
print(f"Successfully ensured {files_active} text files in {unstructured_dir} directory.")
print("Done!")
if __name__ == "__main__":
main()