The Palos Publishing Company

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

Build a quote recommendation system

Here’s a basic Quote Recommendation System built using Python. This system recommends quotes to users based on their preferred categories using content-based filtering. For simplicity, it works offline with a static dataset of quotes.


🛠 Requirements

  • Python 3.7+

  • pandas

  • sklearn

Install dependencies:

bash
pip install pandas scikit-learn

📁 Step-by-Step Code

python
import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import linear_kernel # Sample quotes dataset data = [ {"quote": "The only limit to our realization of tomorrow is our doubts of today.", "author": "Franklin D. Roosevelt", "tags": "inspiration, future"}, {"quote": "In the middle of every difficulty lies opportunity.", "author": "Albert Einstein", "tags": "inspiration, motivation"}, {"quote": "Life is what happens when you're busy making other plans.", "author": "John Lennon", "tags": "life, reality"}, {"quote": "Success is not final, failure is not fatal: It is the courage to continue that counts.", "author": "Winston Churchill", "tags": "success, courage"}, {"quote": "Happiness depends upon ourselves.", "author": "Aristotle", "tags": "happiness, self"}, {"quote": "The purpose of our lives is to be happy.", "author": "Dalai Lama", "tags": "life, happiness"}, ] # Load into DataFrame df = pd.DataFrame(data) # Vectorize the tags using TF-IDF tfidf = TfidfVectorizer(stop_words='english') tfidf_matrix = tfidf.fit_transform(df['tags']) # Compute cosine similarity matrix cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix) # Mapping from quote index to tags indices = pd.Series(df.index, index=df['tags']) # Recommendation function def recommend_quotes(input_tag, top_n=3): input_vec = tfidf.transform([input_tag]) sim_scores = linear_kernel(input_vec, tfidf_matrix).flatten() sim_indices = sim_scores.argsort()[::-1][:top_n] recommendations = df.iloc[sim_indices][['quote', 'author']] return recommendations # Example usage user_interest = "happiness" results = recommend_quotes(user_interest) for i, row in results.iterrows(): print(f'"{row.quote}" — {row.author}')

🔄 Sample Output

If the user inputs "happiness":

arduino
"The purpose of our lives is to be happy." — Dalai Lama "Happiness depends upon ourselves." — Aristotle "Life is what happens when you're busy making other plans." — John Lennon

🔧 Optional Enhancements

  • Add a user feedback loop (like/dislike) and re-rank quotes.

  • Use NLP embeddings (like BERT) for deeper semantic understanding.

  • Store user preferences in a database.

  • Build a REST API using Flask or FastAPI for production deployment.

  • Add filters by author or time period.

Let me know if you’d like a web-based UI or database integration version.

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