CAntoniadis commited on
Commit
dfdac3f
·
verified ·
1 Parent(s): da3f35d

Fixed indentation error at line 142

Browse files
Files changed (1) hide show
  1. simple_search_engine/search_engine.py +36 -35
simple_search_engine/search_engine.py CHANGED
@@ -140,38 +140,39 @@ class SimpleSearchEngine:
140
  return results
141
 
142
  def search_hybrid(self, query: str, top_k: int = 5, alpha: float = 0.5):
143
- if self.vectorizer is None or self.doc_tfidf is None or self.bm25 is None:
144
- raise RuntimeError("Indexes not built. Call build_index() first.")
145
-
146
- # TF-IDF scores
147
- query_vec = self.vectorizer.transform([query])
148
- tfidf_scores = linear_kernel(query_vec, self.doc_tfidf).flatten()
149
-
150
- # BM25 scores
151
- q_tokens = _tokenize(query)
152
- bm25_scores = np.array(self.bm25.get_scores(q_tokens), dtype=float)
153
-
154
- # Normalize
155
- tfidf_norm = self._minmax_normalize(tfidf_scores)
156
- bm25_norm = self._minmax_normalize(bm25_scores)
157
-
158
- # Combine
159
- hybrid_scores = alpha * tfidf_norm + (1 - alpha) * bm25_norm
160
- print(f"[DEBUG][HYBRID] query={query} alpha={alpha}")
161
- print("[DEBUG][HYBRID] top5_hybrid=", hybrid_scores[hybrid_scores.argsort()[::-1][:5]])
162
-
163
- ranked_idx = hybrid_scores.argsort()[::-1][:top_k]
164
-
165
- results = []
166
- for idx in ranked_idx:
167
- doc = self.documents[int(idx)]
168
- snippet = doc["text"][:200] + ("..." if len(doc["text"]) > 200 else "")
169
- results.append({
170
- "score": float(hybrid_scores[int(idx)]),
171
- "title": doc["title"],
172
- "url": doc["url"],
173
- "path": doc["path"],
174
- "snippet": snippet,
175
- })
176
-
177
- return results
 
 
140
  return results
141
 
142
  def search_hybrid(self, query: str, top_k: int = 5, alpha: float = 0.5):
143
+ if self.vectorizer is None or self.doc_tfidf is None or self.bm25 is None:
144
+ raise RuntimeError("Indexes not built. Call build_index() first.")
145
+
146
+ # TF-IDF scores
147
+ query_vec = self.vectorizer.transform([query])
148
+ tfidf_scores = linear_kernel(query_vec, self.doc_tfidf).flatten()
149
+
150
+ # BM25 scores
151
+ q_tokens = _tokenize(query)
152
+ bm25_scores = np.array(self.bm25.get_scores(q_tokens), dtype=float)
153
+
154
+ # Normalize
155
+ tfidf_norm = self._minmax_normalize(tfidf_scores)
156
+ bm25_norm = self._minmax_normalize(bm25_scores)
157
+
158
+ # Combine
159
+ hybrid_scores = alpha * tfidf_norm + (1 - alpha) * bm25_norm
160
+ print(f"[DEBUG][HYBRID] query={query} alpha={alpha}")
161
+ print("[DEBUG][HYBRID] top5_hybrid=",
162
+ hybrid_scores[hybrid_scores.argsort()[::-1][:5]])
163
+
164
+ ranked_idx = hybrid_scores.argsort()[::-1][:top_k]
165
+
166
+ results = []
167
+ for idx in ranked_idx:
168
+ doc = self.documents[int(idx)]
169
+ snippet = doc["text"][:200] + ("..." if len(doc["text"]) > 200 else "")
170
+ results.append({
171
+ "score": float(hybrid_scores[int(idx)]),
172
+ "title": doc["title"],
173
+ "url": doc["url"],
174
+ "path": doc["path"],
175
+ "snippet": snippet,
176
+ })
177
+
178
+ return results