gabrielaltay albertvillanova HF staff commited on
Commit
c954944
1 Parent(s): 18f251f

Fix TOO MANY REQUESTS error (#2)

Browse files

- Optimize code to perform less calls to filesystem (28f99700957f082e630ed5127c21d836e1b08a46)
- Rename variable id to guid (c896074d018b93214011fa745ec70c854b1b2ff8)


Co-authored-by: Albert Villanova <albertvillanova@users.noreply.huggingface.co>

Files changed (1) hide show
  1. twadrl.py +20 -25
twadrl.py CHANGED
@@ -108,32 +108,27 @@ class TwADRL(datasets.GeneratorBasedBuilder):
108
  dl_dir = dl_manager.download_and_extract(_URLs)
109
  dataset_dir = os.path.join(dl_dir, "datasets", "TwADR-L")
110
  # dataset supports k-folds
111
- splits = []
112
- for split_name in [
113
- datasets.Split.TRAIN,
114
- datasets.Split.VALIDATION,
115
- datasets.Split.TEST,
116
- ]:
117
- for fold_filepath in glob.glob(
118
- os.path.join(dataset_dir, f"TwADR-L.fold-*.{split_name}.txt")
119
- ):
120
- fold_id = re.search("TwADR-L\.fold-(\d)\.", fold_filepath).group(1)
121
- split_id = f"{split_name}_{fold_id}"
122
- splits.append(
123
- datasets.SplitGenerator(
124
- name=split_id,
125
- gen_kwargs={"filepath": fold_filepath, "split_id": split_id},
126
- )
127
- )
128
- return splits
129
 
130
  def _generate_examples(self, filepath, split_id):
131
  with open(filepath, "r", encoding="latin-1") as f:
132
  for i, line in enumerate(f):
133
- id = f"{split_id}_{i}"
134
  cui, medical_concept, social_media_text = line.strip().split("\t")
135
  if self.config.schema == "source":
136
- yield id, {
137
  "cui": cui,
138
  "medical_concept": medical_concept,
139
  "social_media_text": social_media_text,
@@ -141,12 +136,12 @@ class TwADRL(datasets.GeneratorBasedBuilder):
141
  elif self.config.schema == "bigbio_kb":
142
  text_type = "social_media_text"
143
  offset = (0, len(social_media_text))
144
- yield id, {
145
- "id": id,
146
- "document_id": id,
147
  "passages": [
148
  {
149
- "id": f"{id}_passage",
150
  "type": text_type,
151
  "text": [social_media_text],
152
  "offsets": [offset],
@@ -154,7 +149,7 @@ class TwADRL(datasets.GeneratorBasedBuilder):
154
  ],
155
  "entities": [
156
  {
157
- "id": f"{id}_entity",
158
  "type": text_type,
159
  "text": [social_media_text],
160
  "offsets": [offset],
 
108
  dl_dir = dl_manager.download_and_extract(_URLs)
109
  dataset_dir = os.path.join(dl_dir, "datasets", "TwADR-L")
110
  # dataset supports k-folds
111
+ splits_names = ["train", "validation", "test"]
112
+ fold_ids = range(10)
113
+ return [
114
+ datasets.SplitGenerator(
115
+ name=f"{split_name}_{fold_id}",
116
+ gen_kwargs={
117
+ "filepath": os.path.join(dataset_dir, f"TwADR-L.fold-{fold_id}.{split_name}.txt"),
118
+ "split_id": f"{split_name}_{fold_id}",
119
+ },
120
+ )
121
+ for split_name in splits_names
122
+ for fold_id in fold_ids
123
+ ]
 
 
 
 
 
124
 
125
  def _generate_examples(self, filepath, split_id):
126
  with open(filepath, "r", encoding="latin-1") as f:
127
  for i, line in enumerate(f):
128
+ guid = f"{split_id}_{i}"
129
  cui, medical_concept, social_media_text = line.strip().split("\t")
130
  if self.config.schema == "source":
131
+ yield guid, {
132
  "cui": cui,
133
  "medical_concept": medical_concept,
134
  "social_media_text": social_media_text,
 
136
  elif self.config.schema == "bigbio_kb":
137
  text_type = "social_media_text"
138
  offset = (0, len(social_media_text))
139
+ yield guid, {
140
+ "id": guid,
141
+ "document_id": guid,
142
  "passages": [
143
  {
144
+ "id": f"{guid}_passage",
145
  "type": text_type,
146
  "text": [social_media_text],
147
  "offsets": [offset],
 
149
  ],
150
  "entities": [
151
  {
152
+ "id": f"{guid}_entity",
153
  "type": text_type,
154
  "text": [social_media_text],
155
  "offsets": [offset],