-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons.py
38 lines (29 loc) · 1.12 KB
/
commons.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
import pickle
import numpy as np
def transform_sparse_model_to_dense_matrix(bow_corpus, vector_length):
dense_matrix = []
for tokenized_line in bow_corpus:
cur_pos = 0
dense_vector = []
for i in range(0, vector_length):
if len(tokenized_line) > 0 and tokenized_line[cur_pos][0] == i:
dense_vector.append(tokenized_line[cur_pos][1])
cur_pos = min(cur_pos + 1, len(tokenized_line) - 1)
else:
dense_vector.append(0)
dense_matrix.append(np.array(dense_vector))
dense_matrix = np.array(dense_matrix)
return dense_matrix
def write_list(a_list, file_path):
# store list in binary file so 'wb' mode
with open(file_path, 'wb+') as fp:
pickle.dump(a_list, fp)
print('Done writing list into a binary file')
def read_list(filename):
# for reading also binary mode is important
with open(filename, 'rb') as fp:
n_list = pickle.load(fp)
return n_list
def split_file(file, chunksize):
chunks = [file[x:x + chunksize] for x in range(0, len(file), chunksize)]
return chunks