Spaces:
Sleeping
Sleeping
Update vectorization/vectorize_movies_50000.py
Browse files- vectorization/vectorize_movies_50000.py +136 -139
vectorization/vectorize_movies_50000.py
CHANGED
|
@@ -1,140 +1,137 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
- data/movie/
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
- data/movie/vectorized/
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
import
|
| 20 |
-
import
|
| 21 |
-
|
| 22 |
-
from
|
| 23 |
-
from
|
| 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 |
-
except
|
| 55 |
-
print(f"Error:
|
| 56 |
-
return []
|
| 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 |
-
# Save the
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
"""
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# Save the
|
| 107 |
-
with open(output_dir / f"{prefix}
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
print(f"
|
| 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 |
-
if __name__ == "__main__":
|
| 140 |
main()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
vectorization/vectorize_movies_50000.py
|
| 3 |
+
|
| 4 |
+
Vectorizes preprocessed TMDb movie data using TF-IDF and SBERT.
|
| 5 |
+
|
| 6 |
+
Input:
|
| 7 |
+
- data/movie/preprocessed/movies_preprocessed_50000.json
|
| 8 |
+
|
| 9 |
+
Outputs:
|
| 10 |
+
- data/movie/vectorized/movies_tfidf_vectorizer_50000.pkl
|
| 11 |
+
- data/movie/vectorized/movies_tfidf_matrix_50000.npz
|
| 12 |
+
- data/movie/vectorized/movies_sbert_embeddings_50000.pkl
|
| 13 |
+
- data/movie/vectorized/movies_sbert_model_50000.txt
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import json
|
| 18 |
+
import pickle
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
from typing import List, Dict, Any
|
| 21 |
+
|
| 22 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 23 |
+
from sentence_transformers import SentenceTransformer
|
| 24 |
+
from scipy import sparse
|
| 25 |
+
|
| 26 |
+
# --- Path Configurations ---
|
| 27 |
+
# Define the base directory for movie data
|
| 28 |
+
DATA_DIR = Path(__file__).parent.parent / "data" / "movie"
|
| 29 |
+
# Path to the input preprocessed JSON file (updated for 50,000 records)
|
| 30 |
+
INPUT_PATH = DATA_DIR / "preprocessed" / "movies_preprocessed_50000.json"
|
| 31 |
+
# Directory to save vectorized outputs
|
| 32 |
+
OUT_DIR = DATA_DIR / "vectorized"
|
| 33 |
+
# Ensure the output directory exists
|
| 34 |
+
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 35 |
+
|
| 36 |
+
# --- Constants ---
|
| 37 |
+
# Default SBERT model name to use for embeddings
|
| 38 |
+
DEFAULT_SBERT_MODEL = "all-MiniLM-L6-v2"
|
| 39 |
+
# Maximum number of features for TF-IDF vectorizer. Increased for larger dataset.
|
| 40 |
+
TFIDF_MAX_FEATURES = 10000 # Adjusted from 5000 to 10000 for a larger corpus
|
| 41 |
+
# Prefix for output files
|
| 42 |
+
OUTPUT_PREFIX = "movies"
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# --- Data Loading Function ---
|
| 46 |
+
def load_records(path: Path) -> List[Dict[str, Any]]:
|
| 47 |
+
"""Loads preprocessed movie records from a JSON file."""
|
| 48 |
+
try:
|
| 49 |
+
with open(path, 'r', encoding='utf-8') as f:
|
| 50 |
+
return json.load(f)
|
| 51 |
+
except FileNotFoundError:
|
| 52 |
+
print(f"Error: Input file not found at {path}")
|
| 53 |
+
return []
|
| 54 |
+
except json.JSONDecodeError:
|
| 55 |
+
print(f"Error: Could not decode JSON from {path}. Check file format.")
|
| 56 |
+
return []
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# --- Corpus Building Function ---
|
| 60 |
+
def build_corpus(records: List[Dict[str, Any]]) -> List[str]:
|
| 61 |
+
"""
|
| 62 |
+
Constructs a corpus of text from movie records.
|
| 63 |
+
Each document in the corpus is a concatenation of title and overview tokens.
|
| 64 |
+
"""
|
| 65 |
+
corpus = []
|
| 66 |
+
for r in records:
|
| 67 |
+
tokens = r.get('title_tokens', []) + r.get('overview_tokens', [])
|
| 68 |
+
corpus.append(' '.join(tokens))
|
| 69 |
+
return corpus
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# --- TF-IDF Vectorization ---
|
| 73 |
+
def vectorize_tfidf(corpus: List[str], output_dir: Path, prefix: str):
|
| 74 |
+
"""
|
| 75 |
+
Performs TF-IDF vectorization on the given corpus.
|
| 76 |
+
Saves the trained TF-IDF vectorizer and the resulting sparse matrix.
|
| 77 |
+
"""
|
| 78 |
+
print(f"🚀 [TF-IDF] Starting TF-IDF vectorization with {TFIDF_MAX_FEATURES} features...")
|
| 79 |
+
vectorizer = TfidfVectorizer(max_features=TFIDF_MAX_FEATURES)
|
| 80 |
+
tfidf_matrix = vectorizer.fit_transform(corpus)
|
| 81 |
+
|
| 82 |
+
# Save the fitted TF-IDF vectorizer (updated filename)
|
| 83 |
+
with open(output_dir / f"{prefix}_tfidf_vectorizer_50000.pkl", 'wb') as f:
|
| 84 |
+
pickle.dump(vectorizer, f)
|
| 85 |
+
# Save the TF-IDF matrix in sparse NPZ format (updated filename)
|
| 86 |
+
sparse.save_npz(output_dir / f"{prefix}_tfidf_matrix_50000.npz", tfidf_matrix)
|
| 87 |
+
|
| 88 |
+
print(f"✅ [TF-IDF] Vectorizer and matrix saved to {output_dir}.")
|
| 89 |
+
print(f" TF-IDF matrix shape: {tfidf_matrix.shape}")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# --- SBERT Vectorization ---
|
| 93 |
+
def vectorize_sbert(corpus: List[str], output_dir: Path, prefix: str, model_name: str = DEFAULT_SBERT_MODEL):
|
| 94 |
+
"""
|
| 95 |
+
Performs SBERT embedding generation on the given corpus.
|
| 96 |
+
Saves the generated embeddings and the name of the SBERT model used.
|
| 97 |
+
"""
|
| 98 |
+
print(f"🚀 [SBERT] Starting SBERT embedding generation using model: {model_name}...")
|
| 99 |
+
try:
|
| 100 |
+
model = SentenceTransformer(model_name)
|
| 101 |
+
embeddings = model.encode(corpus, show_progress_bar=True, batch_size=32)
|
| 102 |
+
|
| 103 |
+
# Save the generated embeddings (updated filename)
|
| 104 |
+
with open(output_dir / f"{prefix}_sbert_embeddings_50000.pkl", 'wb') as f:
|
| 105 |
+
pickle.dump(embeddings, f)
|
| 106 |
+
# Save the name of the SBERT model used (updated filename)
|
| 107 |
+
with open(output_dir / f"{prefix}_sbert_model_50000.txt", 'w', encoding='utf-8') as f:
|
| 108 |
+
f.write(model_name)
|
| 109 |
+
|
| 110 |
+
print(f"✅ [SBERT] Embeddings and model name saved to {output_dir}.")
|
| 111 |
+
print(f" SBERT embeddings shape: {embeddings.shape}")
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"❌ [SBERT] Error during SBERT embedding generation: {e}")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# --- Main Execution ---
|
| 117 |
+
def main():
|
| 118 |
+
"""Main function to orchestrate the movie vectorization process."""
|
| 119 |
+
if not INPUT_PATH.exists():
|
| 120 |
+
raise FileNotFoundError(f"Missing input file: {INPUT_PATH}. Please ensure preprocessing is complete.")
|
| 121 |
+
|
| 122 |
+
records = load_records(INPUT_PATH)
|
| 123 |
+
if not records:
|
| 124 |
+
print("No records loaded. Exiting vectorization process.")
|
| 125 |
+
return
|
| 126 |
+
|
| 127 |
+
corpus = build_corpus(records)
|
| 128 |
+
print(f"📽️ Loaded {len(records)} movie records and built corpus of {len(corpus)} documents.")
|
| 129 |
+
print("Starting vectorization process...")
|
| 130 |
+
|
| 131 |
+
vectorize_tfidf(corpus, OUT_DIR, OUTPUT_PREFIX)
|
| 132 |
+
vectorize_sbert(corpus, OUT_DIR, OUTPUT_PREFIX, DEFAULT_SBERT_MODEL)
|
| 133 |
+
print("✨ Movie vectorization complete!")
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 137 |
main()
|