Datasets:
Upload knessetCorpus.py
Browse files- knessetCorpus.py +21 -23
knessetCorpus.py
CHANGED
@@ -366,33 +366,31 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
|
|
366 |
id_field_name = "person_id"
|
367 |
|
368 |
for filepath in data_files:
|
369 |
-
if not os.path.exists(filepath):
|
370 |
-
print(f"File '{filepath}' does not exist. Skipping.")
|
371 |
-
continue
|
372 |
-
|
373 |
try:
|
374 |
-
# Handle
|
375 |
-
if filepath.
|
376 |
-
with
|
377 |
-
for filename in archive.namelist():
|
378 |
-
with archive.open(filename) as f:
|
379 |
-
for line_number, line in enumerate(io.TextIOWrapper(f, encoding="utf-8")):
|
380 |
-
yield from self._process_line(line, id_field_name, line_number, filename)
|
381 |
-
|
382 |
-
# Handle `.bz2` files
|
383 |
-
elif filepath.endswith(".bz2"):
|
384 |
-
with bz2.open(filepath, "rt", encoding="utf-8") as f:
|
385 |
for line_number, line in enumerate(f):
|
386 |
yield from self._process_line(line, id_field_name, line_number, filepath)
|
387 |
-
|
388 |
-
# Handle regular `.jsonl` files
|
389 |
else:
|
390 |
-
|
391 |
-
|
392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
|
394 |
except Exception as e:
|
395 |
-
print(f"Error
|
396 |
|
397 |
|
398 |
|
@@ -411,7 +409,7 @@ def _process_line(self, line, id_field_name, line_number, filepath):
|
|
411 |
|
412 |
# Extract the ID field
|
413 |
id_ = row.get(id_field_name, f"unknown_{line_number}")
|
414 |
-
if id_
|
415 |
print(f"Key '{id_field_name}' not found in row at line {line_number}. Skipping row.")
|
416 |
return
|
417 |
|
@@ -429,4 +427,4 @@ def _process_line(self, line, id_field_name, line_number, filepath):
|
|
429 |
try:
|
430 |
yield id_, sample
|
431 |
except Exception as e:
|
432 |
-
print(f"Failed to yield sample at line {line_number} in file {filepath}. Error: {e}. Sample: {sample}.")
|
|
|
366 |
id_field_name = "person_id"
|
367 |
|
368 |
for filepath in data_files:
|
|
|
|
|
|
|
|
|
369 |
try:
|
370 |
+
# Streaming mode: Handle URLs directly
|
371 |
+
if filepath.startswith("http"):
|
372 |
+
with datasets.utils.stream_files(filepath) as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
373 |
for line_number, line in enumerate(f):
|
374 |
yield from self._process_line(line, id_field_name, line_number, filepath)
|
|
|
|
|
375 |
else:
|
376 |
+
# Non-streaming mode: Handle local files
|
377 |
+
if filepath.endswith(".bz2"):
|
378 |
+
with bz2.open(filepath, "rt", encoding="utf-8") as f:
|
379 |
+
for line_number, line in enumerate(f):
|
380 |
+
yield from self._process_line(line, id_field_name, line_number, filepath)
|
381 |
+
elif filepath.endswith(".zip"):
|
382 |
+
with zipfile.ZipFile(filepath, "r") as archive:
|
383 |
+
for filename in archive.namelist():
|
384 |
+
with archive.open(filename) as f:
|
385 |
+
for line_number, line in enumerate(io.TextIOWrapper(f, encoding="utf-8")):
|
386 |
+
yield from self._process_line(line, id_field_name, line_number, filename)
|
387 |
+
else:
|
388 |
+
with open(filepath, "rt", encoding="utf-8") as f:
|
389 |
+
for line_number, line in enumerate(f):
|
390 |
+
yield from self._process_line(line, id_field_name, line_number, filepath)
|
391 |
|
392 |
except Exception as e:
|
393 |
+
print(f"Error processing file '{filepath}': {e}. Skipping.")
|
394 |
|
395 |
|
396 |
|
|
|
409 |
|
410 |
# Extract the ID field
|
411 |
id_ = row.get(id_field_name, f"unknown_{line_number}")
|
412 |
+
if not id_:
|
413 |
print(f"Key '{id_field_name}' not found in row at line {line_number}. Skipping row.")
|
414 |
return
|
415 |
|
|
|
427 |
try:
|
428 |
yield id_, sample
|
429 |
except Exception as e:
|
430 |
+
print(f"Failed to yield sample at line {line_number} in file {filepath}. Error: {e}. Sample: {sample}.")
|