Datasets:

Languages:
French
License:
schlevik commited on
Commit
35b2ba6
·
1 Parent(s): 7b45cb5

fix text script

Browse files
Files changed (1) hide show
  1. cas.py +111 -84
cas.py CHANGED
@@ -168,95 +168,122 @@ class CAS(datasets.GeneratorBasedBuilder):
168
 
169
  def _generate_examples(self, datadir):
170
  key = 0
171
- for file in ["CAS_neg.txt", "CAS_spec.txt"]:
172
- filepath = os.path.join(datadir, file)
173
- label = "negation" if "neg" in file else "speculation"
174
- id_docs = []
175
- id_words = []
176
- words = []
177
- lemmas = []
178
- POS_tags = []
 
 
 
 
 
179
 
180
- with open(filepath) as f:
181
- for line in f.readlines():
182
- line_content = line.split("\t")
183
- if len(line_content) > 1:
184
- id_docs.append(line_content[0])
185
- id_words.append(line_content[1])
186
- words.append(line_content[2])
187
- lemmas.append(line_content[3])
188
- POS_tags.append(line_content[4])
 
189
 
190
- dic = {
191
- "id_docs": np.array(list(map(int, id_docs))),
192
- "id_words": id_words,
193
- "words": words,
194
- "lemmas": lemmas,
195
- "POS_tags": POS_tags,
196
- }
197
- if self.config.schema == "source":
198
- for doc_id in set(dic["id_docs"]):
199
- idces = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
200
- text = [dic["words"][id] for id in idces]
201
- text_lemmas = [dic["lemmas"][id] for id in idces]
202
- POS_tags_ = [dic["POS_tags"][id] for id in idces]
203
- yield key, {
204
- "id": key,
205
- "document_id": doc_id,
206
- "text": text,
207
- "lemmas": text_lemmas,
208
- "POS_tags": POS_tags_,
209
- "labels": [label],
210
- }
211
- key += 1
212
- elif self.config.schema == "bigbio_text":
213
- for doc_id in set(dic["id_docs"]):
214
- idces = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
215
- text = " ".join([dic["words"][id] for id in idces])
216
- yield key, {
217
- "id": key,
218
- "document_id": doc_id,
219
- "text": text,
220
- "labels": [label],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  }
222
- key += 1
223
- elif self.config.schema == "bigbio_kb":
224
- for doc_id in set(dic["id_docs"]):
225
- idces = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
226
- text = [dic["words"][id] for id in idces]
227
- POS_tags_ = [dic["POS_tags"][id] for id in idces]
228
 
229
- data = {
230
- "id": str(key),
231
- "document_id": doc_id,
232
- "passages": [],
233
- "entities": [],
234
- "relations": [],
235
- "events": [],
236
- "coreferences": [],
237
  }
 
238
  key += 1
239
 
240
- data["passages"] = [
241
- {
242
- "id": str(key + i),
243
- "type": "sentence",
244
- "text": [text[i]],
245
- "offsets": [[i, i + 1]],
246
- }
247
- for i in range(len(text))
248
- ]
249
- key += len(text)
250
-
251
- for i in range(len(text)):
252
- entity = {
253
- "id": key,
254
- "type": "POS_tag",
255
- "text": [POS_tags_[i]],
256
- "offsets": [[i, i + 1]],
257
- "normalized": [],
258
- }
259
- data["entities"].append(entity)
260
- key += 1
261
-
262
  yield key, data
 
168
 
169
  def _generate_examples(self, datadir):
170
  key = 0
171
+ # for file in ["CAS_neg.txt", "CAS_spec.txt"]:
172
+ file = 'CAS_neg.txt'
173
+ filepath = os.path.join(datadir, file)
174
+ filepath2 = os.path.join(datadir, 'CAS_spec.txt')
175
+ label = "negation" if "neg" in file else "speculation"
176
+ id_docs = []
177
+ id_docs_2 = []
178
+ id_words = []
179
+ words = []
180
+ lemmas = []
181
+ POS_tags = []
182
+ NER_tags = []
183
+ NER_tags_2 = []
184
 
