-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathprepare_data.py
159 lines (125 loc) · 5.58 KB
/
prepare_data.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
"""
Prepare vocabulary, initial word vectors and tagging scheme.
"""
import json
import pickle
import argparse
import numpy as np
from collections import Counter, defaultdict
from utils import vocab, helper, constant
import codecs
def parse_args():
parser = argparse.ArgumentParser(description='Prepare vocab for joint relation extraction.')
parser.add_argument('--data_dir', default='dataset/NYT-multi/data', help='Input data directory.')
parser.add_argument('--vocab_dir', default='dataset/NYT-multi/vocab', help='Output vocab directory.')
parser.add_argument('--emb_dir', default='dataset/embedding', help='GloVe directory.')
parser.add_argument('--wv_file', default='glove.840B.300d.txt', help='GloVe vector file.')
parser.add_argument('--wv_dim', type=int, default=300, help='GloVe vector dimension.')
parser.add_argument('--min_freq', type=int, default=0, help='If > 0, use min_freq as the cutoff.')
parser.add_argument('--lower', action='store_true', help='If specified, lowercase all words.')
args = parser.parse_args()
return args
def main():
args = parse_args()
# input files
train_file = args.data_dir + '/train.json'
dev_file = args.data_dir + '/dev.json'
test_file = args.data_dir + '/test.json'
schema_file = args.data_dir + '/schemas.json'
wv_file = args.emb_dir + '/' + args.wv_file
wv_dim = args.wv_dim
# output files
helper.ensure_dir(args.vocab_dir)
vocab_file = args.vocab_dir + '/vocab.pkl'
char_file = args.vocab_dir + '/chars.json'
emb_file = args.vocab_dir + '/embedding.npy'
# load files
print("loading files...")
train_tokens = load_tokens(train_file)
dev_tokens = load_tokens(dev_file)
test_tokens = load_tokens(test_file)
if args.lower:
train_tokens, dev_tokens, test_tokens = [[t.lower() for t in tokens] for tokens in\
(train_tokens, dev_tokens, test_tokens)]
# load glove
print("loading glove...")
glove_vocab = vocab.load_glove_vocab(wv_file, wv_dim)
print("{} words loaded from glove.".format(len(glove_vocab)))
print("building vocab...")
v = build_vocab(train_tokens, glove_vocab, args.min_freq)
print("calculating oov...")
datasets = {'train': train_tokens, 'dev': dev_tokens, 'test': test_tokens}
for dname, d in datasets.items():
total, oov = count_oov(d, v)
print("{} oov: {}/{} ({:.2f}%)".format(dname, oov, total, oov*100.0/total))
print("building embeddings...")
embedding = vocab.build_embedding(wv_file, v, wv_dim)
print("embedding size: {} x {}".format(*embedding.shape))
print("dumping embeddings to files...")
with open(vocab_file, 'wb') as outfile:
pickle.dump(v, outfile)
np.save(emb_file, embedding)
# print("all done.")
print("building schemas...")
all_schemas = set()
subj_type = set()
obj_type = set()
min_count = 2
pos_tags = set()
chars = defaultdict(int)
with open(train_file) as f:
a = json.load(f)
for ins in a:
for spo in ins['spo_details']:
all_schemas.add(spo[3])
subj_type.add(spo[2])
obj_type.add(spo[6])
for pos in ins['pos_tags']:
pos_tags.add(pos)
for token in ins['tokens']:
for char in token:
chars[char] += 1
id2predicate = {i+1:j for i,j in enumerate(all_schemas)} # 0表示终止类别
predicate2id = {j:i for i,j in id2predicate.items()}
id2subj_type = {i+1:j for i,j in enumerate(subj_type)} # 0表示终止类别
subj_type2id = {j:i for i,j in id2subj_type.items()}
id2obj_type = {i+1:j for i,j in enumerate(obj_type)} # 0表示终止类别
obj_type2id = {j:i for i,j in id2obj_type.items()}
with codecs.open(schema_file, 'w', encoding='utf-8') as f:
json.dump([id2predicate, predicate2id, id2subj_type, subj_type2id, id2obj_type, obj_type2id], f, indent=4, ensure_ascii=False)
print("dumping chars to files...")
with codecs.open(char_file, 'w', encoding='utf-8') as f:
chars = {i:j for i,j in chars.items() if j >= min_count}
id2char = {i+2:j for i,j in enumerate(chars)} # padding: 0, unk: 1
char2id = {j:i for i,j in id2char.items()}
id2pos = {i+2:j for i,j in enumerate(pos_tags)} # padding: 0, unk: 1
pos2id = {j:i for i,j in id2pos.items()}
json.dump([id2char, char2id, id2pos, pos2id], f, indent=4, ensure_ascii=False)
def load_tokens(filename):
with open(filename) as infile:
data = json.load(infile)
tokens = []
for d in data:
# tokens.extend([word["word"] for word in d['postag']])
tokens.extend(d['tokens'])
print("{} tokens from {} examples loaded from {}.".format(len(tokens), len(data), filename))
return tokens
def build_vocab(tokens, glove_vocab, min_freq):
""" build vocab from tokens and glove words. """
counter = Counter(t for t in tokens)
# if min_freq > 0, use min_freq, otherwise keep all glove words
if min_freq > 0:
v = sorted([t for t in counter if counter.get(t) >= min_freq], key=counter.get, reverse=True)
else:
v = sorted([t for t in counter if t in glove_vocab], key=counter.get, reverse=True)
# add special tokens and entity mask tokens
v = constant.VOCAB_PREFIX + v
print("vocab built with {}/{} words.".format(len(v), len(counter)))
return v
def count_oov(tokens, vocab):
c = Counter(t for t in tokens)
total = sum(c.values())
matched = sum(c[t] for t in vocab)
return total, total-matched
if __name__ == '__main__':
main()