Datasets:
Upload knessetCorpus.py
Browse files- knessetCorpus.py +70 -34
knessetCorpus.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
-
import json
|
2 |
-
import os
|
3 |
-
from typing import List
|
4 |
-
from huggingface_hub import hf_hub_url
|
5 |
|
6 |
import datasets
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
_KnessetCorpus_CITATION = "@misc{goldin2024knesset, title={The Knesset Corpus: An Annotated Corpus of Hebrew Parliamentary Proceedings}, author={Gili Goldin and Nick Howell and Noam Ordan and Ella Rabinovich and Shuly Wintner}, year={2024}, eprint={2405.18115}, archivePrefix={arXiv}, primaryClass={cs.CL} }"
|
@@ -354,39 +355,74 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
|
|
354 |
elif self.config.name == "knessetMembers":
|
355 |
id_field_name = "person_id"
|
356 |
|
357 |
-
for
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
try:
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
sample[feature] = row[feature]
|
373 |
-
|
374 |
-
# Handle nested structures (e.g., `protocol_sentences`)
|
375 |
-
if "protocol_sentences" in row:
|
376 |
-
protocol_sentences = row.get("protocol_sentences", [])
|
377 |
-
for sentence in protocol_sentences:
|
378 |
-
sentence_id = sentence.get("sentence_id", None)
|
379 |
-
if sentence_id:
|
380 |
-
yield sentence_id, sentence
|
381 |
-
else:
|
382 |
-
try:
|
383 |
-
yield id_, sample
|
384 |
-
except Exception as e:
|
385 |
-
print(f'couldnt yield sample. error: {e}. sample is: {sample}.', flush=True)
|
386 |
-
except Exception as e:
|
387 |
-
print(f"Error opening file '{filepath}': {e}. Skipping.")
|
388 |
|
|
|
389 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
390 |
|
391 |
|
392 |
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
import datasets
|
3 |
+
import json
|
4 |
+
import zipfile
|
5 |
+
import bz2
|
6 |
+
import os
|
7 |
+
from typing import List, Dict, Any, Generator
|
8 |
|
9 |
|
10 |
_KnessetCorpus_CITATION = "@misc{goldin2024knesset, title={The Knesset Corpus: An Annotated Corpus of Hebrew Parliamentary Proceedings}, author={Gili Goldin and Nick Howell and Noam Ordan and Ella Rabinovich and Shuly Wintner}, year={2024}, eprint={2405.18115}, archivePrefix={arXiv}, primaryClass={cs.CL} }"
|
|
|
355 |
elif self.config.name == "knessetMembers":
|
356 |
id_field_name = "person_id"
|
357 |
|
358 |
+
for filepath in data_files:
|
359 |
+
if filepath.endswith('.zip'):
|
360 |
+
yield from self._process_zip_file(filepath, id_field_name)
|
361 |
+
elif filepath.endswith('.bz2'):
|
362 |
+
yield from self._process_bz2_file(filepath, id_field_name)
|
363 |
+
else:
|
364 |
+
print(f"Unsupported file format: {filepath}")
|
365 |
+
|
366 |
+
def _process_zip_file(self, filepath: str, id_field_name: str) -> Generator[tuple, None, None]:
|
367 |
+
try:
|
368 |
+
with zipfile.ZipFile(filepath, 'r') as zip_ref:
|
369 |
+
for file_info in zip_ref.filelist:
|
370 |
+
if not file_info.filename.endswith('.json'):
|
371 |
+
continue
|
372 |
+
|
373 |
+
with zip_ref.open(file_info) as f:
|
374 |
+
content = f.read().decode('utf-8')
|
375 |
try:
|
376 |
+
for line in content.splitlines():
|
377 |
+
if not line.strip():
|
378 |
+
continue
|
379 |
+
|
380 |
+
try:
|
381 |
+
row = json.loads(line)
|
382 |
+
except json.JSONDecodeError as e:
|
383 |
+
print(f"JSON decode error in zip file {filepath}, file {file_info.filename}: {e}")
|
384 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
|
386 |
+
yield from self._process_row(row, id_field_name)
|
387 |
|
388 |
+
except Exception as e:
|
389 |
+
print(f"Error processing file {file_info.filename} in zip {filepath}: {e}")
|
390 |
+
|
391 |
+
except Exception as e:
|
392 |
+
print(f"Error opening zip file {filepath}: {e}")
|
393 |
+
|
394 |
+
def _process_bz2_file(self, filepath: str, id_field_name: str) -> Generator[tuple, None, None]:
|
395 |
+
try:
|
396 |
+
with bz2.open(filepath, 'rt', encoding='utf-8') as f:
|
397 |
+
for line in f:
|
398 |
+
if not line.strip():
|
399 |
+
continue
|
400 |
+
|
401 |
+
try:
|
402 |
+
row = json.loads(line)
|
403 |
+
except json.JSONDecodeError as e:
|
404 |
+
print(f"JSON decode error in bz2 file {filepath}: {e}")
|
405 |
+
continue
|
406 |
+
|
407 |
+
yield from self._process_row(row, id_field_name)
|
408 |
+
|
409 |
+
except Exception as e:
|
410 |
+
print(f"Error opening bz2 file {filepath}: {e}")
|
411 |
+
|
412 |
+
def _process_row(self, row: Dict[str, Any], id_field_name: str) -> Generator[tuple, None, None]:
|
413 |
+
id_ = row.get(id_field_name)
|
414 |
+
if id_ is None:
|
415 |
+
print(f"Key '{id_field_name}' not found in row. Skipping this row. row is: {row}")
|
416 |
+
return
|
417 |
+
|
418 |
+
if "protocol_sentences" in row:
|
419 |
+
for sentence in row.get("protocol_sentences", []):
|
420 |
+
sentence_id = sentence.get("sentence_id")
|
421 |
+
if sentence_id:
|
422 |
+
yield sentence_id, sentence
|
423 |
+
else:
|
424 |
+
sample = {feature: row[feature] for feature in self._info().features if feature in row}
|
425 |
+
yield id_, sample
|
426 |
|
427 |
|
428 |
|