ArneBinder
commited on
Commit
•
06be6e9
1
Parent(s):
d0a3f11
from https://github.com/ArneBinder/pie-datasets/pull/150
Browse files- drugprot.py +125 -13
drugprot.py
CHANGED
@@ -30,7 +30,7 @@ class DrugprotBigbioDocument(TextBasedDocument):
|
|
30 |
|
31 |
|
32 |
def example2drugprot(example: Dict[str, Any]) -> DrugprotDocument:
|
33 |
-
metadata = {"entity_ids": []}
|
34 |
id2labeled_span: Dict[str, LabeledSpan] = {}
|
35 |
|
36 |
document = DrugprotDocument(
|
@@ -40,6 +40,7 @@ def example2drugprot(example: Dict[str, Any]) -> DrugprotDocument:
|
|
40 |
id=example["document_id"],
|
41 |
metadata=metadata,
|
42 |
)
|
|
|
43 |
for span in example["entities"]:
|
44 |
labeled_span = LabeledSpan(
|
45 |
start=span["offset"][0],
|
@@ -47,23 +48,30 @@ def example2drugprot(example: Dict[str, Any]) -> DrugprotDocument:
|
|
47 |
label=span["type"],
|
48 |
)
|
49 |
document.entities.append(labeled_span)
|
50 |
-
|
51 |
-
|
|
|
|
|
52 |
for relation in example["relations"]:
|
|
|
|
|
53 |
document.relations.append(
|
54 |
BinaryRelation(
|
55 |
-
head=id2labeled_span[
|
56 |
-
tail=id2labeled_span[
|
57 |
label=relation["type"],
|
58 |
)
|
59 |
)
|
|
|
|
|
|
|
60 |
return document
|
61 |
|
62 |
|
63 |
def example2drugprot_bigbio(example: Dict[str, Any]) -> DrugprotBigbioDocument:
|
64 |
text = " ".join([" ".join(passage["text"]) for passage in example["passages"]])
|
65 |
doc_id = example["document_id"]
|
66 |
-
metadata = {"entity_ids": []}
|
67 |
id2labeled_span: Dict[str, LabeledSpan] = {}
|
68 |
|
69 |
document = DrugprotBigbioDocument(
|
@@ -79,7 +87,7 @@ def example2drugprot_bigbio(example: Dict[str, Any]) -> DrugprotBigbioDocument:
|
|
79 |
label=passage["type"],
|
80 |
)
|
81 |
)
|
82 |
-
# We sort labels and relation to always have
|
83 |
for span in example["entities"]:
|
84 |
labeled_span = LabeledSpan(
|
85 |
start=span["offsets"][0][0],
|
@@ -87,19 +95,114 @@ def example2drugprot_bigbio(example: Dict[str, Any]) -> DrugprotBigbioDocument:
|
|
87 |
label=span["type"],
|
88 |
)
|
89 |
document.entities.append(labeled_span)
|
90 |
-
|
91 |
-
|
|
|
|
|
92 |
for relation in example["relations"]:
|
|
|
|
|
93 |
document.relations.append(
|
94 |
BinaryRelation(
|
95 |
-
head=id2labeled_span[
|
96 |
-
tail=id2labeled_span[
|
97 |
label=relation["type"],
|
98 |
)
|
99 |
)
|
|
|
|
|
|
|
100 |
return document
|
101 |
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
class Drugprot(GeneratorBasedBuilder):
|
104 |
DOCUMENT_TYPES = {
|
105 |
"drugprot_source": DrugprotDocument,
|
@@ -144,8 +247,7 @@ class Drugprot(GeneratorBasedBuilder):
|
|
144 |
raise ValueError(f"Unknown dataset name: {self.config.name}")
|
145 |
|
146 |
def _generate_document(
|
147 |
-
self,
|
148 |
-
example: Dict[str, Any],
|
149 |
) -> Union[DrugprotDocument, DrugprotBigbioDocument]:
|
150 |
if self.config.name == "drugprot_source":
|
151 |
return example2drugprot(example)
|
@@ -153,3 +255,13 @@ class Drugprot(GeneratorBasedBuilder):
|
|
153 |
return example2drugprot_bigbio(example)
|
154 |
else:
|
155 |
raise ValueError(f"Unknown dataset config name: {self.config.name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
def example2drugprot(example: Dict[str, Any]) -> DrugprotDocument:
|
33 |
+
metadata = {"entity_ids": [], "relation_ids": []}
|
34 |
id2labeled_span: Dict[str, LabeledSpan] = {}
|
35 |
|
36 |
document = DrugprotDocument(
|
|
|
40 |
id=example["document_id"],
|
41 |
metadata=metadata,
|
42 |
)
|
43 |
+
|
44 |
for span in example["entities"]:
|
45 |
labeled_span = LabeledSpan(
|
46 |
start=span["offset"][0],
|
|
|
48 |
label=span["type"],
|
49 |
)
|
50 |
document.entities.append(labeled_span)
|
51 |
+
entity_id = span["id"].split("_")[1]
|
52 |
+
document.metadata["entity_ids"].append(entity_id)
|
53 |
+
id2labeled_span[entity_id] = labeled_span
|
54 |
+
|
55 |
for relation in example["relations"]:
|
56 |
+
arg1_id = relation["arg1_id"].split("_")[1]
|
57 |
+
arg2_id = relation["arg2_id"].split("_")[1]
|
58 |
document.relations.append(
|
59 |
BinaryRelation(
|
60 |
+
head=id2labeled_span[arg1_id],
|
61 |
+
tail=id2labeled_span[arg2_id],
|
62 |
label=relation["type"],
|
63 |
)
|
64 |
)
|
65 |
+
relation_id = "R" + relation["id"].split("_")[1]
|
66 |
+
document.metadata["relation_ids"].append(relation_id)
|
67 |
+
|
68 |
return document
|
69 |
|
70 |
|
71 |
def example2drugprot_bigbio(example: Dict[str, Any]) -> DrugprotBigbioDocument:
|
72 |
text = " ".join([" ".join(passage["text"]) for passage in example["passages"]])
|
73 |
doc_id = example["document_id"]
|
74 |
+
metadata = {"entity_ids": [], "relation_ids": []}
|
75 |
id2labeled_span: Dict[str, LabeledSpan] = {}
|
76 |
|
77 |
document = DrugprotBigbioDocument(
|
|
|
87 |
label=passage["type"],
|
88 |
)
|
89 |
)
|
90 |
+
# We sort labels and relation to always have a deterministic order for testing purposes.
|
91 |
for span in example["entities"]:
|
92 |
labeled_span = LabeledSpan(
|
93 |
start=span["offsets"][0][0],
|
|
|
95 |
label=span["type"],
|
96 |
)
|
97 |
document.entities.append(labeled_span)
|
98 |
+
entity_id = span["id"].split("_")[1]
|
99 |
+
document.metadata["entity_ids"].append(entity_id)
|
100 |
+
id2labeled_span[entity_id] = labeled_span
|
101 |
+
|
102 |
for relation in example["relations"]:
|
103 |
+
arg1_id = relation["arg1_id"].split("_")[1]
|
104 |
+
arg2_id = relation["arg2_id"].split("_")[1]
|
105 |
document.relations.append(
|
106 |
BinaryRelation(
|
107 |
+
head=id2labeled_span[arg1_id],
|
108 |
+
tail=id2labeled_span[arg2_id],
|
109 |
label=relation["type"],
|
110 |
)
|
111 |
)
|
112 |
+
relation_id = "R" + relation["id"].split("_")[1]
|
113 |
+
document.metadata["relation_ids"].append(relation_id)
|
114 |
+
|
115 |
return document
|
116 |
|
117 |
|
118 |
+
def drugprot2example(doc: DrugprotDocument) -> Dict[str, Any]:
|
119 |
+
entities = []
|
120 |
+
for i, entity in enumerate(doc.entities):
|
121 |
+
entities.append(
|
122 |
+
{
|
123 |
+
"id": doc.id + "_" + doc.metadata["entity_ids"][i],
|
124 |
+
"type": entity.label,
|
125 |
+
"text": doc.text[entity.start : entity.end],
|
126 |
+
"offset": [entity.start, entity.end],
|
127 |
+
}
|
128 |
+
)
|
129 |
+
|
130 |
+
relations = []
|
131 |
+
for i, relation in enumerate(doc.relations):
|
132 |
+
relations.append(
|
133 |
+
{
|
134 |
+
"id": doc.id + "_" + doc.metadata["relation_ids"][i][1:],
|
135 |
+
"arg1_id": doc.id
|
136 |
+
+ "_"
|
137 |
+
+ doc.metadata["entity_ids"][doc.entities.index(relation.head)],
|
138 |
+
"arg2_id": doc.id
|
139 |
+
+ "_"
|
140 |
+
+ doc.metadata["entity_ids"][doc.entities.index(relation.tail)],
|
141 |
+
"type": relation.label,
|
142 |
+
}
|
143 |
+
)
|
144 |
+
|
145 |
+
return {
|
146 |
+
"document_id": doc.id,
|
147 |
+
"title": doc.title,
|
148 |
+
"abstract": doc.abstract,
|
149 |
+
"text": doc.text,
|
150 |
+
"entities": entities,
|
151 |
+
"relations": relations,
|
152 |
+
}
|
153 |
+
|
154 |
+
|
155 |
+
def drugprot_bigbio2example(doc: DrugprotBigbioDocument) -> Dict[str, Any]:
|
156 |
+
entities = []
|
157 |
+
for i, entity in enumerate(doc.entities):
|
158 |
+
entities.append(
|
159 |
+
{
|
160 |
+
"id": doc.id + "_" + doc.metadata["entity_ids"][i],
|
161 |
+
"normalized": [],
|
162 |
+
"offsets": [[entity.start, entity.end]],
|
163 |
+
"type": entity.label,
|
164 |
+
"text": [doc.text[entity.start : entity.end]],
|
165 |
+
}
|
166 |
+
)
|
167 |
+
|
168 |
+
relations = []
|
169 |
+
for i, relation in enumerate(doc.relations):
|
170 |
+
relations.append(
|
171 |
+
{
|
172 |
+
"id": doc.id + "_" + doc.metadata["relation_ids"][i][1:],
|
173 |
+
"arg1_id": doc.id
|
174 |
+
+ "_"
|
175 |
+
+ doc.metadata["entity_ids"][doc.entities.index(relation.head)],
|
176 |
+
"arg2_id": doc.id
|
177 |
+
+ "_"
|
178 |
+
+ doc.metadata["entity_ids"][doc.entities.index(relation.tail)],
|
179 |
+
"normalized": [],
|
180 |
+
"type": relation.label,
|
181 |
+
}
|
182 |
+
)
|
183 |
+
|
184 |
+
passages = []
|
185 |
+
for passage in doc.passages:
|
186 |
+
passages.append(
|
187 |
+
{
|
188 |
+
"id": doc.id + "_" + passage.label,
|
189 |
+
"text": [doc.text[passage.start : passage.end]],
|
190 |
+
"offsets": [[passage.start, passage.end]],
|
191 |
+
"type": passage.label,
|
192 |
+
}
|
193 |
+
)
|
194 |
+
|
195 |
+
return {
|
196 |
+
"coreferences": [],
|
197 |
+
"document_id": doc.id,
|
198 |
+
"entities": entities,
|
199 |
+
"events": [],
|
200 |
+
"id": doc.id,
|
201 |
+
"passages": passages,
|
202 |
+
"relations": relations,
|
203 |
+
}
|
204 |
+
|
205 |
+
|
206 |
class Drugprot(GeneratorBasedBuilder):
|
207 |
DOCUMENT_TYPES = {
|
208 |
"drugprot_source": DrugprotDocument,
|
|
|
247 |
raise ValueError(f"Unknown dataset name: {self.config.name}")
|
248 |
|
249 |
def _generate_document(
|
250 |
+
self, example: Dict[str, Any], **kwargs
|
|
|
251 |
) -> Union[DrugprotDocument, DrugprotBigbioDocument]:
|
252 |
if self.config.name == "drugprot_source":
|
253 |
return example2drugprot(example)
|
|
|
255 |
return example2drugprot_bigbio(example)
|
256 |
else:
|
257 |
raise ValueError(f"Unknown dataset config name: {self.config.name}")
|
258 |
+
|
259 |
+
def _generate_example(
|
260 |
+
self, document: Union[DrugprotDocument, DrugprotBigbioDocument], **kwargs
|
261 |
+
) -> Dict[str, Any]:
|
262 |
+
if isinstance(document, DrugprotBigbioDocument):
|
263 |
+
return drugprot_bigbio2example(document)
|
264 |
+
elif isinstance(document, DrugprotDocument):
|
265 |
+
return drugprot2example(document)
|
266 |
+
else:
|
267 |
+
raise ValueError(f"Unknown document type: {type(document)}")
|