185
+ with open(filepath) as f:
186
+ for line in f.readlines():
187
+ line_content = line.split("\t")
188
+ if len(line_content) > 1:
189
+ id_docs.append(line_content[0])
190
+ id_words.append(line_content[1])
191
+ words.append(line_content[2])
192
+ lemmas.append(line_content[3])
193
+ POS_tags.append(line_content[4])
194
+ NER_tags.append(line_content[5].strip())
195
 
196
+ with open(filepath2) as f:
197
+ for line in f.readlines():
198
+ line_content = line.split("\t")
199
+ if len(line_content) > 1:
200
+ id_docs_2.append(line_content[0])
201
+ NER_tags_2.append(line_content[5].strip())
202
+
203
+ dic = {
204
+ "id_docs": np.array(list(map(int, id_docs))),
205
+ "id_words": id_words,
206
+ "words": words,
207
+ "lemmas": lemmas,
208
+ "POS_tags": POS_tags,
209
+ "NER_tags": NER_tags
210
+ }
211
+ dic2 = {
212
+ "id_docs": np.array(list(map(int, id_docs_2))),
213
+ "NER_tags": NER_tags_2
214
+ }
215
+ if self.config.schema == "source":
216
+ for doc_id in set(dic["id_docs"]):
217
+ idces = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
218
+ text = [dic["words"][id] for id in idces]
219
+ text_lemmas = [dic["lemmas"][id] for id in idces]
220
+ POS_tags_ = [dic["POS_tags"][id] for id in idces]
221
+ yield key, {
222
+ "id": key,
223
+ "document_id": doc_id,
224
+ "text": text,
225
+ "lemmas": text_lemmas,
226
+ "POS_tags": POS_tags_,
227
+ "labels": [label],
228
+ }
229
+ key += 1
230
+ elif self.config.schema == "bigbio_text":
231
+ for doc_id in set(dic["id_docs"]):
232
+ idces = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
233
+ idces_2 = np.argwhere(dic2["id_docs"] == doc_id)[:, 0]
234
+
235
+ text = " ".join([dic["words"][id] for id in idces])
236
+ label_tokens = [dic["NER_tags"][id] for id in idces]
237
+ label2_tokens = [dic2["NER_tags"][id] for id in idces_2]
238
+ label_ = []
239
+ if not all(l == '***' for l in label_tokens):
240
+ label_.append("negation")
241
+ if not all(l == '***' for l in label2_tokens):
242
+ label_.append("speculation")
243
+ yield key, {
244
+ "id": key,
245
+ "document_id": doc_id,
246
+ "text": text,
247
+ "labels": label_,
248
+ }
249
+ key += 1
250
+ elif self.config.schema == "bigbio_kb":
251
+ for doc_id in set(dic["id_docs"]):
252
+ idces = np.argwhere(dic["id_docs"] == doc_id)[:, 0]
253
+ text = [dic["words"][id] for id in idces]
254
+ POS_tags_ = [dic["POS_tags"][id] for id in idces]
255
+
256
+ data = {
257
+ "id": str(key),
258
+ "document_id": doc_id,
259
+ "passages": [],
260
+ "entities": [],
261
+ "relations": [],
262
+ "events": [],
263
+ "coreferences": [],
264
+ }
265
+ key += 1
266
+
267
+ data["passages"] = [
268
+ {
269
+ "id": str(key + i),
270
+ "type": "sentence",
271
+ "text": [text[i]],
272
+ "offsets": [[i, i + 1]],
273
  }
274
+ for i in range(len(text))
275
+ ]
276
+ key += len(text)
 
 
 
277
 
278
+ for i in range(len(text)):
279
+ entity = {
280
+ "id": key,
281
+ "type": "POS_tag",
282
+ "text": [POS_tags_[i]],
283
+ "offsets": [[i, i + 1]],
284
+ "normalized": [],
 
285
  }
286
+ data["entities"].append(entity)
287
  key += 1
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  yield key, data