-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
59 lines (50 loc) · 2.06 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import random
import spacy
print("ChatBot Prototype 2")
# Load the spaCy model
nlp = spacy.load('en_core_web_sm')
# Sample data structure
health_data = {
"diet_type": {
"eggetarian": "You follow an eggetarian diet.",
"vegetarian": "You follow a vegetarian diet.",
"non-vegetarian": "You follow a non-vegetarian diet.",
"jain": "You follow a Jain diet."
},
"chronic_illnesses": {
"diabetes": "Managing diabetes requires monitoring your carbohydrate intake.",
"hypertension": "For hypertension, focus on low-sodium foods."
},
"dietary_restrictions": {
"lactose_intolerance": "Avoid dairy products.",
"gluten_free": "Avoid wheat and barley."
},
"health_goals": {
"bp_control": "For blood pressure control, consider reducing salt intake.",
"sugar_control": "Monitor your sugar intake."
}
}
# Function to get response based on user input
def chatbot_response(user_input):
tokens = nlp(user_input.lower())
response = "I'm sorry, I didn't understand that."
# Check for diet type
if any(token.text in ['diet', 'vegetarian', 'non-vegetarian', 'eggetarian', 'jain'] for token in tokens):
response = random.choice(list(health_data['diet_type'].values()))
# Check for chronic illnesses
elif any(token.text in ['diabetes', 'hypertension'] for token in tokens):
response = random.choice(list(health_data['chronic_illnesses'].values()))
# Check for dietary restrictions
elif any(token.text in ['lactose', 'gluten'] for token in tokens):
response = random.choice(list(health_data['dietary_restrictions'].values()))
# Check for health goals
elif any(token.text in ['blood pressure', 'sugar'] for token in tokens):
response = random.choice(list(health_data['health_goals'].values()))
return response
# Example interaction
while True:
user_input = input("You: ")
if user_input.lower() in ['exit', 'quit']:
print("Chatbot: Goodbye!")
break
print("Chatbot:", chatbot_response(user_input))