Datasets:
Upload knessetCorpus.py
Browse files- knessetCorpus.py +73 -20
knessetCorpus.py
CHANGED
@@ -4,6 +4,10 @@ import json
|
|
4 |
import zipfile
|
5 |
import bz2
|
6 |
import os
|
|
|
|
|
|
|
|
|
7 |
from typing import List, Dict, Any, Generator
|
8 |
|
9 |
|
@@ -356,12 +360,66 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
|
|
356 |
id_field_name = "person_id"
|
357 |
|
358 |
for filepath in data_files:
|
359 |
-
if
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
|
366 |
def _process_zip_file(self, filepath: str, id_field_name: str) -> Generator[tuple, None, None]:
|
367 |
try:
|
@@ -372,21 +430,17 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
|
|
372 |
|
373 |
with zip_ref.open(file_info) as f:
|
374 |
content = f.read().decode('utf-8')
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
continue
|
379 |
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
|
386 |
-
|
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}")
|
@@ -426,4 +480,3 @@ class KnessetCorpus(datasets.GeneratorBasedBuilder):
|
|
426 |
|
427 |
|
428 |
|
429 |
-
|
|
|
4 |
import zipfile
|
5 |
import bz2
|
6 |
import os
|
7 |
+
import urllib.request
|
8 |
+
from io import BytesIO
|
9 |
+
|
10 |
+
|
11 |
from typing import List, Dict, Any, Generator
|
12 |
|
13 |
|
|
|
360 |
id_field_name = "person_id"
|
361 |
|
362 |
for filepath in data_files:
|
363 |
+
if '::' in filepath: # Handle URLs with protocol prefixes
|
364 |
+
_, url = filepath.split('::', 1)
|
365 |
+
try:
|
366 |
+
response = urllib.request.urlopen(url)
|
367 |
+
content = response.read()
|
368 |
+
if filepath.endswith('.zip'):
|
369 |
+
yield from self._process_zip_content(content, id_field_name, url)
|
370 |
+
elif filepath.endswith('.bz2'):
|
371 |
+
yield from self._process_bz2_content(content, id_field_name, url)
|
372 |
+
except Exception as e:
|
373 |
+
print(f"Error downloading {url}: {e}")
|
374 |
+
else: # Handle local files
|
375 |
+
if filepath.endswith('.zip'):
|
376 |
+
yield from self._process_zip_file(filepath, id_field_name)
|
377 |
+
elif filepath.endswith('.bz2'):
|
378 |
+
yield from self._process_bz2_file(filepath, id_field_name)
|
379 |
+
else:
|
380 |
+
print(f"Unsupported file format: {filepath}")
|
381 |
+
|
382 |
+
def _process_zip_content(self, content: bytes, id_field_name: str, url: str) -> Generator[tuple, None, None]:
|
383 |
+
try:
|
384 |
+
with zipfile.ZipFile(BytesIO(content)) as zip_ref:
|
385 |
+
for file_info in zip_ref.filelist:
|
386 |
+
if not file_info.filename.endswith('.json'):
|
387 |
+
continue
|
388 |
+
|
389 |
+
with zip_ref.open(file_info) as f:
|
390 |
+
content = f.read().decode('utf-8')
|
391 |
+
for line in content.splitlines():
|
392 |
+
if not line.strip():
|
393 |
+
continue
|
394 |
+
|
395 |
+
try:
|
396 |
+
row = json.loads(line)
|
397 |
+
except json.JSONDecodeError as e:
|
398 |
+
print(f"JSON decode error in zip file from {url}, file {file_info.filename}: {e}")
|
399 |
+
continue
|
400 |
+
|
401 |
+
yield from self._process_row(row, id_field_name)
|
402 |
+
|
403 |
+
except Exception as e:
|
404 |
+
print(f"Error processing zip content from {url}: {e}")
|
405 |
+
|
406 |
+
def _process_bz2_content(self, content: bytes, id_field_name: str, url: str) -> Generator[tuple, None, None]:
|
407 |
+
try:
|
408 |
+
decompressed = bz2.decompress(content).decode('utf-8')
|
409 |
+
for line in decompressed.splitlines():
|
410 |
+
if not line.strip():
|
411 |
+
continue
|
412 |
+
|
413 |
+
try:
|
414 |
+
row = json.loads(line)
|
415 |
+
except json.JSONDecodeError as e:
|
416 |
+
print(f"JSON decode error in bz2 content from {url}: {e}")
|
417 |
+
continue
|
418 |
+
|
419 |
+
yield from self._process_row(row, id_field_name)
|
420 |
+
|
421 |
+
except Exception as e:
|
422 |
+
print(f"Error processing bz2 content from {url}: {e}")
|
423 |
|
424 |
def _process_zip_file(self, filepath: str, id_field_name: str) -> Generator[tuple, None, None]:
|
425 |
try:
|
|
|
430 |
|
431 |
with zip_ref.open(file_info) as f:
|
432 |
content = f.read().decode('utf-8')
|
433 |
+
for line in content.splitlines():
|
434 |
+
if not line.strip():
|
435 |
+
continue
|
|
|
436 |
|
437 |
+
try:
|
438 |
+
row = json.loads(line)
|
439 |
+
except json.JSONDecodeError as e:
|
440 |
+
print(f"JSON decode error in zip file {filepath}, file {file_info.filename}: {e}")
|
441 |
+
continue
|
442 |
|
443 |
+
yield from self._process_row(row, id_field_name)
|
|
|
|
|
|
|
444 |
|
445 |
except Exception as e:
|
446 |
print(f"Error opening zip file {filepath}: {e}")
|
|
|
480 |
|
481 |
|
482 |
|
|