-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencoding.py
245 lines (190 loc) · 8.11 KB
/
encoding.py
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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import re
from pathlib import Path
from typing import Dict, List, Tuple
from fire import Fire
from pydantic import BaseModel
from tqdm import tqdm
from transformers import AutoTokenizer
from nltk import word_tokenize
from transformer_base import run_summarization
from utils import RelationData, RelationSentence
class Encoder(BaseModel):
def encode_x(self, x: str) -> str:
raise NotImplementedError
def encode(self, sent: RelationSentence) -> Tuple[str, str]:
raise NotImplementedError
def decode(self, x: str, y: str) -> RelationSentence:
raise NotImplementedError
def decode_x(self, x: str) -> str:
raise NotImplementedError
def safe_decode(self, x: str, y: str) -> RelationSentence:
text = self.decode_x(x)
try:
s = self.decode(x=x, y=y)
except Exception as e:
s = RelationSentence(
tokens=text.split(), head=[], tail=[], label="", error=str(e), raw=y
)
return s
def encode_to_line(self, sent: RelationSentence) -> str:
raise NotImplementedError
def decode_from_line(self, line: str) -> RelationSentence:
raise NotImplementedError
def parse_line(self, line: str) -> Tuple[str, str]:
raise NotImplementedError
def decode_(self, x: str, y: str) -> RelationSentence:
raise NotImplementedError
def re_line(self, line: str) -> str:
# https://stackoverflow.com/questions/44263446/python-regex-to-add-space-after-dot-or-comma
line_0 = re.sub(r'(?<=[.,:?"])(?=[^\s])', r' ', line)
line = re.sub(r'(?=[.,:?"])(?<=[^\s])', r' ', line_0)
return line
class GenerateEncoder(Encoder):
def encode_x(self, r: str) -> str:
return f"Relation : {r} ."
def decode_x(self, text: str) -> str:
return text.split("Relation : ")[-1][:-2]
def encode_triplet(self, sent: RelationSentence) -> str:
s, r, o = sent.as_tuple()
return f"Context : {sent.text} Head Entity : {s} , Tail Entity : {o} ."
def decode_triplet(self, text: str, label: str) -> RelationSentence:
front, back = text.split(" Head Entity : ")
_, context = front.split("Context : ")
head, back = back.split(" , Tail Entity : ")
tail = back[:-2]
return RelationSentence.from_spans(context, head, tail, label)
def encode_y(self, sent: RelationSentence) -> str:
return self.encode_x(sent.label) + " " + self.encode_triplet(sent)
def decode_y(self, text: str, label: str) -> RelationSentence:
del label
front, back = text.split(" . Context : ")
label = self.decode_x(front + " .")
return self.decode_triplet("Context : " + back, label)
def decode(self, x: str, y: str) -> RelationSentence:
r = self.decode_x(x)
sent = self.decode_y(y, r)
return sent
def encode(self, sent: RelationSentence) -> Tuple[str, str]:
x = self.encode_x(sent.label)
y = self.encode_y(sent)
return x, y
def decode_from_line(self, line: str) -> RelationSentence:
x, y = self.parse_line(line)
return self.decode(x, y)
def encode_to_line(self, sent: RelationSentence) -> str:
x, y = self.encode(sent)
return y + "\n"
def parse_line(self, line: str) -> Tuple[str, str]:
line_ = " ".join(word_tokenize(line.strip()))
return "", line_
class ExtractEncoder(Encoder):
def encode_x(self, text: str) -> str:
return f"Context : {text}"
def encode_x_(self, text: str) -> str:
mask = '<mask>'
return f"Context : {text} Head Entity : {mask} , Tail Entity : {mask} , Relation : {mask} ."
def decode_x(self, x: str) -> str:
return x.split("Context : ")[-1]
def decode_x_(self, x: str) -> str:
end = x.split("Context : ")[-1]
return end.split(" Head Entity :")[0]
def encode_y(self, sent: RelationSentence) -> str:
s, r, o = sent.as_tuple()
return f"Head Entity : {s} , Tail Entity : {o} , Relation : {r} ."
def decode_y(self, x: str, y: str) -> RelationSentence:
context = self.decode_x(x)
front, label = y.split(" , Relation : ")
label = label[:-2]
front, tail = front.split(" , Tail Entity : ")
_, head = front.split("Head Entity : ")
return RelationSentence.from_spans(context, head, tail, label)
def decode_y_(self, x: str, y: str) -> RelationSentence:
context = self.decode_x_(x)
front, label = y.split(" , Relation : ")
label = label[:-2]
front, tail = front.split(" , Tail Entity : ")
_, head = front.split("Head Entity : ")
return RelationSentence.from_spans(context, head, tail, label)
def encode_entity_prompt(self, head: str, tail: str) -> str:
return f"Head Entity : {head} , Tail Entity : {tail} , Relation :"
def encode(self, sent: RelationSentence) -> Tuple[str, str]:
x = self.encode_x(sent.text)
y = self.encode_y(sent)
return x, y
def decode(self, x: str, y: str) -> RelationSentence:
return self.decode_y(x, y)
def decode_(self, x: str, y: str) -> RelationSentence:
return self.decode_y_(x, y)
def safe_decode_(self, x: str, y: str) -> RelationSentence:
text = self.decode_x_(x)
try:
s = self.decode_(x=x, y=y)
except Exception as e:
s = RelationSentence(
tokens=text.split(), head=[], tail=[], label="", error=str(e), raw=y
)
return s
def encode_to_line(self, sent: RelationSentence) -> str:
x, y = self.encode(sent)
return run_summarization.encode_to_line(x, y)
def decode_from_line(self, line: str) -> RelationSentence:
x, y = self.parse_line(line)
return self.decode(x, y)
def parse_line(self, line: str) -> Tuple[str, str]:
return run_summarization.decode_from_line(line)
def test_encoders(
paths: List[str] = [
"outputs/data/zsl/wiki/unseen_5_seed_0/train.jsonl",
"outputs/data/zsl/fewrel/unseen_5_seed_0/train.jsonl",
],
print_limit: int = 4,
encoder_names: List[str] = ["generate", "extract"],
limit: int = 1000,
):
encoders = {k: select_encoder(k) for k in encoder_names}
for p in paths:
data = RelationData.load(Path(p))
_, data = data.train_test_split(min(limit, len(data.sents)), random_seed=0)
for name, e in tqdm(list(encoders.items())):
num_fail = 0
print(dict(name=name, p=p))
for s in data.sents:
encoded = e.encode_to_line(s)
x, y = e.parse_line(encoded)
decoded: RelationSentence = e.safe_decode(x, y)
if decoded.as_tuple() != s.as_tuple():
if num_fail < print_limit:
print(dict(gold=s.as_tuple(), text=s.text))
print(dict(pred=decoded.as_tuple(), text=decoded.text))
print(dict(x=x, y=y, e=decoded.error))
print()
num_fail += 1
print(dict(success_rate=1 - (num_fail / len(data.sents))))
print("#" * 80)
def select_encoder(name: str) -> Encoder:
mapping: Dict[str, Encoder] = dict(
extract=ExtractEncoder(),
generate=GenerateEncoder(),
)
encoder = mapping[name]
return encoder
def test_entity_prompts(
path: str = "outputs/data/zsl/wiki/unseen_10_seed_0/test.jsonl", limit: int = 100
):
def tokenize(text: str, tok) -> List[str]:
return tok.convert_ids_to_tokens(tok(text, add_special_tokens=False).input_ids)
data = RelationData.load(Path(path))
e = ExtractEncoder()
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-base")
print(tokenizer)
for i, s in enumerate(tqdm(data.sents[:limit])):
head, label, tail = s.as_tuple()
x, y = e.encode(s)
prompt = e.encode_entity_prompt(head, tail)
tokens_y = tokenize(y, tokenizer)
tokens_prompt = tokenize(prompt, tokenizer)
assert tokens_y[: len(tokens_prompt)] == tokens_prompt
if i < 3:
print(tokens_y)
if __name__ == "__main__":
Fire()