-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_reader.py
225 lines (151 loc) · 7.12 KB
/
data_reader.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
import numpy as np
import gensim
from gensim.corpora import WikiCorpus
import torch
from torch.utils.data import Dataset
import itertools
import os.path
import os
import sys
np.random.seed(12345)
class DataReader:
NEGATIVE_TABLE_SIZE = 1e6
def __init__(self, inFile, txtFile=None):
self.negatives = []
self.discards = []
self.negpos = 0
self.word2id = dict()
self.id2word = dict()
self.token_count = 0
self.word_freq = dict()
self.train_text = []
self.inFile = inFile
self.txtFile = txtFile
self.xml_to_txt()
self.read_words()
self.init_table_negatives()
# self.init_table_discards()
# self.test()
def xml_to_txt(self):
if not os.path.isfile(self.txtFile):
print(">>> Converting XML to TXT file...")
i = 0
wiki = WikiCorpus(self.inFile, lemmatize=False, dictionary={})
output = open(self.txtFile, 'w')
with open(self.txtFile, 'w') as f:
for text in wiki.get_texts():
f.write(' '.join(text) + '\n')
i = i + 1
if (i % 10000 == 0):
print("Saved {} articles".format(i))
else:
print(">>> Located TXT file {}...".format(self.txtFile))
def read_words(self, thr_freq=5):
word_freq = dict()
with open(self.txtFile, 'r') as f:
# self.sentences_count = len(f.readLines())
corpus = np.array(f.read().split(' '))
print("Total Words:",len(corpus))
# 0. Prepare variables
unique_elements, counts_elements = np.unique(corpus, return_counts=True)
word_freq = {word : counts_elements[i] for i, word in enumerate(unique_elements)}
word2Ind = {word : i for i, word in enumerate(unique_elements)}
# 1. Remove those words that barely appear in the corpus
print("Removing Low Freq words:",len(corpus))
del_idx = list([])
del_el = set(word for word, freq in word_freq.items() if freq < thr_freq)
for i, word in enumerate(corpus):
if word in del_el:
del_idx.append(i)
corpus = np.delete(corpus, del_idx)
print("Reduced Corpus:",len(corpus))
# 2. Subsampling
del_idx = list([])
corpus_size = len(corpus)
for i, word in enumerate(corpus):
idx = word2Ind[word]
freq = counts_elements[idx] / corpus_size
prob = (np.sqrt(freq / 1e-3) + 1) * (1e-3 / freq)
prob = 1.0 - prob
if np.random.uniform() <= prob:
del_idx.append(i)
print("Deleting Sampled words...")
corpus_sampled = np.delete(corpus, del_idx)
print("Sampled Corpus Size:",len(corpus_sampled))
# 2.5 Removing words with less than 3 characters
corpus_sampled = [word for word in corpus_sampled if len(word) >= 3]
# 3. Word2Ind & Ind2Word
tokens = list({word for word in corpus_sampled})
tokens.sort()
self.token_count = len(tokens)
print("Creating word2Ind vectors...")
for i, token in enumerate(tokens):
self.word2id[token] = i
self.id2word[i] = token
# 4. Remove sequences of repeated words
del_rep = []
i = 0
while i < (len(corpus_sampled) - 1 ):
j = i + 1
while corpus_sampled[i] == corpus_sampled[j]:
del_rep.append(j)
j = j + 1
i += 1
print("Deleted Consecutive Words:", len(del_rep))
corpus_sampled = np.delete(corpus_sampled, del_rep)
print("Final Word Corpus Size:", len(corpus_sampled))
# 5. Create train_text
print("Creating train_text array...")
self.train_text = [self.word2id[word] for word in corpus_sampled]
# I use the word_freq after processing. Other examples exists where they use it before
unique_elements, counts_elements = np.unique(corpus_sampled, return_counts=True)
self.word_freq = {word : counts_elements[i] for i, word in enumerate(unique_elements)}
print("Tokens:", self.token_count)
def init_table_negatives(self):
pow_freq = np.array( list(self.word_freq.values())) ** 0.75
sum_pow_freq = np.sum(pow_freq)
ratio = pow_freq / sum_pow_freq
count = np.round(ratio * DataReader.NEGATIVE_TABLE_SIZE)
self.max_sample_id = len(count)
for wid, c in enumerate(count):
self.negatives += [wid] * int(c)
self.negatives = np.array(self.negatives)
np.random.shuffle(self.negatives)
# Pick a random number.Those elements that appear more have a higher probability.
def get_negatives(self, target, N, K=5):
res = np.random.choice(self.negatives, size=(N, K))
for i in range(len(res)):
for j in range(K):
if res[i,j] == target:
res[i,j] = np.random.choice(self.negatives, size=1)
return res
def test(self):
print("Table:")
print(self.train_text[22045:22085])
print([self.id2word[self.train_text[idx]] for idx in range(22045, 22085)])
sys.exit()
#================================================================================
class Word2VecDataset(Dataset):
def __init__(self, data, window_size):
self.data = data
self.window_size = window_size
def __len__(self):
return len(self.data.train_text)
# Returns centerInd(v), contextInd(u) & negSamples
def __getitem__(self, idx):
v = self.data.train_text[idx]
u = self.data.train_text[ max(0, idx - self.window_size) :
min(idx + self.window_size,len(self.data.train_text) )]
u = [i for i in u if i != v]
v = [v] * len(u)
neg = self.data.get_negatives(v[0], len(v))
return (v, u, neg)
@staticmethod
def collate(batches):
v_vec = [batch[0] for batch in batches]
u_vec = [batch[1] for batch in batches]
neg_mat = [batch[2] for batch in batches]
v_vec = list(itertools.chain(*v_vec))
u_vec = list(itertools.chain(*u_vec))
neg_mat = np.vstack(neg_mat)
return torch.LongTensor(v_vec), torch.LongTensor(u_vec), torch.LongTensor(neg_mat)