Download query/llm_post/X-ROBUST-009/example_submission/src/features.py from Migraine/ABench: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/datasets/Migraine/ABench/resolve/main/query/llm_post/X-ROBUST-009/example_submission/src/features.py
- Command line
-
hf download hf://datasets/Migraine/ABench/query/llm_post/X-ROBUST-009/example_submission/src/features.py
-
curl -L -o features.py https://huggingface.co/datasets/Migraine/ABench/resolve/main/query/llm_post/X-ROBUST-009/example_submission/src/features.py
1.3 kB
| """TF-IDF features of word 1-2-grams and character 3-5-grams (within word boundaries), stored without pickle.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| def vectorizers(): | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| return (TfidfVectorizer(analyzer="word", ngram_range=(1, 2), min_df=1, sublinear_tf=True, lowercase=True), | |
| TfidfVectorizer(analyzer="char_wb", ngram_range=(3, 5), min_df=2, sublinear_tf=True, lowercase=True)) | |
| def save(vecs, path: Path) -> None: | |
| path.mkdir(parents=True, exist_ok=True) | |
| for i, v in enumerate(vecs): | |
| (path / f"vocab{i}.json").write_text(json.dumps({k: int(x) for k, x in v.vocabulary_.items()}), encoding="utf-8") | |
| np.save(path / f"idf{i}.npy", v.idf_.astype(np.float32)) | |
| def load(path: Path): | |
| vecs = vectorizers() | |
| for i, v in enumerate(vecs): | |
| v.vocabulary = json.loads((path / f"vocab{i}.json").read_text(encoding="utf-8")) | |
| v.fit(["x"]) # sets up the fixed vocabulary; idf replaced below | |
| v.idf_ = np.load(path / f"idf{i}.npy").astype(np.float64) | |
| return vecs | |
| def transform(vecs, texts): | |
| from scipy.sparse import hstack | |
| return hstack([v.transform(texts) for v in vecs]).tocsr() | |