Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
4.1 kB
1
Indexable
Never
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel

# Movie dataset with 5 different movies from different genres
movies = pd.DataFrame({
    'title': ['Scream', 'Inside Out', 'Iron Man', 'Anyone but You', 'Yesterday'],
    'description': [
        'Scream is set in the fictional town of Woodsboro, California. The plot follows high school student Sidney Prescott and her friends, who, on the anniversary of the death of her mother, become the targets of a costumed serial killer known as Ghostface.',
        'Inside Out is an animated comedy about a girl named Riley, who is uprooted from her life in Minnesota when her father gets a new job in San Francisco. Riley is largely guided by her emotions, each of which is shown as an actual character.',
        'Iron Man tells the story of Tony Stark, a billionaire industrialist and genius inventor who is kidnapped and forced to build a devastating weapon. Instead, using his intelligence and ingenuity, Tony builds a high-tech suit of armor and escapes captivity.',
        'In the edgy comedy Anyone But You, Bea and Ben look like the perfect couple, but after an amazing first date something happens that turns their fiery hot attraction ice cold -- until they find themselves unexpectedly thrust together at a destination wedding in Australia.',
        'Jack Malik is a struggling singer-songwriter in an English seaside town whose dreams of fame are rapidly fading, despite the fierce devotion and support of his childhood best friend, Ellie. After a freak bus accident during a mysterious global blackout, Jack wakes up to discover that The Beatles have never existed. Performing songs by the greatest band in history to a world that has never heard them, Jack becomes an overnight sensation.'
    ],
    'genre': ['Horror', 'Animation', 'Action', 'Rom-Com', 'Comedy']
})

# Combine genre and description for better recommendations
movies['content'] = movies['genre'] + " " + movies['description']

# Text preprocessing using TF-IDF
# Initialize the TF-IDF Vectorizer with English stop words
tfidf = TfidfVectorizer(stop_words='english')
# Fit and transform the 'content' data to create the TF-IDF matrix
tfidf_matrix = tfidf.fit_transform(movies['content'])

# Compute cosine similarity matrix
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)

# Function to recommend movies
def recommended_movies(query, cosine_sim=cosine_sim):
    # Add the query to the movies dataset for similarity comparison
    query_df = pd.DataFrame({'content': [query]})
    # Transform the query using the same TF-IDF vectorizer
    query_tfidf = tfidf.transform(query_df['content'])
    # Compute the cosine similarity between the query and all movies
    query_sim_scores = linear_kernel(query_tfidf, tfidf_matrix).flatten()
    # Get the indices of the most similar movies
    sim_scores = list(enumerate(query_sim_scores))
    # Sort the movies based on similarity score in descending order
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
    # Get the scores of the top 5 most similar movies
    sim_scores = sim_scores[:5]
    # Get the movie indices
    movie_indices = [i[0] for i in sim_scores]
    # Return the top 5 most similar movies
    return movies['title'].iloc[movie_indices]

# Interactive function for user input and recommendations
def get_user_recommendations():
    print("Welcome to the movie Recommendation System!")
    # Prompt the user to input the type of movie they are looking for
    query = input("Please describe the type of movie you are looking for (e.g., A funny and romantic movie that takes place in Australia, Animation, Horror, etc.): ")
    # Get the movie recommendations based on user's input
    recommendations = recommended_movies(query)
    # Print recommended movies
    print("\nMovies recommended for your query:")
    for i, movie in enumerate(recommendations, 1):
        print(f"{i}. {movie}")

# Run the recommendation system
if __name__ == "__main__":
    get_user_recommendations()
Leave a Comment