-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpreprocess_chemprot.py
More file actions
178 lines (139 loc) · 5.08 KB
/
preprocess_chemprot.py
File metadata and controls
178 lines (139 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import bisect
import itertools
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import polars as pl
from datasets import load_dataset
from datasets.dataset_dict import Dataset, DatasetDict
from datasets.utils import disable_progress_bars, enable_progress_bars
from rich.console import Console
def load_chemprot_dataset(cache_dir="./cache") -> DatasetDict:
disable_progress_bars()
dataset = load_dataset(
"bigbio/chemprot", "chemprot_full_source", cache_dir=cache_dir
)
enable_progress_bars()
return dataset
def clean_text(text: str):
return (
text.replace("'", "'")
.replace("\n", " <cr> ")
.replace("\r", " <cr> ")
.replace("\t", " ")
)
def expand_to_word_boundaries(start: int, end: int, text: str):
while start > 0 and text[start] != " ":
start -= 1
while text[start] == " ":
start += 1
while end < len(text) and text[end - 1] != " ":
end += 1
while text[end - 1] == " ":
end -= 1
return start, end
@dataclass
class Entity:
eid: str
start_word: int
end_word: int
label: str
def tags(self):
return [f"B-{self.label}"] + (
[f"I-{self.label}"] * (self.end_word - self.start_word - 1)
)
@dataclass
class Relation:
arg1: Entity
arg2: Entity
label: str
def cnlp_str(self):
return f"({self.arg1.start_word},{self.arg2.start_word},{self.label})"
class ChemprotRow:
def __init__(self, row: dict[str, Any]):
self.pmid: str = row["pmid"]
raw_text: str = row["text"]
boundaries = set([0, len(raw_text)])
for offsets in row["entities"]["offsets"]:
boundaries.update(offsets)
spans: list[str] = []
new_offsets: dict[int, int] = {0: 0}
clean_len = 0
for start, end in itertools.pairwise(sorted(boundaries)):
span = clean_text(raw_text[start:end])
clean_len += len(span)
new_offsets[end] = clean_len
spans.append(span)
self.text = "".join(spans)
self.n_words = len(self.text.split(" "))
self.entities: dict[str, Entity] = {}
space_idxs = [m.start() for m in re.finditer(" ", self.text)]
for eid, ent_label, (old_start, old_end) in zip(
row["entities"]["id"], row["entities"]["type"], row["entities"]["offsets"]
):
start = new_offsets[old_start]
end = new_offsets[old_end]
start, end = expand_to_word_boundaries(start, end, self.text)
start_word = bisect.bisect_right(space_idxs, start)
end_word = bisect.bisect_right(space_idxs, end)
entity = Entity(
eid=eid, start_word=start_word, end_word=end_word, label=ent_label
)
self.entities[eid] = entity
self.relations: list[Relation] = []
for rel_label, eid1, eid2 in zip(
row["relations"]["type"], row["relations"]["arg1"], row["relations"]["arg2"]
):
self.relations.append(
Relation(
arg1=self.entities[eid1], arg2=self.entities[eid2], label=rel_label
)
)
def _ner_str(self, entities: list[Entity]):
tags: list[str] = []
for entity in sorted(entities, key=lambda e: e.start_word):
if entity.start_word < len(tags):
continue # ignore overlapping entities for tagging
while entity.start_word > len(tags):
tags.append("O")
tags.extend(entity.tags())
while len(tags) < self.n_words:
tags.append("O")
return " ".join(tags)
def chemical_ner_str(self):
return self._ner_str(
[e for e in self.entities.values() if e.label.startswith("CHEMICAL")]
)
def gene_ner_str(self):
return self._ner_str(
[e for e in self.entities.values() if e.label.startswith("GENE")]
)
def relations_str(self):
if len(self.relations) == 0:
return "None"
return " , ".join([r.cnlp_str() for r in self.relations])
def preprocess_data(split: Dataset):
rows = [ChemprotRow(row) for row in split]
return pl.DataFrame(
{
"id": [r.pmid for r in rows],
"text": [r.text for r in rows],
"chemical_ner": [r.chemical_ner_str() for r in rows],
"gene_ner": [r.gene_ner_str() for r in rows],
"end_to_end": [r.relations_str() for r in rows],
}
)
if __name__ == "__main__":
console = Console()
out_dir = Path(__file__).parent / "dataset"
out_dir.mkdir(exist_ok=True)
with console.status("Loading dataset...") as st:
dataset = load_chemprot_dataset()
for split in ("train", "test", "validation"):
st.update(f"Preprocessing {split} data...")
preprocessed = preprocess_data(dataset[split])
preprocessed.write_csv(out_dir / f"{split}.tsv", separator="\t")
console.print(
f"[green i]Preprocessed chemprot data saved to [repr.filename]{out_dir}[/]."
)