Skip to content

Commit 25759f4

Browse files
Merge pull request #2555 from andoriyaprashant/branch21
AI-based Chatbot for Diverse Topics Script Added
2 parents 9bd43cf + 3be8e4b commit 25759f4

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# AI-Based Chatbot
2+
3+
An AI-based chatbot implemented in Python using the spaCy NLP library. The chatbot is designed to engage in conversations on a wide range of topics, from general knowledge to specific domains. It can understand user queries and respond in a relevant and informative manner.
4+
5+
## Requirements
6+
7+
- Python 3.x
8+
- spaCy (Install using: `pip install spacy`)
9+
- spaCy English language model (Install using: `python -m spacy download en_core_web_sm`)
10+
11+
## Usage
12+
13+
1. Clone the repository to your local machine
14+
15+
2. Install the required dependencies:
16+
17+
```bash
18+
pip install -r requirements.txt
19+
```
20+
21+
3. Run the chatbot:
22+
23+
```bash
24+
python chatbot.py
25+
```
26+
27+
## Features
28+
29+
- Responds to greetings and basic conversation starters.
30+
- Handles 'who', 'what', 'where', and 'when' and many more questions with custom responses for specific topics.
31+
- Able to identify named entities like cities and provide relevant information.
32+
33+
## Customization
34+
35+
You can extend the chatbot's functionality by adding more domain-specific logic and custom responses. Update the `get_response()` function in the `chatbot.py` script to handle additional topics or integrate external APIs for more comprehensive responses.
36+
37+
## Contributing
38+
39+
Contributions to the project are welcome! If you have any improvements, bug fixes, or new features to add, feel free to submit a pull request.
40+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spacy

0 commit comments

Comments
 (0)