text embeddings for plot

This commit is contained in:
IshaAtteri
2026-03-26 14:05:02 -04:00
parent 496761ca78
commit ee358acf64
5 changed files with 25 additions and 11 deletions

BIN
plot_embeddings.npy Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -18,10 +18,6 @@ sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)
stemmer = PorterStemmer() stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer() lemmatizer = WordNetLemmatizer()
# model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
# df = pd.read_excel('C:\\Users\\ishaa\\OneDrive\\Documents\\MSU\\Spring 2026\\Data mining\\Project\\sample_data.xlsx', engine='openpyxl')
def clean_plot(text): def clean_plot(text):
text = text.lower() text = text.lower()
text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation
@@ -35,11 +31,12 @@ def clean_plot(text):
return text return text
def get_genre(row): def get_genre(row):
if pd.isna(row['Genre']):
return ""
movie = row['Title'] movie = row['Title']
print(movie)
text = row['Genre'] text = row['Genre']
text = text.split(".")[0]
text = text.replace(movie, "") text = text.replace(movie, "")
text = text.split(".")[0]
text = text.lower() text = text.lower()
match = re.search(r'is a ((?:\S+\s+){4}\S+)', text) match = re.search(r'is a ((?:\S+\s+){4}\S+)', text)
if match: if match:
@@ -53,13 +50,14 @@ def get_genre(row):
return text return text
def pre_director(text): def pre_director(text):
if not text: if pd.isna(text) or not text:
return "" return ""
text = text.lower().strip() text = text.lower().strip()
return text return text
def clean_cast(text, top_k=5): def clean_cast(text):
if not text: print(f"Original cast: {text}")
if pd.isna(text) or not text:
return [] return []
text = text.lower() text = text.lower()
cast_list = [actor.strip() for actor in text.split(",")] cast_list = [actor.strip() for actor in text.split(",")]

View File

@@ -2,10 +2,12 @@ import pandas as pd
from sentence_transformers import SentenceTransformer from sentence_transformers import SentenceTransformer
from preprocessing import clean_plot, get_genre, pre_director, clean_cast from preprocessing import clean_plot, get_genre, pre_director, clean_cast
from sklearn.metrics.pairwise import cosine_similarity from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
df = pd.read_excel('C:\\Users\\ishaa\\OneDrive\\Documents\\MSU\\Spring 2026\\Data mining\\Project\\updated_data.xlsx', engine='openpyxl') df = pd.read_excel('C:\\Users\\ishaa\\OneDrive\\Documents\\MSU\\Spring 2026\\Data mining\\Project\\updated_datav2.xlsx', engine='openpyxl')
df = df.dropna(subset=['Plot'])
df = df.dropna(subset=['Genre', 'Plot']) print(len(df))
df['Processed_Plot'] = df['Plot'].apply(clean_plot) df['Processed_Plot'] = df['Plot'].apply(clean_plot)
@@ -14,4 +16,18 @@ df['Pre_genre'] = df[['Genre', 'Title']].apply(get_genre, axis=1)
df['Pre_director'] = df['Director'].apply(pre_director) df['Pre_director'] = df['Director'].apply(pre_director)
df['Pre_cast'] = df['Cast'].apply(clean_cast) df['Pre_cast'] = df['Cast'].apply(clean_cast)
# Load embedding model
model = SentenceTransformer("all-MiniLM-L6-v2")
# Generate one embedding per movie plot
plot_embeddings = model.encode(
df["Processed_Plot"].tolist(),
show_progress_bar=True,
convert_to_numpy=True
)
np.save(r"C:\Users\ishaa\OneDrive\Documents\MSU\Spring 2026\Data mining\Project\plot_embeddings.npy",plot_embeddings)
df.to_excel('C:\\Users\\ishaa\\OneDrive\\Documents\\MSU\\Spring 2026\\Data mining\\Project\\preprocessed_data.xlsx', index=False) df.to_excel('C:\\Users\\ishaa\\OneDrive\\Documents\\MSU\\Spring 2026\\Data mining\\Project\\preprocessed_data.xlsx', index=False)

BIN
updated_datav2.xlsx Normal file

Binary file not shown.