Datasets:

GiliGold commited on
Commit
b890947
·
verified ·
1 Parent(s): f7d6cb3

Upload knessetCorpus.py

Browse files
Files changed (1) hide show
  1. knessetCorpus.py +36 -54
knessetCorpus.py CHANGED
@@ -2,10 +2,8 @@ import json
2
  import os
3
  from typing import List
4
  from huggingface_hub import hf_hub_url
5
- import zipfile
6
- import bz2
7
  import datasets
8
- import io
9
 
10
 
11
  _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} }"
@@ -335,30 +333,18 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
335
  )
336
  def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
337
  urls = self.config.data_urls
 
338
 
339
- if dl_manager.is_streaming:
340
- return [
341
- datasets.SplitGenerator(
342
- name=datasets.Split.TRAIN,
343
- gen_kwargs={"data_files": urls},
344
- )
345
- ]
346
- else:
347
- downloaded_files = dl_manager.download_and_extract(urls)
348
-
349
- return [
350
- datasets.SplitGenerator(
351
- name=datasets.Split.TRAIN,
352
- gen_kwargs={
353
- "data_files": downloaded_files
354
- },
355
- ),
356
- ]
357
 
358
  def _generate_examples(self, data_files):
359
- """
360
- Generate examples from files, supporting both streaming and non-streaming modes.
361
- """
362
  if "all_features_sentences" in self.config.name:
363
  id_field_name = "sentence_id"
364
  elif self.config.name == "protocols":
@@ -367,36 +353,32 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
367
  id_field_name = "faction_id"
368
  elif self.config.name == "knessetMembers":
369
  id_field_name = "person_id"
370
- else:
371
- id_field_name = "id" # Default for generic configurations
372
 
373
- for filepath in data_files:
374
  try:
375
- # Hugging Face automatically handles streaming
376
- for line_number, row in enumerate(filepath):
377
- try:
378
- row = json.loads(row)
379
- except json.JSONDecodeError as e:
380
- print(f"Failed to decode JSON at line {line_number} in file {filepath}: {e}. Skipping.")
381
- continue
382
-
383
- # Extract the ID field
384
- id_ = row.get(id_field_name, f"unknown_{line_number}")
385
- if not id_:
386
- print(f"Key '{id_field_name}' not found in row at line {line_number}. Skipping row.")
387
- continue
388
-
389
- # Prepare the sample dictionary
390
- sample = {feature: row.get(feature, None) for feature in self._info().features}
391
-
392
- # Handle nested structures (e.g., `protocol_sentences`)
393
- if "protocol_sentences" in row:
394
- protocol_sentences = row.get("protocol_sentences", [])
395
- for sentence in protocol_sentences:
396
- sentence_id = sentence.get("sentence_id", None)
397
- if sentence_id:
398
- yield sentence_id, sentence
399
- else:
400
- yield id_, sample
401
  except Exception as e:
402
- print(f"Error processing file '{filepath}': {e}. Skipping.")
 
 
 
 
 
 
 
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} }"
 
333
  )
334
  def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
335
  urls = self.config.data_urls
336
+ downloaded_files = dl_manager.download_and_extract(urls)
337
 
338
+ return [
339
+ datasets.SplitGenerator(
340
+ name=datasets.Split.TRAIN,
341
+ gen_kwargs={
342
+ "data_files": downloaded_files
343
+ },
344
+ ),
345
+ ]
 
 
 
 
 
 
 
 
 
 
346
 
347
  def _generate_examples(self, data_files):
 
 
 
348
  if "all_features_sentences" in self.config.name:
349
  id_field_name = "sentence_id"
350
  elif self.config.name == "protocols":
 
353
  id_field_name = "faction_id"
354
  elif self.config.name == "knessetMembers":
355
  id_field_name = "person_id"
 
 
356
 
357
+ for i, filepath in enumerate(data_files):
358
  try:
359
+ with open(filepath, encoding="utf-8") as f:
360
+ for line in f:
361
+ sample = {}
362
+ try:
363
+ row = json.loads(line)
364
+ except Exception as e:
365
+ print(f'couldnt load sample. error was: {e}. Continuing to next sample')
366
+ continue
367
+ id_ = row.get(id_field_name, None)
368
+ if id_ is None:
369
+ print(f"Key '{id_field_name}' not found in row. Skipping this row. row is: {row}")
370
+ continue
371
+ for feature in self._info().features:
372
+ sample[feature] = row[feature]
373
+ try:
374
+ yield id_, sample
375
+ except Exception as e:
376
+ print(f'couldnt yield sample. error: {e}. sample is: {sample}.', flush=True)
 
 
 
 
 
 
 
 
377
  except Exception as e:
378
+ print(f"Error opening file '{filepath}': {e}. Skipping.")
379
+
380
+
381
+
382
+
383
+
384
+