-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_preprocessing.py
686 lines (480 loc) · 30.2 KB
/
data_preprocessing.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import os
import re
import tqdm
import glob
import json
import time
import inspect
import pandas as pd
from extract_source_text import make_sources
def sorted_list(path_list):
path_list = sorted(path_list, reverse=False)
path_list = sorted(path_list, key=len)
return path_list
def divide_source_file_list(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
def json_file_path_list(path_list):
json_file_path = [glob.glob(i, recursive = True) for i in path_list][0]
json_file_path = sorted_list(json_file_path)
return json_file_path
def train_valid_json_file_path_list(path_list):
train_json_file_path, valid_json_file_path = [glob.glob(i, recursive = True) if 'rain' in i
else glob.glob(i, recursive = True)
for i in path_list]
train_json_file_path = sorted_list(train_json_file_path)
valid_json_file_path = sorted_list(valid_json_file_path)
return train_json_file_path, valid_json_file_path
def txt_file_path_list(file_path, folder_corpus_type_path):
txt_file_path = [folder_corpus_type_path + str(i) + ".txt"
for i in range(len(file_path))]
return txt_file_path
def make_train_valid_json_txt_file_path_list(json_path_list, txt_path_list, corpus_name):
train_json_file_path, valid_json_file_path = train_valid_json_file_path_list(json_path_list)
the_number_of_train_json_file = len(train_json_file_path)
the_number_of_valid_json_file = len(valid_json_file_path)
the_number_of_json_file = the_number_of_train_json_file + the_number_of_valid_json_file
print("The number of train json file:", the_number_of_train_json_file)
print("The number of valid json file:", the_number_of_valid_json_file)
print("The number of json file:", the_number_of_json_file)
train_txt_file_path = txt_file_path_list(train_json_file_path, txt_path_list[0])
valid_txt_file_path = txt_file_path_list(valid_json_file_path, txt_path_list[1])
source_file_index_df = pd.DataFrame(train_json_file_path, columns=['source_file_path'])
source_file_index_df.to_excel("source_file_index/" + corpus_name + "_source_train_file_index.xlsx", index=False)
source_file_index_df = pd.DataFrame(valid_json_file_path, columns=['source_file_path'])
source_file_index_df.to_excel("source_file_index/" + corpus_name + "_source_valid_file_index.xlsx", index=False)
return train_json_file_path, valid_json_file_path, train_txt_file_path, valid_txt_file_path
def make_json_txt_file_path_list(json_path_list, txt_path_list, corpus_name):
json_file_path = json_file_path_list(json_path_list)
the_number_of_json_file = len(json_file_path)
print("The number of json file:", the_number_of_json_file)
text_file_path_list = txt_file_path_list(json_file_path, txt_path_list[0])
source_file_index_df = pd.DataFrame(json_file_path, columns=['source_file_path'])
source_file_index_df.to_excel("source_file_index/" + corpus_name + "_source_file_index.xlsx", index=False)
return json_file_path, text_file_path_list
def make_topic_json_txt_file_path_list(json_folder_list, topic_name_list, txt_path_list, corpus_name):
# summary_and_report_generation_data
# web_data_based_korean_corpus_data
json_path_list = []
train_topic_json_list = []
valid_topic_json_list = []
the_number_of_train_file = 0
the_number_of_valid_file = 0
the_number_of_file = 0
the_numer_of_txt_file = 0
for json_folder in json_folder_list:
for topic_name in topic_name_list:
json_path_list.append(json_folder + topic_name + '/**/*.json')
for i in range(len(json_path_list)):
json_path = json_path_list[i]
if 'train'[1:] in json_path:
train_file_path = glob.glob(json_path, recursive = True)
the_number_of_train_topic_json_file = len(train_file_path)
the_number_of_train_file += len(train_file_path)
the_number_of_file += len(train_file_path)
if i < 9:
topic_index = "0" + str(i + 1)
elif i >= 9:
topic_index = str(i + 1)
# train_file_list_variable = "train_json_file_path_list_" + topic_index
# txt_file_path_list_variable = "train_txt_file_path_list_" + topic_index
train_topic_json_list.append(the_number_of_train_topic_json_file)
# globals()[train_file_list_variable] = train_file_path
# globals()[txt_file_path_list_variable] = txt_file_path_list(train_file_path,
# txt_path_list[0] + topic_name_list[i] + "_train_")
train_txt_file_path = txt_file_path_list(train_file_path,
txt_path_list[0] + topic_name_list[i] + "_train_")
source_file_index_df = pd.DataFrame({'source_file_path': train_file_path,
'txt_file_path': train_txt_file_path})
source_file_index_df.to_excel("source_file_index/" + corpus_name + "_train" + "_source_file_index.xlsx", index=False)
elif 'valid'[1:] in json_path:
valid_file_path = glob.glob(json_path, recursive = True)
the_number_of_valid_topic_json_file = len(valid_file_path)
the_number_of_valid_file += len(valid_file_path)
the_number_of_file += len(valid_file_path)
if '요약문 및 레포트 생성 데이터' in json_path:
if i - 10 < 9:
topic_index = "0" + str(i + 1 - 10)
elif i - 10>= 9:
topic_index = str(i + 1 - 10)
elif '웹데이터 기반 한국어 말뭉치 데이터' in json_path:
if i - 17 < 9:
topic_index = "0" + str(i + 1 - 17)
elif i - 17 >= 9:
topic_index = str(i + 1 - 17)
# valid_file_list_variable = "valid_json_file_path_list_" + topic_index
# txt_file_path_list_variable = "valid_txt_file_path_list_" + topic_index
valid_topic_json_list.append(the_number_of_valid_topic_json_file)
# globals()[train_file_list_variable] = valid_file_path
# globals()[txt_file_path_list_variable] = txt_file_path_list(valid_file_path,
# txt_path_list[1] + topic_name_list[i - (len(json_path_list)) // 2] + "_valid_")
valid_txt_file_path = txt_file_path_list(valid_file_path,
txt_path_list[0] + topic_name_list[i] + "_valid_")
source_file_index_df = pd.DataFrame({'source_file_path': valid_file_path,
'txt_file_path': valid_txt_file_path})
source_file_index_df.to_excel("source_file_index/" + corpus_name + "_valid" + "_source_file_index.xlsx", index=False)
the_number_of_topic_json_file_df = pd.DataFrame({'Topic':topic_name_list,
'The number of train topic json file':train_topic_json_list,
'The number of valid topic json file':valid_topic_json_list})
the_number_of_topic_json_file_df.loc['Total', : ] = the_number_of_topic_json_file_df.loc[topic_name_list[0]:topic_name_list[-1], :].sum(axis=0)
the_number_of_topic_json_file_df.loc[:, 'Total'] = the_number_of_topic_json_file_df.loc[:, 'The number of train topic json file':'The number of valid topic json file'].sum(axis=1)
print()
print("The number of train json file: ", the_number_of_train_file)
print("The number of valid json file: ", the_number_of_valid_file)
print("The number of json file: ", the_number_of_file)
return the_number_of_topic_json_file_df
def post_txt_file_path_list(corpus_path_list):
post_corpus_path_list = [corpus_file.replace("pro", "post")
for corpus_file in corpus_path_list]
return post_corpus_path_list
def make_pro_post_txt_file_path_list(pro_corpus_path, pro_post_xlsx_path):
pro_total_corpus_path_list = glob.glob(pro_corpus_path)
pro_total_corpus_path_list = sorted_list(pro_total_corpus_path_list)
post_total_corpus_path_list = post_txt_file_path_list(pro_total_corpus_path_list)
pro_post_total_corpus_path_df = pd.DataFrame({'Pro': pro_total_corpus_path_list, 'Post': post_total_corpus_path_list})
pro_post_total_corpus_path_df.to_excel(pro_post_xlsx_path, index=False)
return pro_total_corpus_path_list, post_total_corpus_path_list
def make_topic_pro_post_txt_file_name_list(pro_corpus_name, topic_name_list):
# summary_and_report_generation_data
# web_data_based_korean_corpus_data
for i in range(len(topic_name_list)):
pro_total_corpus_path_list = sorted_list(glob.glob(pro_corpus_name + topic_name_list[i] + "_train" + "*.txt"))
post_total_corpus_path_list = post_txt_file_path_list(pro_total_corpus_path_list)
if i < 9:
topic_index = "0" + str(i + 1)
elif i >= 9:
topic_index = str(i + 1)
# pro_total_corpus_path_list_variable = "pro_total_corpus_path_list_" + "train_" + topic_index
# post_total_corpus_path_list_variable = "post_total_corpus_path_list_" + "train_" + topic_index
# globals()[pro_total_corpus_path_list_variable] = pro_total_corpus_path_list
# globals()[post_total_corpus_path_list_variable] = post_total_corpus_path_list
if 'summary_and_report_generation_data' in pro_corpus_name:
pro_post_total_corpus_path_df = pd.DataFrame({'Pro': pro_total_corpus_path_list, 'Post': post_total_corpus_path_list})
pro_post_xlsx_path = "pro_post_corpus_path/summary_and_report_generation_data_" + "train_" + topic_index + ".xlsx"
pro_post_total_corpus_path_df.to_excel(pro_post_xlsx_path, index=False)
if 'web_data_based_korean_corpus_data' in pro_corpus_name:
pro_post_total_corpus_path_df = pd.DataFrame({'Pro': pro_total_corpus_path_list, 'Post': post_total_corpus_path_list})
pro_post_xlsx_path = "pro_post_corpus_path/web_data_based_korean_corpus_data_" + "train_" + topic_index + ".xlsx"
pro_post_total_corpus_path_df.to_excel(pro_post_xlsx_path, index=False)
pro_total_corpus_path_list = sorted_list(glob.glob(pro_corpus_name + topic_name_list[i] + "_valid" + "*.txt"))
post_total_corpus_path_list = post_txt_file_path_list(pro_total_corpus_path_list)
if 'summary_and_report_generation_data' in pro_corpus_name:
if i - 10 < 9:
topic_index = "0" + str(i + 1 - 10)
elif i - 10 >= 9:
topic_index = str(i + 1 - 10)
elif 'web_data_based_korean_corpus_data' in pro_corpus_name:
if i - 17 < 9:
topic_index = "0" + str(i + 1 - 17)
elif i - 17 >= 9:
topic_index = str(i + 1 - 17)
# pro_total_corpus_path_list_variable = "pro_total_corpus_path_list_" + "valid_" + topic_index
# post_total_corpus_path_list_variable = "post_total_corpus_path_list_" + "valid_" + topic_index
# globals()[pro_total_corpus_path_list_variable] = pro_total_corpus_path_list
# globals()[post_total_corpus_path_list_variable] = post_total_corpus_path_list
if 'summary_and_report_generation_data' in pro_corpus_name:
pro_post_total_corpus_path_df = pd.DataFrame({'Pro': pro_total_corpus_path_list, 'Post': post_total_corpus_path_list})
pro_post_xlsx_path = "pro_post_corpus_path/summary_and_report_generation_data_" + "valid_" + topic_index + ".xlsx"
pro_post_total_corpus_path_df.to_excel(pro_post_xlsx_path, index=False)
if 'web_data_based_korean_corpus_data' in pro_corpus_name:
pro_post_total_corpus_path_df = pd.DataFrame({'Pro': pro_total_corpus_path_list, 'Post': post_total_corpus_path_list})
pro_post_xlsx_path = "pro_post_corpus_path/web_data_based_korean_corpus_data_" + "valid_" + topic_index + ".xlsx"
pro_post_total_corpus_path_df.to_excel(pro_post_xlsx_path, index=False)
def merge_and_deduplicate_corpus_txt(preprocessing_corpus_path, merge_corpus_path,
deduplicate_corpus_path):
corpus_list = glob.glob(preprocessing_corpus_path)
corpus_list = sorted_list(corpus_list)
with open(merge_corpus_path, 'w', encoding='utf-8') as f:
for corpus in corpus_list:
with open(corpus, encoding='utf-8') as text:
for line in text:
f.write(line)
with open(deduplicate_corpus_path, 'w', encoding='utf-8') as f1:
with open(merge_corpus_path, encoding='utf-8') as f2:
lines = f2.read().splitlines()
single_sentence_dict = dict.fromkeys(lines)
single_sentence_list = list(single_sentence_dict)
f1.write("\n".join(single_sentence_list))
def merge_and_deduplicate_topic_corpus_txt(preprocessing_corpus_path, merge_corpus_path,
deduplicate_corpus_path, topic_name_list):
# summary_and_report_generation_data
# web_data_based_korean_corpus_data
corpus_list = glob(preprocessing_corpus_path)
corpus_list = sorted_list(corpus_list)
merge_corpus_list = glob(merge_corpus_path + "*.txt")
for i in range(len(topic_name_list)):
with open(merge_corpus_path + topic_name_list[i] + '.txt', 'w', encoding='utf-8') as f:
topic_corpus_list_with_none = [j if topic_name_list[i] in j else None for j in corpus_list]
topic_corpus_list = list(filter(None, topic_corpus_list))
for corpus in topic_corpus_list:
with open(corpus, encoding='utf-8') as text:
for line in text:
f.write(line)
with open(deduplicate_corpus_path, 'w', encoding='utf-8') as f1:
for corpus in merge_corpus_list:
with open(corpus, encoding='utf-8') as f2:
lines = f2.read().splitlines()
single_sentence_dict = dict.fromkeys(lines)
single_sentence_list = list(single_sentence_dict)
f1.write("\n".join(single_sentence_list))
def count_number_of_txt_file_with_batch_list(source_file_list, batch_size, corpus_name):
corpus_type1 = ["summary_and_report_generation_data",
"web_data_based_korean_corpus_data"]
corpus_type2 = ["legal_regulations_(such_as_terms_and_conditions_of_judgment)_text_analysis_data",
"summary_of_book_materials"]
corpus_type3 = ['automatic_patent_classification',
'british_korean_data_on_patents_in_major_countries_linked_to_industrial_information',
'machine_reading_data_for_administrative_documents',
'professional_corpus',
'summary_of_thesis_materials',
'document_summary_text', 'general_common_sense', 'machine_reading',
'news_article_machine_reading_data', 'reading_books_by_machine',
'korean_corpus_data_based_on_large_scale_purchase_books']
source_file_by_batch_df = pd.DataFrame({'File':[0], 'Length of Source List':[0],
'The Number of TXT File':[0],
'Description':[0]})
if any(corpus in corpus_name for corpus in corpus_type1):
the_number_of_total_txt_file = 0
the_number_of_txt_file_list = []
source_list = []
frame = inspect.currentframe()
frame = inspect.getouterframes(frame)[1]
string = inspect.getframeinfo(frame[0]).code_context[0].strip()
name = string[string.find('(') + 1:-1].split(',')[0]
file_name_number = re.findall(r'\d+', name)[0]
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
sources = make_sources(one_json_sample)
for source in sources:
source_list.append(source[0])
the_number_of_txt_file = 1
if len(source_list) >= batch_size:
source_file_by_batch_df.loc[i] = [source_file,
len(source_list), the_number_of_txt_file, ""]
elif len(source_list) < batch_size:
source_file_by_batch_df.loc[i] = [source_file,
len(source_list), the_number_of_txt_file,
"not subject of batch. small source list."]
the_number_of_txt_file_list.append(len(source_list))
the_number_of_total_txt_file += len(source_list)
source_list = []
the_number_of_total_txt_file = the_number_of_total_txt_file // batch_size
print("Batch Size:", batch_size)
print("The number of txt file:", the_number_of_total_txt_file)
source_file_by_batch_df = source_file_by_batch_df.astype({'Length of Source List':'int',
'The Number of txt File':'int'})
if 'rain' in source_file:
train_file = "source_file_by_batch/summary_and_report_generation_data_train_" + file_name_number + ".xlsx"
source_file_by_batch_df.to_excel(train_file, index=False)
elif 'alid' in source_file:
valid_file = "source_file_by_batch/summary_and_report_generation_data_valid_" + file_name_number + ".xlsx"
source_file_by_batch_df.to_excel(valid_file, index=False)
else:
plain_file = "source_file_by_batch/summary_and_report_generation_data_" + file_name_number + ".xlsx"
source_file_by_batch_df.to_excel(plain_file, index=False)
elif any(corpus in corpus_name for corpus in corpus_type2):
the_number_of_total_txt_file = 0
the_number_of_txt_file_list = []
source_list = []
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
sources = make_sources(source_file, one_json_sample)
for source in sources:
source_list.append(source[0])
the_number_of_txt_file = 1
if len(source_list) >= batch_size:
source_file_by_batch_df.loc[i] = [source_file,
len(source_list), the_number_of_txt_file, ""]
elif len(source_list) < batch_size:
source_file_by_batch_df.loc[i] = [source_file,
len(source_list), the_number_of_txt_file,
"not subject of batch. small source list."]
the_number_of_txt_file_list.append(len(source_list))
the_number_of_total_txt_file += len(source_list)
source_list = []
the_number_of_total_txt_file = the_number_of_total_txt_file // batch_size
source_file_by_batch_df = source_file_by_batch_df.astype({'Length of Source List':'int',
'The Number of txt File':'int'})
print("Batch Size:", batch_size)
print("The number of txt file:", the_number_of_total_txt_file)
if 'rain' in source_file:
source_file_by_batch_df.to_excel("source_file_by_batch/" + corpus_name + "_train.xlsx", index=False)
elif 'alid' in source_file:
source_file_by_batch_df.to_excel("source_file_by_batch/" + corpus_name + "_valid.xlsx", index=False)
else:
source_file_by_batch_df.to_excel("source_file_by_batch/" + corpus_name + ".xlsx", index=False)
elif any(corpus in corpus_name for corpus in corpus_type3):
the_number_of_total_txt_file = 0
the_number_of_txt_file_list = []
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
try:
source_list = make_sources(one_json_sample)
the_number_of_txt_file = ((len(source_list) // batch_size) + 1)
if len(source_list) >= batch_size:
source_file_by_batch_df.loc[i] = [source_file,
len(source_list), the_number_of_txt_file, ""]
the_number_of_txt_file_list.append(the_number_of_txt_file)
the_number_of_total_txt_file += the_number_of_txt_file
elif len(source_list) < batch_size:
source_file_by_batch_df.loc[i] = [source_file,
len(source_list), the_number_of_txt_file,
"not subject of batch. small source list."]
the_number_of_txt_file_list.append(1)
the_number_of_total_txt_file += 1
except:
pass
print("Batch Size:", batch_size)
print("The number of txt file:", the_number_of_total_txt_file)
if 'rain' in source_file:
source_file_by_batch_df.to_excel("source_file_by_batch/" + corpus_name + "_train.xlsx", index=False)
elif 'alid' in source_file:
source_file_by_batch_df.to_excel("source_file_by_batch/" + corpus_name + "_valid.xlsx", index=False)
else:
source_file_by_batch_df.to_excel("source_file_by_batch/" + corpus_name + ".xlsx", index=False)
return the_number_of_total_txt_file, the_number_of_txt_file_list
def write_jsontext_to_txt_file_with_batch_list(source_file_list, text_file_path_list,
batch_size, the_number_of_txt_file_list, corpus_name):
corpus_type1 = ['automatic_patent_classification_data',
'british_korean_data_on_patents_in_major_countries_linked_to_industrial_information',
'document_summary_text',
'korean_corpus_data_based_on_large_scale_purchase_books',
'machine_reading',
'news_article_machine_reading_data',
'reading_books_by_machine',
'summary_of_thesis_materials',
'web_data_based_korean_corpus_data']
corpus_type2 = ['legal_regulations_(such_as_terms_and_conditions_of_judgment)_text_analysis_data',
'summary_and_report_generation_data',
'summary_of_book_materials']
corpus_type3 = ['general_common_sense']
corpus_type4 = ['machine_reading_data_for_administrative_documents']
corpus_type5 = ['professional_corpus']
progress_length = sum(the_number_of_txt_file_list)
print("[Size]")
print("The number of preprocessing corpus: " + str(progress_length))
print("\n[Order]")
pbar = tqdm(range(progress_length))
num = 0
if any(corpus in corpus_name for corpus in corpus_type1):
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
try:
source_list = make_sources(one_json_sample)
n = batch_size
source_batch_list = list(divide_source_file_list(source_list, n))
for source_list in source_batch_list:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
except:
pass
elif any(corpus in corpus_name for corpus in corpus_type2):
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
sources = make_sources(source_file, one_json_sample)
if len(source_list) >= batch_size:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
source_list = []
elif i == (len(source_file_list) -1):
for source in sources:
source_list.append(source)
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list[0]))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
for source in sources:
source_list.append(source[0])
elif any(corpus in corpus_name for corpus in corpus_type3):
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
if 'ko_wiki_v1_squad' in source_file:
source_list = make_sources(source_file, one_json_sample)
n = batch_size
source_batch_list = list(divide_source_file_list(source_list, n))
for source_list in source_batch_list:
num += 1
print(str(num), end=" ")
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
else:
source_list = make_sources(source_file, one_json_sample)
n = batch_size
source_batch_list = list(divide_source_file_list(source_list, n))
for source_list in source_batch_list:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
elif any(corpus in corpus_name for corpus in corpus_type4):
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
source_list = make_sources(one_json_sample)
n = batch_size
source_batch_list = list(divide_source_file_list(source_list, n))
for source_list in source_batch_list:
if 'tableqa' in source_file:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + "_tableqa.txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
else:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
elif any(corpus in corpus_name for corpus in corpus_type5):
for i in range(len(source_file_list)):
source_file = source_file_list[i]
with open(source_file, 'r', encoding='utf-8') as one_json_file:
one_json_sample = json.load(one_json_file)
source_list = make_sources(source_file, one_json_sample)
n = batch_size
source_batch_list = list(divide_source_file_list(source_list, n))
for source_list in source_batch_list:
num += 1
print(str(num), end=" ")
if '법령' in source_file or '판례' in source_file:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + "_legal_text.txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
else:
with open(os.path.join('AIHUB_corpus/' + text_file_path_list[i][:-4] + "_" + str(num) + ".txt"), "a", encoding='utf-8') as fp:
fp.write("\n".join(source_list))
num += 1
pbar.n += 1
pbar.refresh()
time.sleep(0.01)
pbar.close()