-
Notifications
You must be signed in to change notification settings - Fork 2
/
vocab_emb.py
executable file
·142 lines (110 loc) · 4.67 KB
/
vocab_emb.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
import pickle as pkl
import torch
# build vocab and word2id
signs = ['(', ')', '?', ';', '!', ', ', '[', ']', '<', '>', '"', ': ', '“', '$', '-', '+', '=', '*',
'#', '&', '_', '{', '}', '|']
replace_signs = [' ( ', ' ) ', ' ? ', ' ; ', ' ! ', ' , ', ' [ ', ' ] ', ' < ', ' > ', ' " ', ' : ', ' “ ', ' $ ', ' - ', ' + ', ' = ', ' * ',
' # ', ' & ', ' _ ', ' { ', ' } ', ' | ']
def replaceSignals(sentence):
sentence = sentence.replace('can\'t', 'can not')
sentence = sentence.replace('won\'t', 'will not')
sentence = sentence.replace('\'ve', ' have')
sentence = sentence.replace('\'', ' \'')
for i in range(len(signs)):
sentence = sentence.replace(signs[i], replace_signs[i])
# print(sentence.split())
return sentence
if __name__ == "__main__":
word_freq = {}
word_set = set()
with open('processed_data/train_exp_imp_6sen', 'rb') as f:
train_docs = pkl.load(f)
with open('processed_data/test_exp_imp_6sen', 'rb') as f:
test_docs = pkl.load(f)
with open('processed_data/valid_exp_imp_6sen', 'rb') as f:
valid_docs = pkl.load(f)
for doc in train_docs['exp']:
print(doc['pair'][0])
doc['pair'][0] = replaceSignals(doc['pair'][0])
print(doc['pair'][0])
doc['pair'][1] = replaceSignals(doc['pair'][1])
for i in range(len(doc['para'])):
doc['para'][i] = replaceSignals(doc['para'][i])
for doc in train_docs['imp']:
doc['pair'][0] = replaceSignals(doc['pair'][0])
doc['pair'][1] = replaceSignals(doc['pair'][1])
for i in range(len(doc['para'])):
doc['para'][i] = replaceSignals(doc['para'][i])
for doc in valid_docs['exp']:
doc['pair'][0] = replaceSignals(doc['pair'][0])
doc['pair'][1] = replaceSignals(doc['pair'][1])
for i in range(len(doc['para'])):
doc['para'][i] = replaceSignals(doc['para'][i])
for doc in valid_docs['imp']:
doc['pair'][0] = replaceSignals(doc['pair'][0])
doc['pair'][1] = replaceSignals(doc['pair'][1])
for i in range(len(doc['para'])):
doc['para'][i] = replaceSignals(doc['para'][i])
for doc in test_docs['exp']:
doc['pair'][0] = replaceSignals(doc['pair'][0])
doc['pair'][1] = replaceSignals(doc['pair'][1])
for i in range(len(doc['para'])):
doc['para'][i] = replaceSignals(doc['para'][i])
for doc in test_docs['imp']:
doc['pair'][0] = replaceSignals(doc['pair'][0])
doc['pair'][1] = replaceSignals(doc['pair'][1])
for i in range(len(doc['para'])):
doc['para'][i] = replaceSignals(doc['para'][i])
with open('processed_data/train_exp_imp_6sen_clean', 'wb') as f:
pkl.dump(train_docs, f, pkl.HIGHEST_PROTOCOL)
with open('processed_data/valid_exp_imp_6sen_clean', 'wb') as f:
pkl.dump(valid_docs, f, pkl.HIGHEST_PROTOCOL)
with open('processed_data/test_exp_imp_6sen_clean', 'wb') as f:
pkl.dump(test_docs, f, pkl.HIGHEST_PROTOCOL)
train_valid_test = train_docs['exp'] + valid_docs['exp'] + test_docs['exp']\
+ train_docs['imp'] + valid_docs['imp'] + test_docs['imp']
print(train_docs['exp'][10]['para'])
for doc in train_valid_test:
arg1 = doc['pair'][0].split()
arg2 = doc['pair'][1].split()
para_word = []
for sen in doc['para']:
para_word += sen.split()
for word in arg1 + arg2 + para_word:
word_set.add(word)
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
vocab = list(word_set)
vocab = ['PAD'] + vocab
vocab_size = len(vocab)
print('vocab', vocab_size)
word2id = dict([(x, y) for (y, x) in enumerate(vocab)])
print(len(word2id))
with open('processed_data/word2id', 'wb') as f:
pkl.dump(word2id, f, pkl.HIGHEST_PROTOCOL)
with open('processed_data/word2id', 'rb') as f:
cat2id = pkl.load(f)
vocab = [key for key in cat2id.keys()]
print(vocab)
with open("glove.840B.300d.txt", 'r', encoding='utf-8') as f:
lines = f.readlines()
embRec = {} # w_idx: 1
emb = torch.randn(len(vocab), 300) * 0.05
count = 0
for line in lines:
vals = line.rstrip("\n").split()
word = vals.pop(0)
# print(word)
if len(vals) != 300:
continue
assert len(vals) == 300
if word in cat2id:
count += 1
emb[cat2id[word], :] = torch.FloatTensor([float(v) for v in vals])
emb = emb.numpy()
print(count)
print(emb.shape)
with open('exp_imp_vocab_embed', 'wb') as f:
pkl.dump(emb, f, pkl.HIGHEST_PROTOCOL)