-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare_data_for_re.py
154 lines (130 loc) · 5.54 KB
/
prepare_data_for_re.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
import json
import os.path
from argparse import Namespace, ArgumentParser
import numpy as np
import torch
from tqdm import tqdm
from models.bert_crf import BertCrf
from re_utils.common import load_jsonl
from re_utils.ner import get_tags_with_positions, get_mean_vector_from_segment
def configure_arg_parser():
arg_parser = ArgumentParser()
arg_parser.add_argument(
"--bert-crf-path",
type=str,
default="weights/bert-crf.pt",
help="The path to the file with bert crf weights",
)
arg_parser.add_argument(
"--labeled-texts",
type=str,
default="resources/data/train/labeled_texts.jsonl",
help="the path to the file with preprocessed texts",
)
arg_parser.add_argument(
"--relations",
type=str,
default="resources/data/train/relations.jsonl",
help="the path to the file with relations between entities",
)
arg_parser.add_argument("--num-labels", type=int, default=17, help="number of possible tags for tokens")
arg_parser.add_argument(
"--bert-name",
type=str,
default="sberbank-ai/ruBert-base",
help="local path or hf hub BERT name that underlies the model",
)
arg_parser.add_argument("--bert-dropout", type=float, default=0.2, help="dropout probability")
arg_parser.add_argument(
"--use-crf",
type=bool,
default=True,
help="whether use conditional random field or not",
)
arg_parser.add_argument(
"--label2id",
type=str,
default="resources/data/train/label2id.json",
help="Json file with mapping from entity label to its id",
)
arg_parser.add_argument(
"--retag2id",
type=str,
default="resources/data/train/retag2id.json",
help="Json file with mapping from relation tag to its id",
)
return arg_parser
def main(args: Namespace):
dir = os.path.dirname(args.labeled_texts)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = BertCrf(
num_labels=args.num_labels,
bert_name=args.bert_name,
dropout=args.bert_dropout,
use_crf=args.use_crf,
)
model.load_from(args.bert_crf_path)
model.eval()
model = model.to(device)
labeled_texts = load_jsonl(args.labeled_texts)
relations = load_jsonl(args.relations)
with open(args.label2id, "r") as label2id_file:
label2id = json.load(label2id_file)
id2label = {id: label for label, id in label2id.items()}
entity_tags_set = set()
for label, id in label2id.items():
if label == "O":
continue
entity_tags_set.add(label.split("-")[1])
entity_tag_to_id = {tag: id for id, tag in enumerate(entity_tags_set)}
with open(args.retag2id, "r") as retag2id_file:
retag2id = json.load(retag2id_file)
no_relation_tag = len(retag2id)
tag_counter = {tag: 0 for tag in retag2id.values()}
tag_counter[no_relation_tag] = 0
with open(os.path.join(dir, "re_data.jsonl"), "w") as relation_training_data_file:
for labeled_text, text_relations in tqdm(list(zip(labeled_texts, relations))):
assert labeled_text["id"] == text_relations["id"]
input_ids = torch.tensor([labeled_text["input_ids"]], device=device)
attention_mask = torch.ones(1, len(labeled_text["input_ids"]), device=device)
_, batched_bert_embeddings = model.get_bert_features(input_ids, attention_mask)
bert_embeddings = batched_bert_embeddings[0]
full_seq_embedding = get_mean_vector_from_segment(bert_embeddings, 0, len(bert_embeddings)).tolist()
labels = model.decode(input_ids, attention_mask)[0]
tags_pos = get_tags_with_positions(labels, id2label)
relation_matrix = np.empty((len(tags_pos), len(tags_pos)))
for i, first_arg in enumerate(tags_pos):
for j, second_arg in enumerate(tags_pos):
relation_tag = no_relation_tag
for relation in text_relations["relations"]:
if (
relation["arg1_tag"] == first_arg["tag"]
and relation["arg2_tag"] == second_arg["tag"]
and relation["arg1_pos"] == first_arg["pos"]
and relation["arg2_pos"] == second_arg["pos"]
):
relation_tag = relation["tag"]
break
relation_matrix[i][j] = relation_tag
entities_positions = [item["pos"] for item in tags_pos]
entities_embeddings = [
get_mean_vector_from_segment(bert_embeddings, pos[0], pos[1]).tolist() for pos in entities_positions
]
entities_tags = [entity_tag_to_id[item["tag"]] for item in tags_pos]
json.dump(
{
"id": labeled_text["id"],
"seq_embedding": full_seq_embedding,
"entities_embeddings": entities_embeddings,
"relation_matrix": relation_matrix.tolist(),
"entities_tags": entities_tags,
"entities_positions": entities_positions,
},
relation_training_data_file,
)
relation_training_data_file.write("\n")
with open(os.path.join(dir, "entity_tag_to_id.json"), "w") as entity_tag_to_id_file:
json.dump(entity_tag_to_id, entity_tag_to_id_file)
if __name__ == "__main__":
_args = configure_arg_parser().parse_args()
main(_args)