-
Notifications
You must be signed in to change notification settings - Fork 19
/
preprocess.py
61 lines (52 loc) · 1.97 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
# -*- coding: utf-8 -*-
import os
import re
import time
import codecs
import argparse
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
BASE_FOLDER = os.path.abspath(os.path.dirname(__file__))
DATA_FOLDER = os.path.join(BASE_FOLDER, 'data')
DEFAULT_FIN = os.path.join(DATA_FOLDER, '唐诗语料库.txt')
DEFAULT_FOUT = os.path.join(DATA_FOLDER, 'poem.txt')
reg_noisy = re.compile('[^\u3000-\uffee]')
reg_note = re.compile('((.*))') # Cannot deal with () in seperate lines
# 中文及全角标点符号(字符)是\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee
def set_arguments():
parser = argparse.ArgumentParser(description='Pre process')
parser.add_argument('--fin', type=str, default=DEFAULT_FIN,
help='Input file path, default is {}'.format(DEFAULT_FIN))
parser.add_argument('--fout', type=str, default=DEFAULT_FOUT,
help='Output file path, default is {}'.format(DEFAULT_FOUT))
return parser
if __name__ == '__main__':
parser = set_arguments()
cmd_args = parser.parse_args()
print('{} START'.format(time.strftime(TIME_FORMAT)))
fd = codecs.open(cmd_args.fin, 'r', 'utf-8')
fw = codecs.open(cmd_args.fout, 'w', 'utf-8')
reg = re.compile('〖(.*)〗')
start_flag = False
for line in fd:
line = line.strip()
if not line or '《全唐诗》' in line or '<http' in line or '□' in line:
continue
elif '〖' in line and '〗' in line:
if start_flag:
fw.write('\n')
start_flag = True
g = reg.search(line)
if g:
fw.write(g.group(1))
fw.write('\n')
else:
# noisy data
print(line)
else:
line = reg_noisy.sub('', line)
line = reg_note.sub('', line)
line = line.replace(' .', '')
fw.write(line)
fd.close()
fw.close()
print('{} STOP'.format(time.strftime(TIME_FORMAT)))