albertvillanova HF staff commited on
Commit
8e60184
1 Parent(s): 4107b38

Support streaming daily_dialog dataset (#4008)

Browse files

Commit from https://github.com/huggingface/datasets/commit/0153001de162f6bc7d0288e033e40a845b5a784e

Files changed (1) hide show
  1. daily_dialog.py +30 -58
daily_dialog.py CHANGED
@@ -82,69 +82,41 @@ class DailyDialog(datasets.GeneratorBasedBuilder):
82
  )
83
 
84
  def _split_generators(self, dl_manager: datasets.DownloadManager):
85
- """Returns SplitGenerators."""
86
- # dl_manager is a datasets.download.DownloadManager that can be used to
87
- # download and extract URLs
88
  dl_dir = dl_manager.download_and_extract(_URL)
89
  data_dir = os.path.join(dl_dir, "ijcnlp_dailydialog")
90
-
91
- # The splits are nested inside the zip
92
- for name in ("train", "validation", "test"):
93
- zip_fpath = os.path.join(data_dir, f"{name}.zip")
94
- with ZipFile(zip_fpath) as zip_file:
95
- zip_file.extractall(path=data_dir)
96
- zip_file.close()
97
-
98
  return [
99
  datasets.SplitGenerator(
100
- name=datasets.Split.TRAIN,
101
- # These kwargs will be passed to _generate_examples
102
- gen_kwargs={
103
- "file_path": os.path.join(data_dir, "train", "dialogues_train.txt"),
104
- "act_path": os.path.join(data_dir, "train", "dialogues_act_train.txt"),
105
- "emotion_path": os.path.join(data_dir, "train", "dialogues_emotion_train.txt"),
106
- "split": "train",
107
- },
108
- ),
109
- datasets.SplitGenerator(
110
- name=datasets.Split.TEST,
111
- # These kwargs will be passed to _generate_examples
112
  gen_kwargs={
113
- "file_path": os.path.join(data_dir, "test", "dialogues_test.txt"),
114
- "act_path": os.path.join(data_dir, "test", "dialogues_act_test.txt"),
115
- "emotion_path": os.path.join(data_dir, "test", "dialogues_emotion_test.txt"),
116
- "split": "test",
117
  },
118
- ),
119
- datasets.SplitGenerator(
120
- name=datasets.Split.VALIDATION,
121
- # These kwargs will be passed to _generate_examples
122
- gen_kwargs={
123
- "file_path": os.path.join(data_dir, "validation", "dialogues_validation.txt"),
124
- "act_path": os.path.join(data_dir, "validation", "dialogues_act_validation.txt"),
125
- "emotion_path": os.path.join(data_dir, "validation", "dialogues_emotion_validation.txt"),
126
- "split": "dev",
127
- },
128
- ),
129
  ]
130
 
131
- def _generate_examples(self, file_path, act_path, emotion_path, split):
132
- """Yields examples."""
133
- # Yields (key, example) tuples from the dataset
134
- with open(file_path, "r", encoding="utf-8") as f, open(act_path, "r", encoding="utf-8") as act, open(
135
- emotion_path, "r", encoding="utf-8"
136
- ) as emotion:
137
- for i, (line_f, line_act, line_emotion) in enumerate(zip(f, act, emotion)):
138
- if len(line_f.strip()) == 0:
139
- break
140
- dialog = line_f.split(self.__EOU__)[:-1]
141
- act = line_act.split(" ")[:-1]
142
- emotion = line_emotion.split(" ")[:-1]
143
-
144
- assert len(dialog) == len(act) == len(emotion), "Different turns btw dialogue & emotion & action"
145
-
146
- yield f"{split}-{i}", {
147
- "dialog": dialog,
148
- "act": [act_label[x] for x in act],
149
- "emotion": [emotion_label[x] for x in emotion],
150
- }
 
 
 
82
  )
83
 
84
  def _split_generators(self, dl_manager: datasets.DownloadManager):
 
 
 
85
  dl_dir = dl_manager.download_and_extract(_URL)
86
  data_dir = os.path.join(dl_dir, "ijcnlp_dailydialog")
87
+ splits = [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]
 
 
 
 
 
 
 
88
  return [
89
  datasets.SplitGenerator(
90
+ name=split,
 
 
 
 
 
 
 
 
 
 
 
91
  gen_kwargs={
92
+ "data_zip": os.path.join(data_dir, f"{split}.zip"),
93
+ "dialog_path": f"{split}/dialogues_{split}.txt",
94
+ "act_path": f"{split}/dialogues_act_{split}.txt",
95
+ "emotion_path": f"{split}/dialogues_emotion_{split}.txt",
96
  },
97
+ )
98
+ for split in splits
 
 
 
 
 
 
 
 
 
99
  ]
100
 
101
+ def _generate_examples(self, data_zip, dialog_path, act_path, emotion_path):
102
+ with open(data_zip, "rb") as data_file:
103
+ with ZipFile(data_file) as zip_file:
104
+ with zip_file.open(dialog_path) as dialog_file, zip_file.open(act_path) as act_file, zip_file.open(
105
+ emotion_path
106
+ ) as emotion_file:
107
+ for idx, (dialog_line, act_line, emotion_line) in enumerate(
108
+ zip(dialog_file, act_file, emotion_file)
109
+ ):
110
+ if not dialog_line.strip():
111
+ break
112
+ dialog = dialog_line.decode().split(self.__EOU__)[:-1]
113
+ act = act_line.decode().split(" ")[:-1]
114
+ emotion = emotion_line.decode().split(" ")[:-1]
115
+ assert (
116
+ len(dialog) == len(act) == len(emotion)
117
+ ), "Different turns btw dialogue & emotion & action"
118
+ yield idx, {
119
+ "dialog": dialog,
120
+ "act": [act_label[x] for x in act],
121
+ "emotion": [emotion_label[x] for x in emotion],
122
+ }