zenaight commited on
Commit
804b635
·
1 Parent(s): b0577bf

Enhance city extraction and search specificity in property search

Browse files

- Improved city extraction from user messages with additional pattern matching for phrases like "show me [city]" and "show [city]".
- Introduced logic to differentiate between broad and specific property searches based on user input.
- Adjusted persona filters to apply only for specific searches, enhancing the accuracy of property suggestions.
- Updated fallback messages to provide clearer feedback based on search type and available cities.

Files changed (1) hide show
  1. ai_chat.py +46 -9
ai_chat.py CHANGED
@@ -191,21 +191,53 @@ async def handle_property_search(user_message, persona):
191
  if persona.get("location_preference"):
192
  city = persona["location_preference"]
193
  else:
194
- # Try to extract city from message (very basic, can be improved)
 
195
  match = re.search(r"in ([a-zA-Z ]+)", user_message, re.IGNORECASE)
196
  if match:
197
  city = match.group(1).strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
- # Optionally extract size, price, features from persona
200
- min_size = persona.get("size_preference_sqm")
201
- features = persona.get("must_have")
 
 
 
 
 
 
 
202
  price_type = None
203
- if persona.get("intent") in ["lease", "rent"]:
204
- price_type = "lease"
205
- elif persona.get("intent") == "buy":
206
- price_type = "sale"
 
 
 
 
 
207
 
208
  # Search properties
 
209
  print(f"Searching properties with filters: city={city}, min_size={min_size}, features={features}, price_type={price_type}")
210
  properties = await search_properties(city=city, min_size=min_size, features=features, price_type=price_type, limit=5)
211
  print(f"Found {len(properties)} properties")
@@ -226,7 +258,10 @@ async def handle_property_search(user_message, persona):
226
  if city:
227
  # Check if the city is actually in available cities
228
  if city.lower() in [c.lower() for c in available_cities]:
229
- msg = f"Sorry, we currently have no properties available in {city.title()} that match your specific requirements."
 
 
 
230
  else:
231
  msg = f"Sorry, we currently have no properties available in {city.title()}."
232
  else:
@@ -234,6 +269,8 @@ async def handle_property_search(user_message, persona):
234
 
235
  if available_cities:
236
  msg += f"\n\nWe do have properties in: {', '.join(available_cities)}."
 
 
237
  return [msg]
238
 
239
  async def process_message(user_message: str, user_info: dict = None, session_id: str = None, wa_id: str = None, wamid: str = None):
 
191
  if persona.get("location_preference"):
192
  city = persona["location_preference"]
193
  else:
194
+ # Try to extract city from message (improved pattern matching)
195
+ # Pattern 1: "in [city]"
196
  match = re.search(r"in ([a-zA-Z ]+)", user_message, re.IGNORECASE)
197
  if match:
198
  city = match.group(1).strip()
199
+ else:
200
+ # Pattern 2: "show me [city]" or "show [city]"
201
+ match = re.search(r"show me ([a-zA-Z ]+)", user_message, re.IGNORECASE)
202
+ if match:
203
+ city = match.group(1).strip()
204
+ else:
205
+ # Pattern 3: "show [city]"
206
+ match = re.search(r"show ([a-zA-Z ]+)", user_message, re.IGNORECASE)
207
+ if match:
208
+ city = match.group(1).strip()
209
+
210
+ # Determine search specificity based on user message
211
+ user_message_lower = user_message.lower()
212
+
213
+ # Check if this is a broad search (just city) or specific search
214
+ broad_search_indicators = [
215
+ "show me", "show", "listings", "properties", "warehouses", "eiendomme", "toon my"
216
+ ]
217
 
218
+ specific_search_indicators = [
219
+ "matching", "that match", "with", "for my", "suitable for", "perfect for", "ideal for"
220
+ ]
221
+
222
+ is_broad_search = any(indicator in user_message_lower for indicator in broad_search_indicators)
223
+ is_specific_search = any(indicator in user_message_lower for indicator in specific_search_indicators)
224
+
225
+ # Use persona filters only for specific searches, not broad searches
226
+ min_size = None
227
+ features = None
228
  price_type = None
229
+
230
+ if is_specific_search or not is_broad_search:
231
+ # Use persona filters for specific searches
232
+ min_size = persona.get("size_preference_sqm")
233
+ features = persona.get("must_have")
234
+ if persona.get("intent") in ["lease", "rent"]:
235
+ price_type = "lease"
236
+ elif persona.get("intent") == "buy":
237
+ price_type = "sale"
238
 
239
  # Search properties
240
+ print(f"Search type: {'Broad' if is_broad_search else 'Specific'}")
241
  print(f"Searching properties with filters: city={city}, min_size={min_size}, features={features}, price_type={price_type}")
242
  properties = await search_properties(city=city, min_size=min_size, features=features, price_type=price_type, limit=5)
243
  print(f"Found {len(properties)} properties")
 
258
  if city:
259
  # Check if the city is actually in available cities
260
  if city.lower() in [c.lower() for c in available_cities]:
261
+ if is_broad_search:
262
+ msg = f"Sorry, we currently have no properties available in {city.title()}."
263
+ else:
264
+ msg = f"Sorry, we currently have no properties available in {city.title()} that match your specific requirements."
265
  else:
266
  msg = f"Sorry, we currently have no properties available in {city.title()}."
267
  else:
 
269
 
270
  if available_cities:
271
  msg += f"\n\nWe do have properties in: {', '.join(available_cities)}."
272
+ if city and city.lower() in [c.lower() for c in available_cities]:
273
+ msg += f"\n\nTry asking for properties in {city.title()} without specific requirements, or let me know what you're looking for!"
274
  return [msg]
275
 
276
  async def process_message(user_message: str, user_info: dict = None, session_id: str = None, wa_id: str = None, wamid: str = None):