MCPcopy Create free account
hub / github.com/cosdata/cosdata / get_dataset

Function get_dataset

tests/test-tf-idf-bm25.py:187–230  ·  view source on GitHub ↗

Generate raw dataset with just term frequencies

(dataset: str)

Source from the content-addressed store, hash-verified

185
186
187def get_dataset(dataset: str) -> tuple:
188 """Generate raw dataset with just term frequencies"""
189 dataset_folder = f"datasets/sparse_idf_dataset_{dataset}"
190 raw_dataset_file = f"{dataset_folder}/raw_vectors.pkl"
191 if os.path.exists(raw_dataset_file):
192 print(f"Loading existing raw dataset from {raw_dataset_file}")
193 with open(raw_dataset_file, "rb") as f:
194 return pickle.load(f)
195
196 save_dir = f"{dataset_folder}/dataset"
197
198 print("Downloading the dataset...")
199 bm25s.utils.beir.download_dataset(dataset, save_dir=save_dir)
200 print("Loading the corpus...")
201 corpus = bm25s.utils.beir.load_corpus(dataset, save_dir=save_dir)
202
203 punctuations = get_all_punctuation()
204 stemmer = SnowballStemmer("english")
205
206 print("Converting to sparse vectors...")
207 vectors = []
208
209 with ThreadPoolExecutor(max_workers=32) as executor:
210 futures = []
211 for k, v in tqdm(corpus.items()):
212 futures.append(
213 executor.submit(
214 transform_sentence_to_vector,
215 k,
216 v["title"] + " " + v["text"],
217 punctuations,
218 stemmer,
219 )
220 )
221
222 for future in tqdm(as_completed(futures)):
223 vectors.append(future.result())
224
225 # Save to disk
226 with open(raw_dataset_file, "wb") as f:
227 pickle.dump(vectors, f)
228
229 print(f"Raw dataset generated and saved to {raw_dataset_file}")
230 return vectors
231
232
233def calculate_corpus_statistics(dataset, vectors):

Callers 2

mainFunction · 0.85
main_non_itFunction · 0.85

Calls 3

get_all_punctuationFunction · 0.85
loadMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected