-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
160 lines (125 loc) · 4.15 KB
/
utils.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
# -*- coding: utf-8 -*-
"""
@Time : 2018/7/17 下午2:42
@FileName: utils.py
@author: 王炳宁
@contact: [email protected]
"""
import itertools
import multiprocessing
import pickle
import random
import numpy as np
from joblib import Parallel, delayed
from tqdm import tqdm
np.random.seed(10245)
def DBC2SBC(ustring):
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 0x3000:
inside_code = 0x0020
else:
inside_code -= 0xfee0
if not (0x0021 <= inside_code <= 0x7e):
rstring += uchar
continue
rstring += chr(inside_code)
return rstring
def SBC2DBC(ustring):
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 0x0020:
inside_code = 0x3000
else:
if not (0x0021 <= inside_code <= 0x7e):
rstring += uchar
continue
inside_code += 0xfee0
rstring += chr(inside_code)
return rstring
def id_lst_to_string(id_lst, id2word):
return ''.join([id2word[x] for x in id_lst])
def write_lst_to_file(lst, filename):
output = '\n'.join(lst)
with open(filename, 'w', encoding='utf-8') as f:
f.write(output)
def dump_file(obj, filename):
f = open(filename, 'wb')
pickle.dump(obj, f)
def load_file(filename):
with open(filename, 'rb') as f:
data = pickle.load(f)
return data
def get_shuffle_data(data, dim=0):
pool = {}
for one in data:
length = len(one[dim])
if length not in pool:
pool[length] = []
pool[length].append(one)
for one in pool:
np.random.shuffle(pool[one])
length_lst = list(pool.keys())
random.shuffle(length_lst)
return [x for y in length_lst for x in pool[y]]
def remove_duplciate_lst(lst):
lst.sort()
return list(k for k, _ in itertools.groupby(lst))
def padding(sequence, pads=0, max_len=None, dtype='int32', return_matrix_for_size=False):
# we should judge the rank
if True or isinstance(sequence[0], list):
v_length = [len(x) for x in sequence] # every sequence length
seq_max_len = max(v_length)
if (max_len is None) or (max_len > seq_max_len):
max_len = seq_max_len
v_length = list(map(lambda z: z if z <= max_len else max_len, v_length))
x = (np.ones((len(sequence), max_len)) * pads).astype(dtype)
for idx, s in enumerate(sequence):
trunc = s[:max_len]
x[idx, :len(trunc)] = trunc
if return_matrix_for_size:
v_matrix = np.asanyarray([map(lambda item: 1 if item < line else 0, range(max_len)) for line in v_length],
dtype=dtype)
return x, v_matrix
return x, np.asarray(v_length, dtype='int32')
else:
seq_len = len(sequence)
if max_len is None:
max_len = seq_len
v_vector = sequence + [0] * (max_len - seq_len)
padded_vector = np.asarray(v_vector, dtype=dtype)
v_index = [1] * seq_len + [0] * (max_len - seq_len)
padded_index = np.asanyarray(v_index, dtype=dtype)
return padded_vector, padded_index
def add2count(value, map):
if value not in map:
map[value] = 0
map[value] += 1
import os
def get_dir_files(dirname):
L = []
for root, dirs, files in os.walk(dirname):
for file in files:
L.append(os.path.join(root, file))
return L
def multi_process(func, lst, num_cores=multiprocessing.cpu_count()):
workers = Parallel(n_jobs=num_cores, backend='multiprocessing')
output = workers(delayed(func)(one) for one in lst)
return output
def count_file(filename):
def blocks(files, size=65536):
while True:
b = files.read(size)
if not b: break
yield b
with open(filename, "r", encoding="utf-8", errors='ignore') as f:
num = (sum(bl.count("\n") for bl in blocks(f)))
return num
def lst2str(lst):
return ' '.join(list(map(str, lst)))
def str2lst(string):
return list(map(int, string.split()))
def reverse_map(maps):
return {v: k for k, v in maps.items()}