-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserinterface_and_search.py
More file actions
192 lines (164 loc) · 7.17 KB
/
userinterface_and_search.py
File metadata and controls
192 lines (164 loc) · 7.17 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import logging
import lancedb
import os
import numpy as np
import datetime
from sentence_transformers import SentenceTransformer
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def initialize_database():
"""Initialize database connection"""
try:
# Get the current directory and create a path for the database
current_dir = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(current_dir, 'lance_db')
os.makedirs(db_path, exist_ok=True)
# Connect to the database
db = lancedb.connect(db_path)
return db
except Exception as e:
logger.error(f"Database initialization error: {str(e)}")
raise
# Initialize the embedding model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Define weighting per category
SEARCH_WEIGHTS = {
"size": 0.35, # Size and layout
"location": 0.35, # Location features
"amenities": 0.20, # Property amenities
"transport": 0.15, # Transportation options
"urban": 0.15 # Urban/suburban balance
}
def create_category_queries(preference_answers):
"""Map preference answers to search categories."""
return {
"size": preference_answers[0],
"location": preference_answers[1],
"amenities": preference_answers[2],
"transport": preference_answers[3],
"urban": preference_answers[4]
}
def convert_to_rating(score):
"""Convert similarity score to 1-5 rating with weight scaling"""
# Calculate average weight to use as scaling factor
avg_weight = sum(SEARCH_WEIGHTS.values()) / len(SEARCH_WEIGHTS)
# Apply weight-based scaling with exponential factor
scaled_score = score * (1 + avg_weight) ** 2
# Convert to 1-5 range with rounding
return min(max(round(scaled_score * 5), 1), 5)
def get_listings(preference_answers, k=5):
try:
# Initialize database and open listings table
db = initialize_database()
table = db.open_table("listings")
logger.info("Connected to database")
# Create category queries from preference answers
category_queries = create_category_queries(preference_answers)
property_scores = {}
# Iterate over each category and perform search
for category, query in category_queries.items():
query_vector = model.encode(query).astype(np.float32)
results = table.search(
query=query_vector,
vector_column_name="embedding"
).limit(k).to_list()
# Process search results
for r in results:
if '_distance' in r:
property_id = r['id']
similarity_score = 1 / (1 + r['_distance'])
weighted_score = similarity_score * SEARCH_WEIGHTS[category]
if property_id not in property_scores:
property_scores[property_id] = {
'total_score': 0,
'category_scores': {},
'details': {
'description': r['description'],
'neighborhood': r['neighborhood'],
'price': r['price'],
'location': r['location'],
'bedrooms': r['bedrooms'],
'bathrooms': r['bathrooms'],
'size': r['size']
}
}
# Store category scores and update total score
property_scores[property_id]['category_scores'][category] = convert_to_rating(weighted_score)
property_scores[property_id]['total_score'] += weighted_score
# Convert total scores to 1 to 5 rating
for prop_data in property_scores.values():
prop_data['rating'] = convert_to_rating(prop_data['total_score'])
# Sort properties by rating
sorted_properties = sorted(
property_scores.items(),
key=lambda x: x[1]['rating'],
reverse=True
)
if not sorted_properties:
return []
# Display all properties with ratings
print("\n=== ALL PROPERTIES RATINGS ===")
for prop_id, prop_data in sorted_properties:
print(f"\nProperty ID: {prop_id}")
print(f"Location: {prop_data['details']['location']}")
print(f"Price: {prop_data['details']['price']}")
print(f"Size: {prop_data['details']['size']}")
print(f"Bedrooms: {prop_data['details']['bedrooms']}")
print(f"Bathrooms: {prop_data['details']['bathrooms']}")
print("\nCategory Ratings (1-5):")
for cat, score in prop_data['category_scores'].items():
print(f"- {cat.title()}: {score}/5")
print(f"OVERALL RATING: {prop_data['rating']}/5")
print("-" * 30)
except Exception as e:
logger.error(f"Search error: {str(e)}")
return []
return sorted_properties
def similarity_search_store(sorted_properties):
"""Store top 3 recommendations in the database"""
db = initialize_database()
# Display top 3 recommendations
print("\n" + "=" * 50)
print("TOP 3 RECOMMENDATIONS".center(50))
print("=" * 50)
top_recommendations = []
for i, (prop_id, prop_data) in enumerate(sorted_properties[:3], 1):
print(f"\n{i}. RECOMMENDED PROPERTY")
print("-" * 20)
print(f"Property ID: {prop_id}")
print(f"Location: {prop_data['details']['location']}")
print(f"Price: {prop_data['details']['price']}")
print(f"Size: {prop_data['details']['size']}")
print(f"Bedrooms: {prop_data['details']['bedrooms']}")
print(f"Bathrooms: {prop_data['details']['bathrooms']}")
print("\nCategory Ratings (1-5):")
for cat, score in prop_data['category_scores'].items():
print(f"- {cat.title()}: {score}/5")
print(f"\nOVERALL RATING: {prop_data['rating']}/5")
print("=" * 50)
top_recommendations.append({
'id': prop_id,
**prop_data['details'],
'rating': prop_data['rating'],
'category_ratings': prop_data['category_scores']
})
# Store top 3 recommendations in LanceDB
db.create_table(
"top_recommendations",
data=[{
'id': prop_id,
'description': prop_data['details']['description'],
'price': prop_data['details']['price'],
'location': prop_data['details']['location'],
'bedrooms': prop_data['details']['bedrooms'],
'bathrooms': prop_data['details']['bathrooms'],
'size': prop_data['details']['size'],
'overall_rating': prop_data['rating'],
'category_ratings': str(prop_data['category_scores']),
'neighborhood': prop_data['details']['neighborhood'],
'timestamp': datetime.datetime.now().isoformat()
} for prop_id, prop_data in sorted_properties[:3]],
mode='overwrite'
)
return top_recommendations