Spaces:
Runtime error
Runtime error
Upload checker/external/local_search.py with huggingface_hub
Browse files
checker/external/local_search.py
CHANGED
|
@@ -163,7 +163,7 @@ def load_cached_embeddings(articles):
|
|
| 163 |
return cache["embeddings"], cache["titles"].tolist()
|
| 164 |
|
| 165 |
|
| 166 |
-
def find_related_articles(user_text, threshold=0.55, top_n=10):
|
| 167 |
"""Find articles related to user's input using hybrid matching.
|
| 168 |
|
| 169 |
Uses cached embeddings for speed — only the user input is encoded live.
|
|
@@ -172,14 +172,27 @@ def find_related_articles(user_text, threshold=0.55, top_n=10):
|
|
| 172 |
1. Semantic similarity (multilingual MiniLM) — meaning-based
|
| 173 |
2. Keyword overlap — catches proper nouns (names, places) across languages
|
| 174 |
|
| 175 |
-
Minimum semantic similarity floor: articles must have at least
|
| 176 |
similarity regardless of keyword overlap, preventing keyword-inflated
|
| 177 |
false matches (e.g., an article about '40 years' matching because of
|
| 178 |
shared generic keywords like 'years' or 'president').
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
"""
|
| 180 |
# Minimum semantic similarity — regardless of keyword overlap, the article
|
| 181 |
# must actually be ABOUT the same topic at a basic meaning level.
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
model = get_model()
|
| 185 |
|
|
@@ -242,7 +255,7 @@ def find_related_articles(user_text, threshold=0.55, top_n=10):
|
|
| 242 |
|
| 243 |
combined_score = (semantic_score * 0.6) + (keyword_score * 0.4)
|
| 244 |
|
| 245 |
-
if combined_score >=
|
| 246 |
results.append(
|
| 247 |
{
|
| 248 |
"title": article["title"],
|
|
|
|
| 163 |
return cache["embeddings"], cache["titles"].tolist()
|
| 164 |
|
| 165 |
|
| 166 |
+
def find_related_articles(user_text, threshold=0.55, top_n=10, detected_lang="en"):
|
| 167 |
"""Find articles related to user's input using hybrid matching.
|
| 168 |
|
| 169 |
Uses cached embeddings for speed — only the user input is encoded live.
|
|
|
|
| 172 |
1. Semantic similarity (multilingual MiniLM) — meaning-based
|
| 173 |
2. Keyword overlap — catches proper nouns (names, places) across languages
|
| 174 |
|
| 175 |
+
Minimum semantic similarity floor: articles must have at least some semantic
|
| 176 |
similarity regardless of keyword overlap, preventing keyword-inflated
|
| 177 |
false matches (e.g., an article about '40 years' matching because of
|
| 178 |
shared generic keywords like 'years' or 'president').
|
| 179 |
+
|
| 180 |
+
For non-English (Cebuano/Tagalog) articles, both the floor and combined
|
| 181 |
+
threshold are lowered because MiniLM cross-lingual similarity between an
|
| 182 |
+
English-translated query and a native-language title is naturally lower
|
| 183 |
+
than English↔English comparison.
|
| 184 |
"""
|
| 185 |
# Minimum semantic similarity — regardless of keyword overlap, the article
|
| 186 |
# must actually be ABOUT the same topic at a basic meaning level.
|
| 187 |
+
# Lower floor for non-English: cross-lingual embedding similarity is
|
| 188 |
+
# naturally reduced when comparing an English translation against
|
| 189 |
+
# Cebuano/Tagalog titles stored in the DB.
|
| 190 |
+
if detected_lang not in ("en",):
|
| 191 |
+
SEMANTIC_FLOOR = 0.30 # was 0.40
|
| 192 |
+
effective_threshold = max(threshold - 0.15, 0.40) # e.g. 0.55 → 0.40
|
| 193 |
+
else:
|
| 194 |
+
SEMANTIC_FLOOR = 0.40
|
| 195 |
+
effective_threshold = threshold
|
| 196 |
|
| 197 |
model = get_model()
|
| 198 |
|
|
|
|
| 255 |
|
| 256 |
combined_score = (semantic_score * 0.6) + (keyword_score * 0.4)
|
| 257 |
|
| 258 |
+
if combined_score >= effective_threshold:
|
| 259 |
results.append(
|
| 260 |
{
|
| 261 |
"title": article["title"],
|