-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
179 lines (147 loc) · 4.92 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
"""
@Time : 2018/7/17 下午2:42
@FileName: utils.py
@author: 王炳宁
@contact: [email protected]
"""
import itertools
import multiprocessing
import pickle
import re
import numpy as np
from joblib import Parallel, delayed
from tqdm import tqdm
np.random.seed(10245)
def get_file_charset(filename):
import chardet
rawdata = open(filename, 'rb').read(1000)
result = chardet.detect(rawdata)
charenc = result['encoding']
return charenc
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 write_lst_to_file(lst, filename, encoding='utf-8'):
output = '\n'.join(lst)
with open(filename, 'w', encoding=encoding, errors='ignore') 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_model_parameters(model):
total = 0
for parameter in model.parameters():
if parameter.requires_grad:
tmp = 1
for a in parameter.size():
tmp *= a
total += tmp
return total
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 clean(txt):
txt = DBC2SBC(txt)
txt = txt.lower()
txt = re.sub('(\s*)?(<.*?>)?', '', txt)
return txt
def multi_process(func, lst, num_cores=multiprocessing.cpu_count(), backend='multiprocessing'):
workers = Parallel(n_jobs=num_cores, backend=backend)
output = workers(delayed(func)(one) for one in tqdm(lst))
# output = workers(delayed(func)(one) for one in lst)
return output
def get_file_info(filename):
with open(filename, encoding=get_file_charset(filename), errors='ignore') as f:
for line in f:
yield line
def evaluate_comqa(results, threshold=0.5):
precision = []
recall = []
f1 = []
accuracy = []
for one in results:
[pred, paras] = one
sample_a = 1.0e-9
sample_b = 1.0e-9
sample_c = 1.0e-9
num = 0
if len(pred) < len(paras):
pred.extend([0.0] * len(paras))
for p, para in zip(pred, paras):
r = para[1]
num += 1
if p > threshold:
sample_a += 1
if r == 1:
sample_b += 1
if p > threshold and r == 1:
sample_c += 1
sample_precision = sample_c / sample_a
sample_recall = sample_c / sample_b
if sample_precision >= 0.999 and sample_recall >= 0.999:
acc = 1
else:
acc = 0
sample_f1 = 2 * sample_precision * sample_recall / (sample_recall + sample_precision)
precision.append(sample_precision)
recall.append(sample_recall)
f1.append(sample_f1)
accuracy.append(acc)
precision = np.mean(precision)
recall = np.mean(recall)
f1 = np.mean(f1)
accuracy = np.mean(accuracy)
macro_f1 = 2 * precision * recall / (precision + recall)
return precision, recall, f1, macro_f1, accuracy