-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
292 lines (232 loc) · 8.33 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import logging
import torch
import re
from tabulate import tabulate
logger = logging.getLogger(__name__)
# Checked.
def initialize_logger(log_name, *args, **kwargs):
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(formatter)
fh = logging.FileHandler(
filename=f'logs/{log_name}'
, mode='a'
, encoding='UTF-8')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(sh)
logger.addHandler(fh)
return logger
# Checked.
def initialize_gpus(gpu_ids_str, *args, **kwargs):
if gpu_ids_str == None:
logger.error('There is no any given gpu.')
raise Exception('There is no any given gpu.')
if not torch.cuda.is_available():
logger.error('CUDA is not available.')
raise Exception('CUDA is not available.')
gpu_ids_str_list = gpu_ids_str.replace(' ', '').split(',')
gpu_ids_int_list = []
for gpu_id_str in gpu_ids_str_list:
gpu_ids_int_list.append(int(gpu_id_str))
logger.info('CUDA is available.')
logger.info('Initialize GPUs successfully.')
return gpu_ids_int_list
# Checked.
def initialize_batch_size(batch_size_str, *args, **kwargs):
if batch_size_str == None:
logger.warn(f'There is no given batch_size.')
logger.info('Set the batch_size to 1 to continue.')
batch_size_str = '1'
batch_size = int(batch_size_str)
return batch_size
# This function is used to preprocess input string (fact).
def process_string(
string
, adjust_chars=False
, process_fact=False
, translator=None):
# Remove special chars and unify punctuations.
if adjust_chars == True:
string = string.replace(' ', '')
string = string.replace('\\', '')
string = string.replace('`', '')
string = string.replace('#', '')
string = string.replace(',', ',')
string = string.replace(':', ':')
string = string.replace(';', ';')
string = string.replace('?', '?')
string = string.replace('!', '!')
string = string.replace('(', '(')
string = string.replace(')', ')')
# Remove unused part which is in string (fact).
if process_fact == True:
string = get_fact(string=string)
# Translate string (fact) to target language.
if translator != None:
string = translator.convert(string)
return string
# Remove unused part which is in string (fact).
def get_fact(string):
chinese_numbers = \
['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
paragraphs = []
record_index = 0
# According titles ('一、', '二、', ...) to separate paragraphs.
for index in range(len(string)):
if (index + 1) < len(string):
if string[index] == '。' and string[index+1] in chinese_numbers:
for temp_index in range((index+1), len(string)):
if string[temp_index] == '、':
paragraphs.append(string[record_index:(index+1)])
record_index = (index + 1)
if string[temp_index] not in chinese_numbers:
break
else:
paragraphs.append(string[record_index:])
# Remove the last paragraph (because last paragraph is unused content).
paragraphs = paragraphs[:-1]
# Remove the title ('一、', '二、', ...) of paragraphs.
for index in range(len(paragraphs)):
for temp_index in range(len(paragraphs[index])):
if paragraphs[index][temp_index] == '、':
paragraphs[index] = paragraphs[index][(temp_index+1):]
break
# Get all sentences that are in paragraphs.
all_sentences = []
for paragraph in paragraphs:
part_sentences = re.split(pattern=r'([,。])', string=paragraph)
for sentence in part_sentences:
all_sentences.append(sentence)
# Use 'Regular Expression' to remove the contents that are not fact.
for index, sentence in enumerate(iterable=all_sentences):
if re.match(
pattern=r'讵[^,:;?!()]*[不未][^,:;?!()]*[悔惕戒]'
, string=sentence) != None:
if (index + 1) >= len(all_sentences):
continue
if len(all_sentences[index+1]) > 1:
all_sentences = all_sentences[index+1:]
else:
all_sentences = all_sentences[index+2:]
# Save the result.
fact = ''
for sentence in all_sentences:
fact += sentence
return fact
# Get article and accusations table.
def get_tables(config, formatter, *args, **kwargs):
names = ['article', 'accusation']
paths = {
'article': config.get('data', 'articles_path')
, 'accusation': config.get('data', 'accusations_path')
}
tables = {'article': {}, 'accusation': {}}
for name in names:
items = []
with open(file=paths[name], mode='r', encoding='UTF-8') as file:
lines = file.readlines()
# Save article, article_source, accusation without '\n'
for index in range(len(lines)):
if lines[index][-1] == '\n':
items.append(lines[index][0:-1])
else:
items.append(lines[index])
file.close()
# Convert article, article_source, accusation to one-hot encoding form.
for item in items:
tables[name][item] = formatter({name: item})
return tables['article'], tables['accusation']
# Get the time string with 'minute:second' format.
def get_time_str(total_seconds):
total_seconds = int(total_seconds)
minutes = total_seconds // 60
seconds = total_seconds % 60
return ('%2d:%02d' % (minutes, seconds))
# Checked.
# Generate and print log.
def log_results(
epoch=None
, stage=None
, iterations=None
, time=None
, loss=None
, sum_loss=0
, cls_loss=0
, is_summarization=False
, learning_rate=None
, results=None):
if sum_loss == 0:
sum_loss = 'None'
else:
sum_loss = str(round(number=sum_loss, ndigits=7))
if cls_loss == 0:
cls_loss = 'None'
else:
cls_loss = str(round(number=cls_loss, ndigits=7))
header2item = {
'epoch': epoch
, 'stage': stage
, 'iterations': iterations
, 'time': time
, 'loss': loss
, 'sum_loss': sum_loss
, 'cls_loss': cls_loss
, 'is_summarization': is_summarization
, 'learning_rate': learning_rate
}
information_headers = []
information_table = [[]]
for header in header2item:
if header2item[header] != None:
information_headers.append(header)
information_table[0].append(header2item[header])
if isinstance(results, dict):
results_headers = [
'Type'
, 'MiP', 'MiR', 'MiF'
, 'MaP', 'MaR', 'MaF'
]
results_table = [
[
'article'
, results['article']['mip']
, results['article']['mir']
, results['article']['mif']
, results['article']['map']
, results['article']['mar']
, results['article']['maf']
]
, [
'accusation'
, results['accusation']['mip']
, results['accusation']['mir']
, results['accusation']['mif']
, results['accusation']['map']
, results['accusation']['mar']
, results['accusation']['maf']
]
]
elif isinstance(results, str):
results_headers = ['Message']
results_table = [[results]]
information = (
'\n'
+ tabulate(
tabular_data=information_table
, headers=information_headers
, tablefmt='pretty')
)
if results != '' and results != None:
information += (
'\n'
+ tabulate(
tabular_data=results_table
, headers=results_headers
, tablefmt='pretty')
)
logger.info(f'{information}\n')