Spaces:
Runtime error
Runtime error
Fixed indentation error at line 142
Browse files
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 |
-
|
| 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 |
-
|
|
|
|
|
|
| 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
|