Swathi6 commited on
Commit
c5e398b
·
verified ·
1 Parent(s): bb63c76

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +55 -54
static/script.js CHANGED
@@ -1,71 +1,72 @@
1
- // Example restaurant data for menu suggestions
2
- const menu = {
3
- vegan: ['Vegan Burger', 'Tofu Stir Fry', 'Veggie Pizza'],
4
- vegetarian: ['Cheese Pizza', 'Pasta Alfredo', 'Caprese Salad'],
5
- non_vegetarian: ['Chicken Curry', 'Grilled Salmon', 'Steak'],
6
- spicy: ['Spicy Tacos', 'Chili Chicken', 'Spicy Wings'],
7
  };
8
 
9
- document.getElementById("chat-form").addEventListener("submit", function(e) {
10
- e.preventDefault();
 
 
 
11
 
12
- const userInput = document.getElementById("user-input").value;
13
- document.getElementById("user-input").value = ''; // Clear input box
14
- addMessage(userInput, 'user-message');
15
 
16
- // Get bot response based on user input
17
- getBotResponse(userInput);
18
- });
19
 
20
- function addMessage(message, className) {
21
- const messageDiv = document.createElement('div');
22
- messageDiv.classList.add('message', className);
23
- messageDiv.innerText = message;
24
- document.getElementById("chat-box").appendChild(messageDiv);
25
- scrollToBottom();
26
  }
27
 
28
- function scrollToBottom() {
29
- const chatBox = document.getElementById("chat-box");
30
- chatBox.scrollTop = chatBox.scrollHeight;
 
 
 
31
  }
32
 
33
- function getBotResponse(userInput) {
34
- const botResponse = getChatbotReply(userInput);
35
- addMessage(botResponse, 'bot-response');
36
- suggestMenu(userInput);
37
- }
38
-
39
- function getChatbotReply(userInput) {
40
- // Simulating a basic conversation
41
- if (userInput.toLowerCase().includes('hello') || userInput.toLowerCase().includes('hi')) {
42
- return "Hello! How can I help you today?";
43
- } else if (userInput.toLowerCase().includes('menu')) {
44
- return "Sure! What type of food are you looking for? Vegan, Vegetarian, Non-Vegetarian, or Spicy?";
45
  } else {
46
- return "Sorry, I didn't quite catch that. Can you rephrase?";
 
 
47
  }
 
 
 
48
  }
49
 
50
- function suggestMenu(userInput) {
51
- let userPreference = '';
 
 
 
 
 
 
 
52
 
53
- if (userInput.toLowerCase().includes('vegan')) {
54
- userPreference = 'vegan';
55
- } else if (userInput.toLowerCase().includes('vegetarian')) {
56
- userPreference = 'vegetarian';
57
- } else if (userInput.toLowerCase().includes('non-vegetarian')) {
58
- userPreference = 'non_vegetarian';
59
- } else if (userInput.toLowerCase().includes('spicy')) {
60
- userPreference = 'spicy';
61
- }
62
 
63
- const suggestedItems = menu[userPreference] || [];
 
 
 
 
 
 
64
 
65
- const menuDiv = document.getElementById('menu');
66
- if (suggestedItems.length > 0) {
67
- menuDiv.innerHTML = `<strong>Recommended Dishes:</strong><br>${suggestedItems.join(', ')}`;
68
- } else {
69
- menuDiv.innerHTML = "Sorry, I couldn't find any suggestions for that. Please specify your preference.";
70
- }
71
  }
 
1
+ const dishes = {
2
+ "rice": ["Biryani", "Fried Rice", "Pulao"],
3
+ "milk": ["Kheer", "Milkshake", "Payasam"],
4
+ "chicken": ["Chicken Curry", "Grilled Chicken", "Chicken Salad"],
5
+ "paneer": ["Paneer Butter Masala", "Paneer Tikka", "Palak Paneer"],
6
+ "potatoes": ["Aloo Gobi", "Aloo Tikki", "Mashed Potatoes"],
7
  };
8
 
9
+ function sendMessage() {
10
+ const userInput = document.getElementById('user-input').value.toLowerCase();
11
+ if (userInput.trim() === "") return;
12
+
13
+ displayMessage(userInput, 'user');
14
 
15
+ const ingredients = userInput.split(' ').map(ingredient => ingredient.trim());
16
+ const suggestedDishes = getDishSuggestions(ingredients);
 
17
 
18
+ setTimeout(() => {
19
+ displayBotMessage(suggestedDishes);
20
+ }, 1000);
21
 
22
+ document.getElementById('user-input').value = '';
 
 
 
 
 
23
  }
24
 
25
+ function displayMessage(message, sender) {
26
+ const messageElement = document.createElement('div');
27
+ messageElement.classList.add('message', sender === 'bot' ? 'bot-message' : 'user-message');
28
+ messageElement.textContent = message;
29
+ document.getElementById('chatbox-body').appendChild(messageElement);
30
+ document.getElementById('chatbox-body').scrollTop = document.getElementById('chatbox-body').scrollHeight;
31
  }
32
 
33
+ function displayBotMessage(dishes) {
34
+ let botMessage = 'Here are some dishes I suggest based on the ingredients you provided:\n\n';
35
+ if (dishes.length === 0) {
36
+ botMessage += 'Sorry, I couldn\'t find any dishes for the given ingredients.';
 
 
 
 
 
 
 
 
37
  } else {
38
+ dishes.forEach(dish => {
39
+ botMessage += `- ${dish}\n`;
40
+ });
41
  }
42
+
43
+ displayMessage(botMessage, 'bot');
44
+ displayFoodItems(dishes);
45
  }
46
 
47
+ function getDishSuggestions(ingredients) {
48
+ let suggestedDishes = [];
49
+ ingredients.forEach(ingredient => {
50
+ if (dishes[ingredient]) {
51
+ suggestedDishes = [...suggestedDishes, ...dishes[ingredient]];
52
+ }
53
+ });
54
+ return [...new Set(suggestedDishes)]; // Remove duplicates
55
+ }
56
 
57
+ function displayFoodItems(dishes) {
58
+ const foodItemsContainer = document.getElementById('food-items');
59
+ foodItemsContainer.innerHTML = ''; // Clear previous items
60
+ foodItemsContainer.style.display = dishes.length > 0 ? 'block' : 'none';
 
 
 
 
 
61
 
62
+ dishes.forEach(dish => {
63
+ const dishElement = document.createElement('div');
64
+ dishElement.textContent = dish;
65
+ dishElement.onclick = () => showFoodDescription(dish);
66
+ foodItemsContainer.appendChild(dishElement);
67
+ });
68
+ }
69
 
70
+ function showFoodDescription(dish) {
71
+ alert(`You selected: ${dish}\nHere is a brief description of ${dish}: ...`);
 
 
 
 
72
  }