Prepare GloVe dataset by converting it to parquet format with chunking
(dataset_dir)
| 140 | |
| 141 | |
| 142 | def prepare_glove_dataset(dataset_dir): |
| 143 | """Prepare GloVe dataset by converting it to parquet format with chunking""" |
| 144 | zip_path = os.path.join(dataset_dir, "glove.6B.zip") |
| 145 | if not os.path.exists(zip_path): |
| 146 | print("Downloading GloVe dataset...") |
| 147 | download_file(datasets["glove-100"]["url"], zip_path) |
| 148 | |
| 149 | print("Extracting GloVe dataset...") |
| 150 | with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 151 | zip_ref.extract("glove.6B.100d.txt", dataset_dir) |
| 152 | |
| 153 | print("Converting GloVe to parquet format with chunking...") |
| 154 | # Read the GloVe text file in chunks |
| 155 | chunk_size = 200_000 |
| 156 | chunk = [] |
| 157 | file_index = 0 |
| 158 | |
| 159 | with open( |
| 160 | os.path.join(dataset_dir, "glove.6B.100d.txt"), "r", encoding="utf-8" |
| 161 | ) as f: |
| 162 | for i, line in enumerate(f): |
| 163 | values = line.split() |
| 164 | word = values[0] |
| 165 | vector = [float(x) for x in values[1:]] |
| 166 | chunk.append({"id": word, "embeddings": vector}) |
| 167 | |
| 168 | if (i + 1) % chunk_size == 0: |
| 169 | # Convert chunk to DataFrame and save |
| 170 | df = pd.DataFrame(chunk) |
| 171 | parquet_path = os.path.join(dataset_dir, f"test{file_index}.parquet") |
| 172 | print(f"Saving chunk {file_index} to {parquet_path}") |
| 173 | df.to_parquet(parquet_path) |
| 174 | |
| 175 | # Clear chunk for next batch |
| 176 | chunk.clear() |
| 177 | file_index += 1 |
| 178 | |
| 179 | # Save any remaining data |
| 180 | if chunk: |
| 181 | df = pd.DataFrame(chunk) |
| 182 | parquet_path = os.path.join(dataset_dir, f"test{file_index}.parquet") |
| 183 | print(f"Saving remaining chunk to {parquet_path}") |
| 184 | df.to_parquet(parquet_path) |
| 185 | |
| 186 | # Clean up temporary files |
| 187 | os.remove(os.path.join(dataset_dir, "glove.6B.100d.txt")) |
| 188 | os.remove(zip_path) |
| 189 | |
| 190 | |
| 191 | def prepare_dataset(dataset_name): |
no test coverage detected