import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer
import re
from nltk.corpus import stopwords
import nltk
import pickle
df = pd.read_csv('C:\\Projects\\Product Recommender System\\products(des).csv', encoding='latin1')

nltk.download('stopwords')
stop_words = set(stopwords.words('english'))


def preprocess_text(text, stop_words):
    if not isinstance(text, str):
        return ""
    text = re.sub(r'\W+', ' ', text)
    text = text.lower()
    text = ' '.join([word for word in text.split() if word not in stop_words])
    return text


df['processed_description'] = df['description'].apply(preprocess_text, stop_words=stop_words)

model = SentenceTransformer('all-MiniLM-L6-v2')

df['embeddings'] = df['processed_description'].apply(lambda x: model.encode(x))


def recommend_similar_products(product_name, top_n=10):
    if not product_name.strip():
        return "Please enter a valid product name."

    product_row = df[df['product_name'] == product_name]
    if product_row.empty:
        return f"Product '{product_name}' not found."

    product_embedding = product_row['embeddings'].values[0]

    similarities = []
    for idx, row in df.iterrows():
        if row['product_name'] != product_name:
            similarity = cosine_similarity(
                product_embedding.reshape(1, -1),
                row['embeddings'].reshape(1, -1)
            )[0][0]
            similarities.append((idx, similarity))

    similarities.sort(key=lambda x: x[1], reverse=True)

    top_recommendations = []
    for idx, similarity in similarities[:top_n]:
        top_recommendations.append({
            'product_name': df.loc[idx, 'product_name'],
            'description': df.loc[idx, 'description'],
            'similarity': similarity
        })

    return top_recommendations


# Save the model
with open('sentence_transformer_model.pkl', 'wb') as f:
    pickle.dump(model, f)

# Save the DataFrame with embeddings
with open('processed_data.pkl', 'wb') as f:
    pickle.dump(df, f)

while True:
    product_name = input("\n Enter The Food You Ate From Menu : ")

    if product_name.lower() == 'stop':
        print("Thankyou for ordering. Goodbye!")
        break

    recommendations = recommend_similar_products(product_name, top_n=10)

    if isinstance(recommendations, str):
        print(recommendations)
    else:
        print(f"\nHere are recommendations to '{product_name}' :")
        for i, rec in enumerate(recommendations):
            print(f"\nRecommendation Number {i + 1}:")
            print(f"Product: {rec['product_name']}")
            print(f"Description: {rec['description']}")
            print(f"Similarity: {rec['similarity']:.4f}")
