suchirsalhan commited on
Commit
585a2df
·
verified ·
1 Parent(s): 6d42b80

Update common-corpus.py

Browse files
Files changed (1) hide show
  1. common-corpus.py +35 -16
common-corpus.py CHANGED
@@ -5,14 +5,15 @@ from datasets import DatasetInfo, GeneratorBasedBuilder, Split, SplitGenerator,
5
  class CommonCorpus(GeneratorBasedBuilder):
6
  VERSION = datasets.Version("1.0.0")
7
  BUILDER_CONFIGS = [
8
- datasets.BuilderConfig(name="bytelevel", version=VERSION, description="Bytelevel corpus data"),
9
  ]
10
 
11
  def _info(self):
12
  return DatasetInfo(
13
- description="Common Corpus from InfoTokenizers - bytelevel config",
14
  features=Features({
15
  "text": Value("string"),
 
16
  }),
17
  supervised_keys=None,
18
  homepage="https://huggingface.co/datasets/InfoTokenizers/common-corpus",
@@ -20,25 +21,43 @@ class CommonCorpus(GeneratorBasedBuilder):
20
  )
21
 
22
  def _split_generators(self, dl_manager):
23
- # Use the local extracted path to dataset files
24
- data_dir = os.path.join(dl_manager.manual_dir, "bytelevel")
 
 
 
 
 
 
 
 
 
25
 
26
  return [
27
  SplitGenerator(
28
  name=Split.TRAIN,
29
- gen_kwargs={"data_dir": data_dir},
30
  ),
31
  ]
32
 
33
- def _generate_examples(self, data_dir):
34
- # Iterate over all .txt files inside bytelevel folder
35
- file_paths = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.endswith(".txt")]
36
-
37
  example_id = 0
38
- for file_path in file_paths:
39
- with open(file_path, encoding="utf-8") as f:
40
- for line in f:
41
- text = line.strip()
42
- if text:
43
- yield example_id, {"text": text}
44
- example_id += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  class CommonCorpus(GeneratorBasedBuilder):
6
  VERSION = datasets.Version("1.0.0")
7
  BUILDER_CONFIGS = [
8
+ datasets.BuilderConfig(name="default", version=VERSION, description="Common Corpus dataset"),
9
  ]
10
 
11
  def _info(self):
12
  return DatasetInfo(
13
+ description="Common Corpus from InfoTokenizers with multiple subfolders",
14
  features=Features({
15
  "text": Value("string"),
16
+ "source": Value("string"),
17
  }),
18
  supervised_keys=None,
19
  homepage="https://huggingface.co/datasets/InfoTokenizers/common-corpus",
 
21
  )
22
 
23
  def _split_generators(self, dl_manager):
24
+ # Downloaded and extracted folder path
25
+ data_dir = dl_manager.download_and_extract("https://huggingface.co/datasets/InfoTokenizers/common-corpus/resolve/main/")
26
+
27
+ # List all subdirectories
28
+ subdirs = ["bytelevel", "bytelevel-llm-data", "bytelevel-subset", "bytelevel-subset_1", "bytelevel-subset_2"]
29
+
30
+ # Check existence of each subdir
31
+ valid_dirs = [os.path.join(data_dir, subdir) for subdir in subdirs if os.path.exists(os.path.join(data_dir, subdir))]
32
+
33
+ if not valid_dirs:
34
+ raise FileNotFoundError(f"No valid data directories found in {data_dir}. Expected one of: {subdirs}")
35
 
36
  return [
37
  SplitGenerator(
38
  name=Split.TRAIN,
39
+ gen_kwargs={"data_dirs": valid_dirs},
40
  ),
41
  ]
42
 
43
+ def _generate_examples(self, data_dirs):
 
 
 
44
  example_id = 0
45
+
46
+ for data_dir in data_dirs:
47
+ subdir_name = os.path.basename(data_dir)
48
+
49
+ # Iterate over all .txt files in the directory
50
+ for root, _, files in os.walk(data_dir):
51
+ for filename in files:
52
+ if filename.endswith(".txt"):
53
+ file_path = os.path.join(root, filename)
54
+
55
+ with open(file_path, encoding="utf-8") as f:
56
+ for line in f:
57
+ text = line.strip()
58
+ if text:
59
+ yield example_id, {
60
+ "text": text,
61
+ "source": subdir_name,
62
+ }
63
+ example_id += 1