tomrb commited on
Commit
6c59d7a
·
1 Parent(s): f4de038
Files changed (1) hide show
  1. minipileoflaw.py +7 -5
minipileoflaw.py CHANGED
@@ -8,6 +8,7 @@ import pandas as pd
8
 
9
  import json
10
  import logging
 
11
 
12
  import datasets
13
  try:
@@ -95,8 +96,9 @@ class MiniPileOfLaw(datasets.GeneratorBasedBuilder):
95
  ),
96
  ]
97
 
 
98
  def _generate_examples(self, filepaths):
99
- """This function returns the examples from JSONL files, with each line being a separate JSON object."""
100
  for filepath in filepaths:
101
  try:
102
  with open(filepath, 'r', encoding='utf-8') as file:
@@ -106,16 +108,16 @@ class MiniPileOfLaw(datasets.GeneratorBasedBuilder):
106
  continue
107
 
108
  try:
109
- # Parse each line as a separate JSON object
110
- item = json.loads(line.strip(), ensure_ascii=False)
111
  yield {
112
  "text": item.get("text", ""),
113
  "created_timestamp": item.get("created_timestamp", ""),
114
  "downloaded_timestamp": item.get("downloaded_timestamp", ""),
115
  "url": item.get("url", "")
116
  }
117
- except json.JSONDecodeError as e:
118
- print(f"Error parsing JSON on line in file {filepath}: {e}")
119
  except Exception as e:
120
  print(f"Error reading file {filepath}: {e}")
121
 
 
8
 
9
  import json
10
  import logging
11
+ import ast
12
 
13
  import datasets
14
  try:
 
96
  ),
97
  ]
98
 
99
+
100
  def _generate_examples(self, filepaths):
101
+ """This function returns the examples from dict-like strings in files."""
102
  for filepath in filepaths:
103
  try:
104
  with open(filepath, 'r', encoding='utf-8') as file:
 
108
  continue
109
 
110
  try:
111
+ # Parse each line as a separate dict object
112
+ item = ast.literal_eval(line.strip())
113
  yield {
114
  "text": item.get("text", ""),
115
  "created_timestamp": item.get("created_timestamp", ""),
116
  "downloaded_timestamp": item.get("downloaded_timestamp", ""),
117
  "url": item.get("url", "")
118
  }
119
+ except (ValueError, SyntaxError) as e:
120
+ print(f"Error parsing dict on line in file {filepath}: {e}")
121
  except Exception as e:
122
  print(f"Error reading file {filepath}: {e}")
123