Ecommerce FAQ BOT USING NLP
Introduction:
Natural language processing (NLP) is a machine learning technology that gives computers the ability to interpret, manipulate, and comprehend human language.
WATCH ON YOUTUBE 😃👇:
CODE :
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Download NLTK data if not done already
nltk.download('punkt')
# Sample FAQ data (Replace with actual FAQs)
faq_data = {
"What is the return policy?": "You can return any item within 30 days of purchase if it meets our return guidelines.",
"How can I track my order?": "Once your order is shipped, you'll receive a tracking number via email.",
"What payment methods do you accept?": "We accept Visa, MasterCard, American Express, and PayPal.",
"How can I contact customer support?": "You can reach out to our customer support via the Contact Us page or call us at our support number.",
"Do you offer international shipping?": "Yes, we offer international shipping to selected countries. Additional fees may apply."
}
# Separate questions and answers for processing
questions = list(faq_data.keys())
answers = list(faq_data.values())
# Initialize the TF-IDF vectorizer
vectorizer = TfidfVectorizer()
# Fit the vectorizer on FAQ questions to create the TF-IDF matrix
tfidf_matrix = vectorizer.fit_transform(questions)
def get_answer(user_query):
"""Finds the best matching answer for the user's query."""
# Transform the user's query into the TF-IDF vector
query_tfidf = vectorizer.transform([user_query])
# Calculate cosine similarity between the user query and all FAQ questions
similarity_scores = cosine_similarity(query_tfidf, tfidf_matrix)
# Find the index of the best-matching question
best_match_index = similarity_scores.argmax()
best_match_score = similarity_scores[0, best_match_index]
# Threshold for similarity (adjust as needed)
similarity_threshold = 0.2 # Minimum required similarity score to consider a match
if best_match_score >= similarity_threshold:
return answers[best_match_index]
else:
return "I'm sorry, I couldn't find an answer to your question. Please contact customer support for more help."
# Main loop for testing the FAQ bot
print("Welcome to the FAQ Bot! Ask a question or type 'exit' to end.")
while True:
user_query = input("\nYou: ")
if user_query.lower() == 'exit':
print("Thank you for using the FAQ Bot! Goodbye.")
break
response = get_answer(user_query)
print("Bot:", response)
No comments:
Post a Comment