schlevik commited on
Commit ·
35b2ba6
1
Parent(s): 7b45cb5
fix text script
Browse files
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 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
"
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
}
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 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 |
-
|
| 230 |
-
|
| 231 |
-
"
|
| 232 |
-
"
|
| 233 |
-
"
|
| 234 |
-
"
|
| 235 |
-
"
|
| 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
|