Spaces:
Running
Running
Delete benchmarks/search/dataset.py
Browse files- benchmarks/search/dataset.py +0 -76
benchmarks/search/dataset.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import json
|
| 4 |
-
from collections import Counter
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
from typing import Any
|
| 7 |
-
|
| 8 |
-
DOC_MARKER_PREFIX = "DOCID "
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def doc_marker(doc_id: str) -> str:
|
| 12 |
-
return f"{DOC_MARKER_PREFIX}{doc_id}"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def write_records(rows: list[dict[str, Any]], path: Path) -> Path:
|
| 16 |
-
path.parent.mkdir(parents=True, exist_ok=True)
|
| 17 |
-
with path.open("w", encoding="utf-8") as fh:
|
| 18 |
-
for row in rows:
|
| 19 |
-
fh.write(json.dumps(row, ensure_ascii=False) + "\n")
|
| 20 |
-
return path
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def load_records(path: Path) -> list[dict[str, Any]]:
|
| 24 |
-
rows: list[dict[str, Any]] = []
|
| 25 |
-
for line in path.read_text(encoding="utf-8").splitlines():
|
| 26 |
-
line = line.strip()
|
| 27 |
-
if not line:
|
| 28 |
-
continue
|
| 29 |
-
rows.append(json.loads(line))
|
| 30 |
-
return rows
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def split_corpus(
|
| 34 |
-
rows: list[dict[str, Any]],
|
| 35 |
-
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
|
| 36 |
-
docs: list[dict[str, Any]] = []
|
| 37 |
-
queries: list[dict[str, Any]] = []
|
| 38 |
-
for row in rows:
|
| 39 |
-
kind = str(row.get("type") or "").strip().lower()
|
| 40 |
-
if kind == "doc":
|
| 41 |
-
docs.append(row)
|
| 42 |
-
elif kind == "query":
|
| 43 |
-
queries.append(row)
|
| 44 |
-
else:
|
| 45 |
-
raise ValueError(f"Unknown record type {kind!r} in {row!r}")
|
| 46 |
-
return docs, queries
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def dataset_stats(rows: list[dict[str, Any]]) -> dict[str, Any]:
|
| 50 |
-
docs, queries = split_corpus(rows)
|
| 51 |
-
slices = Counter(str(q.get("slice") or "unspecified") for q in queries)
|
| 52 |
-
return {
|
| 53 |
-
"n_docs": len(docs),
|
| 54 |
-
"n_queries": len(queries),
|
| 55 |
-
"slices": dict(slices),
|
| 56 |
-
"doc_ids": [str(d.get("doc_id") or "") for d in docs],
|
| 57 |
-
"graded": any(bool(q.get("gold_grades")) for q in queries),
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def map_doc_ids_to_chunks(
|
| 62 |
-
docs: list[dict[str, Any]],
|
| 63 |
-
chunks: list[dict[str, Any]],
|
| 64 |
-
) -> dict[str, set[str]]:
|
| 65 |
-
mapping: dict[str, set[str]] = {str(doc["doc_id"]): set() for doc in docs}
|
| 66 |
-
for chunk in chunks:
|
| 67 |
-
text = str(chunk.get("text") or "")
|
| 68 |
-
chunk_id = str(chunk.get("id") or "")
|
| 69 |
-
if not chunk_id:
|
| 70 |
-
continue
|
| 71 |
-
for doc in docs:
|
| 72 |
-
doc_id = str(doc.get("doc_id") or "")
|
| 73 |
-
marker = str(doc.get("marker") or doc_marker(doc_id))
|
| 74 |
-
if marker and marker in text:
|
| 75 |
-
mapping.setdefault(doc_id, set()).add(chunk_id)
|
| 76 |
-
return mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|