privateuserh commited on
Commit
cee0d52
·
verified ·
1 Parent(s): f21410a

Update main.js

Browse files
Files changed (1) hide show
  1. main.js +72 -48
main.js CHANGED
@@ -107,57 +107,81 @@ document.addEventListener('DOMContentLoaded', () => {
107
  }
108
 
109
  function handleSearch() {
110
- const searchTerm = userInput.value.trim();
111
- if (!searchTerm) return;
112
- addUserMessage(searchTerm);
113
- userInput.value = '';
114
- const searchKeywords = ['search', 'find', 'show', 'where is', 'landmark', 'park', 'beach', 'hollywood', 'map', 'navigate'];
115
- const isLocalSearch = searchKeywords.some(keyword => searchTerm.toLowerCase().includes(keyword));
116
- if (isLocalSearch) {
117
- if (poisData.length === 0) return addAIMessage("Location data is still loading...");
118
- const results = poisData.filter(poi =>
119
- poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
120
- (poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
121
- );
122
- if (results.length > 0) {
123
- let responseMessage = `I found ${results.length} local result(s):<ul>`;
124
- results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
125
- responseMessage += "</ul>";
126
- addAIMessage(responseMessage);
127
- } else {
128
- addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
129
- }
130
- } else {
131
- addAIMessage("AI is thinking...");
132
- fetch(`${config.backendUrl}/api/ask`, {
133
- method: 'POST',
134
- headers: { 'Content-Type': 'application/json' },
135
- body: JSON.stringify({ prompt: searchTerm })
136
- })
137
- .then(response => {
138
- if (!response.ok) throw new Error('Network response was not ok');
139
- return response.json();
140
- })
141
- .then(data => {
142
- const chatContainer = aiAssistant.querySelector('.flex-col');
143
- const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
144
- if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
145
- thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>');
146
- } else {
147
- addAIMessage(data.response.replace(/\n/g, '<br>'));
148
- }
149
- })
150
- .catch(error => {
151
- console.error('Error asking AI:', error);
152
- const chatContainer = aiAssistant.querySelector('.flex-col');
153
- const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
154
- if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
155
- thinkingMessage.querySelector('p').innerHTML = "Sorry, I had trouble connecting to the AI.";
156
- } else {
157
- addAIMessage("Sorry, I had trouble connecting to the AI.");
158
  }
 
 
159
  });
 
 
 
 
 
 
 
 
 
160
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  } // <-- THIS CLOSING BRACE WAS MISSING
162
 
163
  // --- EVENT LISTENERS ---
 
107
  }
108
 
109
  function handleSearch() {
110
+ const searchTerm = userInput.value.trim();
111
+ if (!searchTerm) return;
112
+
113
+ addUserMessage(searchTerm);
114
+ userInput.value = '';
115
+
116
+ const searchKeywords = ['search', 'find', 'show', 'where is', 'landmark', 'park', 'beach', 'hollywood', 'map', 'navigate'];
117
+ const isLocalSearch = searchKeywords.some(keyword => searchTerm.toLowerCase().includes(keyword));
118
+
119
+ if (isLocalSearch) {
120
+ // --- NEW, MORE FORGIVING SEARCH LOGIC ---
121
+ const stopWords = ['show', 'me', 'find', 'is', 'a', 'the', 'for', 'where', 'search', 'of', 'at'];
122
+ const searchTokens = searchTerm.toLowerCase().split(' ').filter(word => !stopWords.includes(word) && word.length > 2);
123
+
124
+ if (searchTokens.length === 0) {
125
+ return addAIMessage("Please be more specific in your local search.");
126
+ }
127
+
128
+ const results = poisData.filter(poi => {
129
+ const poiText = (poi.name + ' ' + (poi.description || '')).toLowerCase();
130
+
131
+ // Check if the POI text contains any of the search keywords
132
+ return searchTokens.some(token => {
133
+ // First, check for a direct match
134
+ if (poiText.includes(token)) return true;
135
+
136
+ // NEW: If no direct match and the word is plural,
137
+ // check for the singular version too (e.g., "landmarks" matches "landmark")
138
+ if (token.endsWith('s')) {
139
+ return poiText.includes(token.slice(0, -1));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  }
141
+
142
+ return false;
143
  });
144
+ });
145
+
146
+ if (results.length > 0) {
147
+ let responseMessage = `I found ${results.length} local result(s):<ul>`;
148
+ results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
149
+ responseMessage += "</ul>";
150
+ addAIMessage(responseMessage);
151
+ } else {
152
+ addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
153
  }
154
+
155
+ } else {
156
+ // --- ASK THE GEMINI AI (Unchanged) ---
157
+ addAIMessage("AI is thinking...");
158
+ fetch(`${config.backendUrl}/api/ask`, {
159
+ method: 'POST',
160
+ headers: { 'Content-Type': 'application/json' },
161
+ body: JSON.stringify({ prompt: searchTerm })
162
+ })
163
+ .then(response => { if (!response.ok) throw new Error('Network error'); return response.json(); })
164
+ .then(data => {
165
+ const chatContainer = aiAssistant.querySelector('.flex-col');
166
+ const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
167
+ if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
168
+ thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>');
169
+ } else {
170
+ addAIMessage(data.response.replace(/\n/g, '<br>'));
171
+ }
172
+ })
173
+ .catch(error => {
174
+ console.error('Error asking AI:', error);
175
+ const chatContainer = aiAssistant.querySelector('.flex-col');
176
+ const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
177
+ if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
178
+ thinkingMessage.querySelector('p').innerHTML = "Sorry, I had trouble connecting to the AI.";
179
+ } else {
180
+ addAIMessage("Sorry, I had trouble connecting to the AI.");
181
+ }
182
+ });
183
+ }
184
+ }
185
  } // <-- THIS CLOSING BRACE WAS MISSING
186
 
187
  // --- EVENT LISTENERS ---