/
preprocess.py
687 lines (575 loc) · 21.3 KB
/
preprocess.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
687
# coding = utf-8
import argparse
import logging
import multiprocessing as mp
import os
import re
from functools import partial
from pyltp import Segmentor, Postagger
import jieba
import jieba.posseg as pseg
import pandas as pd
from gensim.models import Word2Vec
from metrics.rouge import RougeL
from utils.serialization import *
ltp_seg = Segmentor()
ltp_pos = Postagger()
def trans_to_df(raw_data_path):
"""
transfer json file to dataframe
:param raw_data_path: path of raw data of json file
:return: article_df, qa_df
"""
data = read_json(raw_data_path)
questions = []
articles = []
for dc in data:
temp = [dc['article_id'], dc['article_type'], dc['article_title'], dc['article_content']]
articles.append(temp)
for items in dc['questions']:
r = [dc['article_id']]
r = r + list(items.values())
questions.append(r)
article_columns = ['article_id', 'article_type', 'article_title', 'article_content']
if 'answer' in data[0]['questions'][0]:
question_columns = ['article_id', 'question_id', 'question', 'answer', 'question_type']
else:
question_columns = ['article_id', 'question_id', 'question']
article_df = pd.DataFrame(data=articles, columns=article_columns)
qa_df = pd.DataFrame(data=questions, columns=question_columns)
qa_df.fillna('', inplace=True)
return article_df, qa_df
def clean_text(article_df: pd.DataFrame, qa_df: pd.DataFrame):
"""
remove \u3000, swap \r\n with \001, swap \t with ' ',
change numbers to half written
:param df:
:return: cleaned df
"""
def clean(row):
row = re.sub('[\u3000\t]', ' ', row)
row = re.sub('\s{2,}', '', row)
# row = re.sub('[“”]', '', row)
row = re.sub('[\r\n]', ' ', row)
# p_l = re.compile(r'\s+([\u4e00-\u9fa5, ]{1})')
# p_r = re.compile(r'([\u4e00-\u9fa5, ]{1})\s+')
# row = p_l.sub('\1', row)
# row = p_r.sub('\1', row)
row = re.sub(r'0', '0', row)
row = re.sub(r'1', '1', row)
row = re.sub(r'2', '2', row)
row = re.sub(r'3', '3', row)
row = re.sub(r'4', '4', row)
row = re.sub(r'5', '5', row)
row = re.sub(r'6', '6', row)
row = re.sub(r'7', '7', row)
row = re.sub(r'8', '8', row)
row = re.sub(r'9', '9', row)
row = re.sub(r'.', '.', row)
row = re.sub(r'a', 'a', row)
row = re.sub(r'b', 'b', row)
row = re.sub(r'c', 'c', row)
row = re.sub(r'd', 'd', row)
row = re.sub(r'e', 'e', row)
row = re.sub(r'f', 'f', row)
row = re.sub(r'g', 'g', row)
row = re.sub(r'h', 'h', row)
row = re.sub(r'i', 'i', row)
row = re.sub(r'j', 'j', row)
row = re.sub(r'k', 'k', row)
row = re.sub(r'l', 'l', row)
row = re.sub(r'm', 'm', row)
row = re.sub(r'n', 'n', row)
row = re.sub(r'o', 'o', row)
row = re.sub(r'p', 'p', row)
row = re.sub(r'q', 'q', row)
row = re.sub(r'r', 'r', row)
row = re.sub(r's', 's', row)
row = re.sub(r't', 't', row)
row = re.sub(r'u', 'u', row)
row = re.sub(r'v', 'v', row)
row = re.sub(r'w', 'w', row)
row = re.sub(r'x', 'x', row)
row = re.sub(r'y', 'y', row)
row = re.sub(r'z', 'z', row)
row = re.sub(r'A', 'A', row)
row = re.sub(r'B', 'B', row)
row = re.sub(r'C', 'C', row)
row = re.sub(r'D', 'D', row)
row = re.sub(r'E', 'E', row)
row = re.sub(r'F', 'F', row)
row = re.sub(r'G', 'G', row)
row = re.sub(r'H', 'H', row)
row = re.sub(r'I', 'I', row)
row = re.sub(r'J', 'J', row)
row = re.sub(r'K', 'K', row)
row = re.sub(r'L', 'L', row)
row = re.sub(r'M', 'M', row)
row = re.sub(r'N', 'N', row)
row = re.sub(r'O', 'O', row)
row = re.sub(r'P', 'P', row)
row = re.sub(r'Q', 'Q', row)
row = re.sub(r'R', 'R', row)
row = re.sub(r'S', 'S', row)
row = re.sub(r'T', 'T', row)
row = re.sub(r'U', 'U', row)
row = re.sub(r'V', 'V', row)
row = re.sub(r'W', 'W', row)
row = re.sub(r'X', 'X', row)
row = re.sub(r'Y', 'Y', row)
row = re.sub(r'Z', 'Z', row)
if len(row) > 0 and row[-1] == '。':
row = row[:-1].strip()
return row
def merge(row):
"""
merge the article title and content
:param row:
:return:
"""
row['article'] = row['article_title'] + '。' + row['article_content']
return row
article_df['article_title'] = article_df['article_title'].apply(clean)
article_df['article_content'] = article_df['article_content'].apply(clean)
article_df = article_df.apply(merge, axis=1)
article_df.drop(['article_title', 'article_content'], axis=1, inplace=True)
qa_df['question'] = qa_df['question'].apply(clean)
if 'answer' in qa_df.columns:
qa_df['answer'] = qa_df['answer'].apply(clean)
qa_df['answer'] = qa_df['answer'].apply(str.strip)
answers = qa_df[qa_df['answer'] != '']['answer'].values
drop_list = ['。', ',', '、', ';', ':', '?', '!', ' ', '.', '?', '!', ';', ':', ',', '-', '...', '..', '....']
answers = [answer[:-1].strip() if answer[-1] in drop_list else answer for answer in answers]
answers = [answer[1:].strip() if answer[0] in drop_list else answer for answer in answers]
qa_df.loc[qa_df['answer'] != '', 'answer'] = answers
return article_df, qa_df
def _apply_cut_jieba(df, col):
def _cut_jieba(row):
"""
cut the sentences into tokens
:param row:
:return:
"""
cut_words = []
cut_flags = []
if '。' in row:
row = row.split('。')
for idx, s in enumerate(row):
if idx != len(row) - 1:
s = s + '。'
s_cut = list(pseg.lcut(s, HMM=False))
cut_words.extend([c.word for c in s_cut])
cut_flags.extend([c.flag for c in s_cut])
else:
s_cut = list(pseg.lcut(row, HMM=False))
cut_words = [c.word for c in s_cut]
cut_flags = [c.flag for c in s_cut]
new_row = pd.Series()
new_row['tokens'] = cut_words
new_row['flags'] = cut_flags
return new_row
sentence_cut = df[col].apply(_cut_jieba)
return sentence_cut
def _apply_cut_pyltp(df, col):
def _cut_pyltp(row):
"""
cut the sentences into tokens
:param row:
:return:
"""
cut_words = []
cut_flags = []
if '。' in row:
row = row.split('。')
for idx, s in enumerate(row):
if idx != len(row) - 1:
s = s + '。'
tokens = list(ltp_seg.segment(s))
cut_words.extend(tokens)
cut_flags.extend(list(ltp_pos.postag(tokens)))
else:
tokens = list(ltp_seg.segment(row))
cut_words = tokens
cut_flags = list(ltp_pos.postag(tokens))
new_row = pd.Series()
new_row['tokens'] = cut_words
new_row['flags'] = cut_flags
return new_row
sentence_cut = df[col].apply(_cut_pyltp)
return sentence_cut
def parallel_cut(df, col, method):
n_cpu = mp.cpu_count()
with mp.Pool(processes=n_cpu) as p:
split_dfs = np.array_split(df, n_cpu)
if method == 'jieba':
pool_results = p.map(partial(_apply_cut_jieba, col=col), split_dfs)
elif method == 'pyltp':
pool_results = p.map(partial(_apply_cut_pyltp, col=col), split_dfs)
else:
pool_results = p.map(partial(_apply_cut_jieba, col=col), split_dfs)
# merging parts processed by different processes
res = pd.concat(pool_results, axis=0)
return res
def clean_token(article_df: pd.DataFrame, qa_df: pd.DataFrame):
"""
clean data on token level
:param article_df:
:param qa_df:
:return:
"""
def clean(row, token_col, flag_col):
tokens_cleaned = []
flags_cleaned = []
for token, flag in zip(row[token_col], row[flag_col]):
token = token.strip()
if token != '':
tokens_cleaned.append(token)
flags_cleaned.append(flag)
row[token_col] = tokens_cleaned
row[flag_col] = flags_cleaned
return row
article_df = article_df.apply(lambda row: clean(row, 'article_tokens', 'article_flags'), axis=1)
qa_df = qa_df.apply(lambda row: clean(row, 'question_tokens', 'question_flags'), axis=1)
if 'answer_tokens' in qa_df.columns:
qa_df = qa_df.apply(lambda row: clean(row, 'answer_tokens', 'answer_flags'), axis=1)
return article_df, qa_df
def _apply_sample_article(df: pd.DataFrame, article_tokens_col, article_flags_col,
question_tokens_col,
max_token_len=500):
def _sample_article(row, article_tokens_col, article_flags_col, question_tokens_col, max_token_len=500):
"""
:param row:
:param article_tokens_col:
:param article_flags_col:
:param question_tokens_col:
:param max_token_len:
:return:
"""
article_tokens = row[article_tokens_col]
article_flags = row[article_flags_col]
question_tokens = row[question_tokens_col]
if len(article_tokens) <= max_token_len:
return row
sentences, sentences_f = [], []
cur_s, cur_s_f = [], []
question = ''.join(question_tokens)
cand, cand_f = [], []
rl = RougeL()
for idx, (token, flag) in enumerate(zip(article_tokens, article_flags)):
cur_s.append(token)
cur_s_f.append(flag)
if token in '。' or idx == len(article_tokens) - 1:
if len(cur_s) >= 2:
sentences.append(cur_s)
sentences_f.append(cur_s_f)
cur_s_str = ''.join(cur_s)
rl.add_inst(cur_s_str, question)
if rl.p_scores[-1] == 1.0:
rl.r_scores[-1] = 1.0
cur_s, cur_s_f = [], []
continue
if '。' not in ''.join(article_tokens):
row[article_tokens_col] = sentences[0]
row[article_flags_col] = sentences_f[0]
return row
scores = rl.r_scores
s_rank = np.zeros(len(sentences))
arg_sorted = list(reversed(np.argsort(scores)))
for i in range(10):
if i >= len(sentences):
break
pos = arg_sorted[i]
score = scores[pos]
if pos in [0, 1, len(sentences) - 1, len(sentences) - 2] or score == 0:
continue
block_scores = np.array([0.5 * score, 0.9 * score, score, score, 0.9 * score, 0.5 * score, 0.4 * score])
# block_scores = np.array([0.25*score, 0.5*score, score, 0.8*score, 0.64*score, 0.512*score, 0.4096*score])
block = s_rank[pos - 2: pos + 5]
block_scores = block_scores[:len(block)]
block_scores = np.max(np.stack([block_scores, block]), axis=0)
s_rank[pos - 2: pos + 5] = block_scores
rank = list(reversed(np.argsort(s_rank)))
flag = [0 for i in range(len(sentences))]
flag[0], flag[1], flag[-1], flag[-2] = 1, 1, 1, 1
cur_len = len(sentences[0]) + len(sentences[1]) + len(sentences[-1]) + len(sentences[-2])
for pos in rank:
if cur_len < max_token_len:
if s_rank[pos] > 0:
flag[pos] = 1
cur_len += len(sentences[pos])
else:
break
for i in range(len(flag)):
if flag[i] != 0:
cand.extend(sentences[i])
cand_f.extend(sentences_f[i])
row[article_tokens_col] = cand[:max_token_len]
row[article_flags_col] = cand_f[:max_token_len]
return row
df = df.apply(
lambda row: _sample_article(row, article_tokens_col, article_flags_col, question_tokens_col, max_token_len),
axis=1)
return df
def parallel_sample_article(article_df: pd.DataFrame, qa_df: pd.DataFrame, max_token_len=500):
sample_df = pd.merge(article_df, qa_df, how='inner', on=['article_id'])
n_cpu = mp.cpu_count()
with mp.Pool(processes=n_cpu) as p:
split_dfs = np.array_split(sample_df, n_cpu)
pool_results = p.map(partial(_apply_sample_article,
article_tokens_col='article_tokens',
article_flags_col='article_flags',
question_tokens_col='question_tokens',
max_token_len=max_token_len), split_dfs)
# merging parts processed by different processes
res = pd.concat(pool_results, axis=0)
return res
def _apply_find_gold_span(sample_df: pd.DataFrame, article_tokens_col, question_tokens_col, answer_tokens_col):
def _find_golden_span(row, article_tokens_col, question_tokens_col, answer_tokens_col):
article_tokens = row[article_tokens_col]
question_tokens = row[question_tokens_col]
answer_tokens = row[answer_tokens_col]
row['answer_token_start'] = -1
row['answer_token_end'] = -1
row['delta_token_starts'] = []
row['delta_token_ends'] = []
row['delta_rouges'] = []
row['max_rouge'] = 0
rl = RougeL()
rl_q = RougeL()
ground_ans = ''.join(answer_tokens).strip()
questrin_str = ''.join(question_tokens).strip()
len_p = len(article_tokens)
len_a = len(answer_tokens)
s2 = set(ground_ans)
star_spans = []
end_spans = []
rl_q_idx = []
for i in range(len_p - len_a + 1):
for t_len in range(len_a - 2, len_a + 3):
if t_len <= 0 or i + t_len > len_p:
continue
cand_ans = ''.join(article_tokens[i:i + t_len]).strip()
s1 = set(cand_ans)
mlen = max(len(s1), len(s2))
iou = len(s1.intersection(s2)) / mlen if mlen != 0 else 0.0
if iou >= 0.2:
rl.add_inst(cand_ans, ground_ans)
if rl.inst_scores[-1] == 1.0:
s = max(i - 7, 0)
cand_ctx = ''.join(article_tokens[s:i + t_len + 3]).strip()
rl_q.add_inst(cand_ctx, questrin_str)
rl_q_idx.append(len(star_spans))
star_spans.append(i)
end_spans.append(i + t_len - 1)
if len(star_spans) == 0:
return row
else:
max_score = np.max(rl.inst_scores)
row['max_rouge'] = max_score
if max_score == 1:
best_idx = rl_q_idx[int(np.argmax(rl_q.r_scores))]
else:
best_idx = np.argmax(rl.inst_scores)
if best_idx is not None:
row['answer_token_start'] = star_spans[best_idx]
row['answer_token_end'] = end_spans[best_idx]
row['delta_token_starts'] = star_spans
row['delta_token_ends'] = end_spans
row['delta_rouges'] = rl.inst_scores
return row
sample_df = sample_df.apply(
lambda row: _find_golden_span(row,
article_tokens_col,
question_tokens_col,
answer_tokens_col),
axis=1)
return sample_df
def parallel_find_gold_span(sample_df: pd.DataFrame):
n_cpu = mp.cpu_count()
with mp.Pool(processes=n_cpu) as p:
split_dfs = np.array_split(sample_df, n_cpu)
pool_results = p.map(partial(_apply_find_gold_span,
article_tokens_col='article_tokens',
question_tokens_col='question_tokens',
answer_tokens_col='answer_tokens'),
split_dfs)
# merging parts processed by different processes
res = pd.concat(pool_results, axis=0)
return res
def process_dataset(args, raw_file_path):
if args.method == 'jieba':
if osp.isfile(args.jieba_big_dict):
jieba.set_dictionary(args.jieba_big_dict)
jieba.del_word('日电')
jieba.del_word('日刊')
jieba.del_word('亿美元')
jieba.del_word('英国伦敦')
jieba.setLogLevel(logging.INFO)
elif args.method == 'pyltp':
ltp_seg.load(args.ltp_cws_path)
ltp_pos.load(args.ltp_pos_path)
# load and clean
adf, qadf = trans_to_df(raw_file_path)
adf, qadf = clean_text(adf, qadf)
# cut words
article_cut = parallel_cut(adf, 'article', args.method)
adf.drop(['article'], axis=1, inplace=True)
adf['article_tokens'] = article_cut['tokens']
adf['article_flags'] = article_cut['flags']
question_cut = parallel_cut(qadf, 'question', args.method)
qadf.drop(['question'], axis=1, inplace=True)
qadf['question_tokens'] = question_cut['tokens']
qadf['question_flags'] = question_cut['flags']
if 'answer' in qadf.columns and not args.test:
ans_cut = parallel_cut(qadf, 'answer', args.method)
qadf['answer_tokens'] = ans_cut['tokens']
qadf['answer_flags'] = ans_cut['flags']
if args.method == 'pyltp':
ltp_pos.release()
ltp_seg.release()
adf, qadf = clean_token(adf, qadf)
# sample article
if 'answer' in qadf.columns and not args.test:
sample_df = parallel_sample_article(adf, qadf, args.seq_len_train)
sample_df = parallel_find_gold_span(sample_df)
else:
sample_df = parallel_sample_article(adf, qadf, args.seq_len_test)
croups = list(adf['article_tokens']) + list(qadf['question_tokens'])
flag_croups = list(adf['article_flags']) + list(qadf['question_flags'])
if 'answer' in qadf.columns and not args.test:
croups += list(qadf['answer_tokens'])
flag_croups += list(qadf['answer_flags'])
sample_df = sample_df.to_dict(orient='records')
return sample_df, croups, flag_croups
def gen_w2v(args, total_croups, total_flag_croups, iter=60):
print('training token vocab...')
token_wv = Word2Vec(total_croups,
size=args.token_emb_dim,
window=5, compute_loss=True,
min_count=2, iter=iter,
workers=mp.cpu_count()).wv
print('training flag vocab...')
flag_wv = Word2Vec(total_flag_croups,
size=args.flag_emb_dim,
window=5, compute_loss=True, iter=iter,
workers=mp.cpu_count()).wv
return token_wv, flag_wv
def gen_sgns_embed(args, tokens_in):
print('generating sgns vocab and embeddings...')
reader = bz2_vocab_reader(args.sgns_vocab_path)
v_dict = {}
total = len(tokens_in)
tokens_in = set(tokens_in)
for w, v in reader:
if w in tokens_in:
v_dict[w] = v
tokens_in.remove(w)
print('sgns covered {} of vocab'.format(len(v_dict) * 1. / total))
return list(v_dict.keys()), np.array(list(v_dict.values()))
def main(args):
if args.test:
raw_dir = args.test_raw_path
gen_dir = args.test_gen_path
max_len = args.seq_len_test
else:
raw_dir = args.train_raw_path
gen_dir = args.train_gen_path
max_len = args.seq_len_train
for raw_file in os.listdir(raw_dir):
# path stuff
print(raw_file)
raw_file_path = osp.join(raw_dir, raw_file)
raw_file_name = osp.splitext(raw_file)[0]
out_dir = osp.join(gen_dir, raw_file_name)
samples_out_dir = osp.join(out_dir, 'samples_' + args.method + str(max_len))
samples_out_path = osp.join(out_dir, 'total_samples_' + args.method + str(max_len) + '.json')
if not osp.isfile(samples_out_path):
samples, croups, flag_croups = process_dataset(args, raw_file_path)
write_json(samples, samples_out_path)
write_json(croups, osp.join(out_dir, 'croups_' + args.method + '.json'))
write_json(flag_croups, osp.join(out_dir, 'flag_croups_' + args.method + '.json'))
for sample in samples:
if 'max_rouge' in sample.keys() and not args.test:
sample_fname = '_'.join([sample['article_id'],
sample['question_id'],
str(round(sample['max_rouge'], 4))]) + '.json'
else:
sample_fname = '_'.join([sample['article_id'],
sample['question_id']]) + '.json'
write_json(sample, osp.join(samples_out_dir, sample_fname))
total_croups = []
total_flag_croups = []
tokens = set()
for raw_file in os.listdir(raw_dir):
# path stuff
raw_file_name = osp.splitext(raw_file)[0]
out_dir = osp.join(gen_dir, raw_file_name)
croups = read_json(osp.join(out_dir, 'croups_' + args.method + '.json'))
flag_croups = read_json(osp.join(out_dir, 'flag_croups_' + args.method + '.json'))
if not args.test:
total_croups += croups
total_flag_croups += flag_croups
else:
for ts in croups:
tokens = tokens.union(set(ts))
if not args.test:
train_tv_path = osp.join(args.embed_path, 'base_token_vocab_' + args.method + '.pkl')
train_fv_path = osp.join(args.embed_path, 'base_flag_vocab_' + args.method + '.pkl')
train_tembed_path = osp.join(args.embed_path, 'base_token_embed_' + args.method + '.pkl')
train_sgns_e_path = osp.join(args.embed_path, 'train_sgns_embed_' + args.method + '.pkl')
train_sgns_v_path = osp.join(args.embed_path, 'train_sgns_vocab_' + args.method + '.pkl')
train_fembed_path = osp.join(args.embed_path, 'base_flag_embed_' + args.method + '.pkl')
if not osp.isfile(train_tv_path):
token_wv, flag_wv = gen_w2v(args, total_croups, total_flag_croups)
print('token vocab size: {}'.format(len(token_wv.vocab)))
print('flag vocab size: {}'.format(len(flag_wv.vocab)))
tokens = token_wv.index2word.copy()
flags = flag_wv.index2word.copy()
token_embeds = token_wv.vectors
flag_embeds = flag_wv.vectors
write_pkl(token_embeds, train_tembed_path)
write_pkl(flag_embeds, train_fembed_path)
write_pkl(tokens, train_tv_path)
write_pkl(flags, train_fv_path)
del token_wv, flag_wv, total_croups, total_flag_croups, token_embeds, flag_embeds
sgns_vocab, sgns_embed = gen_sgns_embed(args, tokens)
write_pkl(sgns_embed, train_sgns_e_path)
write_pkl(sgns_vocab, train_sgns_v_path)
else:
tokens = list(tokens)
test_sgns_e_path = osp.join(args.embed_path, 'test_sgns_embed_' + args.method + '.pkl')
test_sgns_v_path = osp.join(args.embed_path, 'test_sgns_vocab_' + args.method + '.pkl')
sgns_vocab, sgns_embed = gen_sgns_embed(args, tokens)
write_pkl(sgns_embed, test_sgns_e_path)
write_pkl(sgns_vocab, test_sgns_v_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Prepare Data")
# data
parser.add_argument('--method', type=str, default='jieba',
choices=['jieba', 'pyltp'])
parser.add_argument('--seq_len_train', type=int, default=500)
parser.add_argument('--seq_len_test', type=int, default=500)
parser.add_argument('--token_emb_dim', type=int, default=300)
parser.add_argument('--flag_emb_dim', type=int, default=75)
parser.add_argument('--test', type=bool, default=False)
# misc
working_dir = osp.dirname(osp.abspath(__file__))
parser.add_argument('--train_raw_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/train/raw'))
parser.add_argument('--test_raw_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/test/raw'))
parser.add_argument('--embed_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/embed'))
parser.add_argument('--sgns_vocab_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/embed/merge_sgns_bigram_char300.txt.bz2'))
parser.add_argument('--train_gen_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/train/gen'))
parser.add_argument('--test_gen_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/test/gen'))
parser.add_argument('--jieba_big_dict', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/dict.txt.big'))
parser.add_argument('--ltp_cws_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/ltp_data_v3.4.0/cws.model'))
parser.add_argument('--ltp_pos_path', type=str, metavar='PATH',
default=osp.join(working_dir, 'data/ltp_data_v3.4.0/pos.model'))
main(parser.parse_args())