The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a movie recommendation bot

Here is a simple movie recommendation bot built with Python. It uses content-based filtering (based on movie genres and descriptions) to recommend similar movies.

python
import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import linear_kernel # Sample movie dataset (you can expand this or load from a CSV) data = { 'title': [ 'The Matrix', 'Inception', 'Interstellar', 'The Dark Knight', 'Fight Club', 'Pulp Fiction', 'Forrest Gump', 'The Shawshank Redemption' ], 'description': [ 'A computer hacker learns about the true nature of reality and his role in the war against its controllers.', 'A thief who steals corporate secrets through dream-sharing is given an inverse task of planting an idea.', 'A team of explorers travel through a wormhole in space in an attempt to ensure humanity’s survival.', 'Batman faces the Joker, a criminal mastermind who plunges Gotham into chaos.', 'An office worker forms an underground fight club that evolves into something much more.', 'The lives of two mob hitmen, a boxer, and others intertwine in a tale of violence and redemption.', 'The life journey of Forrest Gump, a man with a low IQ who achieves incredible things.', 'Two imprisoned men bond over a number of years, finding solace and eventual redemption.' ] } # Create a DataFrame df = pd.DataFrame(data) # Vectorize the descriptions using TF-IDF tfidf = TfidfVectorizer(stop_words='english') tfidf_matrix = tfidf.fit_transform(df['description']) # Compute cosine similarity cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix) # Function to get recommendations def get_recommendations(title, cosine_sim=cosine_sim): # Get the index of the movie that matches the title idx = df.index[df['title'] == title].tolist() if not idx: return ["Movie not found."] idx = idx[0] # Get the pairwise similarity scores of all movies with that movie sim_scores = list(enumerate(cosine_sim[idx])) # Sort the movies based on similarity scores sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True) # Get the scores of the 5 most similar movies sim_scores = sim_scores[1:6] # Get the movie indices movie_indices = [i[0] for i in sim_scores] # Return the top 5 most similar movies return df['title'].iloc[movie_indices].tolist() # Example usage if __name__ == "__main__": user_input = input("Enter a movie you like: ") recommendations = get_recommendations(user_input) print("nYou might also like:") for movie in recommendations: print("- " + movie)

How It Works:

  • Takes user input (a movie title).

  • Finds similar movies based on TF-IDF of the movie description.

  • Returns the top 5 most similar movies.

To Enhance This Bot:

  • Use a larger dataset like MovieLens.

  • Add genre-based or collaborative filtering logic.

  • Build a web-based version with Flask or FastAPI.

  • Include user ratings for better personalization.

Let me know if you’d like the bot in a different format (like a chatbot, web app, or Telegram bot).

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About