Spaces:
Sleeping
Sleeping
Delete search.py
Browse files
search.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
| 1 |
-
from typing import List, TypedDict
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
DATA_PATH = "data/index/demo_index.json"
|
| 6 |
-
|
| 7 |
-
class SearchResult(TypedDict):
|
| 8 |
-
document: str
|
| 9 |
-
agency: str
|
| 10 |
-
date: str
|
| 11 |
-
excerpt: str
|
| 12 |
-
citation: str
|
| 13 |
-
score: float
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def search_docs(query: str) -> List[SearchResult]:
|
| 17 |
-
query_l = query.lower()
|
| 18 |
-
results: List[SearchResult] = []
|
| 19 |
-
|
| 20 |
-
if not os.path.exists(DATA_PATH):
|
| 21 |
-
return results
|
| 22 |
-
|
| 23 |
-
with open(DATA_PATH, "r") as f:
|
| 24 |
-
docs = json.load(f)
|
| 25 |
-
|
| 26 |
-
for d in docs:
|
| 27 |
-
text = d["excerpt"].lower()
|
| 28 |
-
if query_l in text:
|
| 29 |
-
score = text.count(query_l) / max(len(text), 1)
|
| 30 |
-
results.append({
|
| 31 |
-
"document": d["document"],
|
| 32 |
-
"agency": d["agency"],
|
| 33 |
-
"date": d["date"],
|
| 34 |
-
"excerpt": d["excerpt"],
|
| 35 |
-
"citation": d["citation"],
|
| 36 |
-
"score": round(score, 4)
|
| 37 |
-
})
|
| 38 |
-
|
| 39 |
-
return sorted(results, key=lambda x: x["score"], reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|