-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data_backup.py
More file actions
198 lines (171 loc) · 6.19 KB
/
prepare_data_backup.py
File metadata and controls
198 lines (171 loc) · 6.19 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from datetime import datetime
from IPython.core.debugger import set_trace
import bert
import run_classifier
from bert import optimization
from bert import tokenization
from tensorflow import keras
import os
import re
import pandas as pd
MAX_SEQ_LENGTH = 128
os.environ['TFHUB_CACHE_DIR'] = '/home/djjindal/bert/script-learning'
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# This is a path to an uncased (all lowercase) version of BERT
BERT_MODEL_HUB = "https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1"
def create_tokenizer_from_hub_module():
"""Get the vocab file and casing info from the Hub module."""
#set_trace()
with tf.Graph().as_default():
bert_module = hub.Module(BERT_MODEL_HUB)
tokenization_info = bert_module(signature="tokenization_info", as_dict=True)
with tf.Session() as sess:
vocab_file, do_lower_case = sess.run([tokenization_info["vocab_file"],
tokenization_info["do_lower_case"]])
return bert.tokenization.FullTokenizer(
vocab_file=vocab_file, do_lower_case=do_lower_case)
tokenizer = create_tokenizer_from_hub_module()
"""# Make Data InputFeatures"""
#If candidates is list of strings, entity can be None
def convert_single_example(example, candidates, entity, label, max_seq_length,
tokenizer):
tokens = []
segment_ids = []
tokens.append("[CLS]")
segment_ids.append(0)
i = 0
for line in example:
for orig_token in line.split(" "):
temp = tokenizer.tokenize(orig_token)
for t in temp:
tokens.append(t)
segment_ids.append(i)
tokens.append("[SEP]")
segment_ids.append(i)
i = i+1
for candidate in candidates:
if type(candidate) == str:
for orig_token in candidate.split(" "):
temp = tokenizer.tokenize(orig_token)
for t in temp:
tokens.append(t)
segment_ids.append(i)
i += 1
elif type(candidate) == tuple or type(candidate) == list:
for svo in candidate:
if svo is None:
svo = entity
for orig_token in svo.split(" "):
temp = tokenizer.tokenize(orig_token)
for t in temp:
tokens.append(t)
segment_ids.append(i)
tokens.append("[SEP]")
segment_ids.append(i)
i += 1
else:
raise TypeError("candidates is not string tuple, or list")
input_ids = tokenizer.convert_tokens_to_ids(tokens)
input_mask = [1] * len(input_ids)
# print(len(input_mask), len(segment_ids))
# print(input_ids)
assert len(input_ids) <= max_seq_length
assert len(input_mask) <= max_seq_length
assert len(segment_ids) <= max_seq_length
while len(input_ids) < max_seq_length:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
# print("input_ids", input_ids)
# print("input_mask", input_mask)
# print("segment_ids", segment_ids)
# print("label", label-1)
# set_trace()
feature = run_classifier.InputFeatures(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
label_id=label+1,
is_real_example=True)
return feature
def convert_single_example2(example, candidates, entity, label, max_seq_length,
tokenizer):
tokens_e = []
segment_ids_e = []
input_id_list = []
input_mask_list = []
segment_id_list = []
tokens_e.append("[CLS]")
segment_ids_e.append(0)
for line in example:
for orig_token in line.split(" "):
temp = tokenizer.tokenize(orig_token)
for t in temp:
tokens_e.append(t)
segment_ids_e.append(0)
tokens_e.append("[SEP]")
segment_ids_e.append(0)
for candidate in candidates:
if type(candidate) == tuple or type(candidate) == list:
tokens = []
segment_ids = []
tokens.extend(tokens_e)
segment_ids.extend(segment_ids_e)
for svo in candidate:
if svo is None:
svo = entity
for orig_token in svo.split(" "):
temp = tokenizer.tokenize(orig_token)
for t in temp:
tokens.append(t)
segment_ids.append(1)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
input_mask = [1] * len(input_ids)
# set_trace()
assert len(input_ids) <= max_seq_length
assert len(input_mask) <= max_seq_length
assert len(segment_ids) <= max_seq_length
while len(input_ids) < max_seq_length:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
input_id_list.append(input_ids)
input_mask_list.append(input_mask)
segment_id_list.append(segment_ids)
feature = run_classifier.InputFeatures(
input_ids=input_id_list,
input_mask=input_mask_list,
segment_ids=segment_id_list,
label_id=label+1,
is_real_example=True)
return feature
# def convert_examples_to_features(examples, candidates, label_list, max_seq_length,
# tokenizer):
# """Convert a set of `InputExample`s to a list of `InputFeatures`."""
# features = []
# for (example) in (examples):
# feature = convert_single_example(example, candidates[i], label_list[i], max_seq_length, tokenizer)
# features.append(feature)
# return features
# def createData(file):
# data = pd.read_csv(file)
# train = (data[['InputSentence1', 'InputSentence2', 'InputSentence3', 'InputSentence4']]).values.tolist()
# candidates = (data[['RandomFifthSentenceQuiz1', 'RandomFifthSentenceQuiz2']]).values.tolist()
# label_lists = (data[['AnswerRightEnding']]).values.tolist()
# label_list = []
# for label in label_lists:
# label_list.append(label[0])
# train_features = convert_examples_to_features(train, candidates,label_list, MAX_SEQ_LENGTH, tokenizer)
# return train_features
def tokenize_dataset_dict(ec_dict):
train_sents = ec_dict['sentences']
train_triples = ec_dict['triples']
candidates = ec_dict['candidates']
correct_ending = ec_dict['correct']
entity = ec_dict['entity']
train_features = convert_single_example2(train_sents[:-1], candidates, entity, correct_ending, MAX_SEQ_LENGTH, tokenizer)
return train_features