| | import spacy |
| | import string |
| | import joblib |
| | |
| | model_filename = 'chatbot_model.joblib' |
| | loaded_model = joblib.load(model_filename) |
| |
|
| | |
| | nlp = spacy.load('en_core_web_sm', disable=['parser', 'ner']) |
| |
|
| | def preprocess_text(text): |
| | text = text.lower() |
| | text = text.translate(str.maketrans('', '', string.punctuation)) |
| | doc = nlp(text) |
| | text = " ".join([token.lemma_ for token in doc]) |
| | return text |
| |
|
| | def get_model_response(question): |
| | processed_question = preprocess_text(question) |
| | response = loaded_model.predict([processed_question])[0] |
| | return response |
| |
|
| | print("Your chatbot is ready. You can start a conversation or type 'quit' to exit.") |
| |
|
| | while True: |
| | try: |
| | user_input = input("You: ").strip() |
| |
|
| | if len(user_input) == 0: |
| | print("Chatbot: Please provide a valid input.") |
| | continue |
| |
|
| | if any(keyword in user_input.lower() for keyword in ["bye", "goodbye", "quit"]): |
| | print("Chatbot: Goodbye! It was a pleasure assisting you.") |
| | break |
| |
|
| | response = get_model_response(user_input) |
| |
|
| | if len(response) == 0: |
| | print("Chatbot: Empty response received. Please try again.") |
| | continue |
| |
|
| | print(f"Chatbot: {response}\n") |
| |
|
| | except KeyboardInterrupt: |
| | print("Chatbot: You ended the conversation. Goodbye!") |
| | break |