|
| 1 | +import spacy |
| 2 | + |
| 3 | +# Load the spaCy English language model |
| 4 | +nlp = spacy.load("en_core_web_sm") |
| 5 | + |
| 6 | +# Function to get the response from the chatbot |
| 7 | +def get_response(query): |
| 8 | + # Process the user query |
| 9 | + doc = nlp(query) |
| 10 | + |
| 11 | + # Extract important information from the user query (e.g., named entities) |
| 12 | + named_entities = [ent.text for ent in doc.ents] |
| 13 | + |
| 14 | + # Example: Detecting greetings and responding accordingly |
| 15 | + greetings = ["hello", "hi", "hey", "howdy"] |
| 16 | + if any(greet in query.lower() for greet in greetings): |
| 17 | + return "Hello! How can I assist you today?" |
| 18 | + |
| 19 | + # Example: Handling 'who' questions |
| 20 | + if "who" in query.lower() and "is" in query.lower(): |
| 21 | + # Replace 'who is' with an empty string to get the subject of the question |
| 22 | + subject = query.lower().replace("who is", "").strip() |
| 23 | + if subject: |
| 24 | + # Custom logic for specific topics |
| 25 | + if subject == "python": |
| 26 | + return "Python is a popular programming language known for its simplicity and versatility." |
| 27 | + elif subject == "ai": |
| 28 | + return "Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that can perform tasks that typically require human intelligence." |
| 29 | + elif subject == "elon musk": |
| 30 | + return "Elon Musk is a visionary entrepreneur and CEO of companies like SpaceX and Tesla, known for his ambitious goals in space exploration and sustainable energy." |
| 31 | + |
| 32 | + # Example: Handling 'what' questions |
| 33 | + if "what" in query.lower() and "is" in query.lower(): |
| 34 | + # Replace 'what is' with an empty string to get the subject of the question |
| 35 | + subject = query.lower().replace("what is", "").strip() |
| 36 | + if subject: |
| 37 | + # Custom logic for specific topics |
| 38 | + if subject == "python": |
| 39 | + return "Python is a popular programming language known for its simplicity and versatility." |
| 40 | + elif subject == "ai": |
| 41 | + return "Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that can perform tasks that typically require human intelligence." |
| 42 | + elif subject == "elon musk": |
| 43 | + return "Elon Musk is a visionary entrepreneur and CEO of companies like SpaceX and Tesla, known for his ambitious goals in space exploration and sustainable energy." |
| 44 | + elif subject == "global warming": |
| 45 | + return "Global warming is the long-term increase in Earth's average surface temperature due to human activities like burning fossil fuels and deforestation." |
| 46 | + |
| 47 | + # Example: Handling 'where' questions |
| 48 | + if "where" in query.lower(): |
| 49 | + # Custom logic for specific locations |
| 50 | + if "city" in named_entities: |
| 51 | + city = named_entities[named_entities.index("city") + 1] |
| 52 | + if city == "new york": |
| 53 | + return "New York City, often called NYC, is a major city in the United States known for its culture, arts, and diverse population." |
| 54 | + elif city == "paris": |
| 55 | + return "Paris is the capital city of France and is famous for its art, history, and iconic landmarks like the Eiffel Tower." |
| 56 | + |
| 57 | + # Example: Handling 'when' questions |
| 58 | + if "when" in query.lower(): |
| 59 | + # Custom logic for specific events |
| 60 | + if "world war 2" in query.lower(): |
| 61 | + return "World War II took place from 1939 to 1945, involving many countries and causing significant global impact." |
| 62 | + |
| 63 | + # If no specific response is generated, provide a default response |
| 64 | + return "I'm sorry, but I'm not sure how to help with that." |
| 65 | + |
| 66 | +# Main chat loop |
| 67 | +print("Chatbot: Hello! How can I assist you today? Type 'exit' to end the conversation.") |
| 68 | +while True: |
| 69 | + user_input = input("You: ") |
| 70 | + if user_input.lower() == "exit": |
| 71 | + print("Chatbot: Goodbye!") |
| 72 | + break |
| 73 | + response = get_response(user_input) |
| 74 | + print("Chatbot:", response) |
0 commit comments