-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_process.py
More file actions
195 lines (169 loc) · 6.76 KB
/
Copy pathdata_process.py
File metadata and controls
195 lines (169 loc) · 6.76 KB
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import argparse
import csv
import json
import re
from collections import Counter
from pyhanlp import *
from hanziconv import HanziConv
StandardTokenizer = JClass('com.hankcs.hanlp.tokenizer.StandardTokenizer')
def add_arguments(parser):
"""Build ArgumentParser."""
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument("--data_file",
type=str,
default=None,
required=True,
help="data file to process")
parser.add_argument("--output_file",
type=str,
default=None,
required=True,
help="data file to process")
parser.add_argument("--vocab_file",
type=str,
default=None,
help="vocab file, needed when training")
parser.add_argument("--vocab_size",
type=int,
default=50000,
help='vocab size')
parser.add_argument("--embedding",
type='bool',
nargs="?",
const=True,
default=False,
help='whether process embedding file')
parser.add_argument("--is_trian_file",
type='bool',
nargs="?",
const=True,
default=False,
help='this is train file')
def replace_dish(content):
return re.sub("【.{5,20}】", "<dish>", content)
def normalize_num(words):
'''Normalize numbers
for example: 123 -> 100, 3934 -> 3000
'''
tokens = []
for w in words:
try:
ww = w
num = int(float(ww))
if len(ww) < 2:
tokens.append(ww)
else:
num = int(ww[0]) * (10**(len(str(num)) - 1))
tokens.append(str(num))
except:
tokens.append(w)
return tokens
def tokenize(content):
content = content.replace("\u0006",'').replace("\u0005",'').replace("\u0007",'')
# 转为简体中文
content = HanziConv.toSimplified(content)
tokens = []
content = content.lower()
# 去除网站,图片引用
content = re.sub(r"[!a-zA-z]+://[^\s]*", "", content)
content = re.sub(r"\d{3,4}-\d{8}|\d{4}-\{7,8}", "", content)
content = re.sub(r"\d{6,8}", "", content)
# 去除邮箱地址
# content = re.sub(r"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", "", content)
# 断句符号统一为中文符号
content = re.sub(r"!", "!", content)
content = re.sub(r"\?", "?", content)
content = re.sub(r";", ";", content)
content = re.sub(r",", ",", content)
content = re.sub(r"、", ",", content)
content = re.sub(r"~", "~", content)
content = re.sub(r"…", "。", content)
# 去除重复字符
content = re.sub('(\n)+','\n',content)
content = re.sub('(\.)+','.',content)
content = re.sub('(\r)+','',content)
content = re.sub('(\t)+',',',content)
content = re.sub('[?]+?[!]+?','',content)
content = re.sub('[?]+?[。]+?','',content)
content = re.sub('(~)+','~',content)
content = re.sub('(,)+','~',content)
content = re.sub('(。)+','。',content)
content = re.sub('(~。)+','',content)
content = re.sub('(?)+','?',content)
content = re.sub('(!)+','!',content)
content = re.sub('(;)+',';',content)
for para in content.split('\n'):
para_tokens = []
words = [str(w.word) for w in StandardTokenizer.segment(para)]
words = normalize_num(words)
para_tokens.extend(words)
para_tokens.append('<para>')
tokens.append(' '.join(para_tokens))
content = " ".join(tokens)
content = re.sub('\s+',' ',content)
content = re.sub('(<para> )+','<para> ',content)
content = re.sub('(- )+','- ',content)
content = re.sub('(= )+','= ',content)
content = re.sub('(\.)+','. ',content).strip()
content = replace_dish(content)
pattern = re.compile(r"\(|\)|(|)|“|”|\*|《|》|&|#|·|`|=|\+|\}|\{|\|||{|}|「|」|『|』|〔|〕|〖|〗|〘|〙|〚|〛|—|﹏|\"|-")
content = re.sub(pattern, "", content)
content = re.sub(r"@", "", content)
content = re.sub('( )+',' ',content)
if content.endswith("<para>"):
content = content[:-7]
return content
def create_vocab(data, vocab_file, vocab_size):
print("# Start to create vocab ...")
words = Counter()
for item in data:
words.update(item['content'].split())
special_tokens = ['<unk>', '<sos>', '<eos>']
with open(vocab_file, 'w', encoding='utf-8') as f:
for w in special_tokens:
f.write(w + '\n')
for w, _ in words.most_common(vocab_size - len(special_tokens)):
f.write(w + '\n')
print("# Created vocab file {0} with vocab size {1}".format(
vocab_file, vocab_size))
def process_data(output_file, data_file):
data = []
with open(output_file, 'w', encoding='utf-8') as f:
with open(data_file, encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for i, item in enumerate(reader):
content = tokenize(item['content'].strip()[1:-1])
item['content'] = content
abc = json.dumps(item, ensure_ascii=False) + '\n'
f.write(abc)
data.append(item)
if (i + 1) % 10000 == 0:
print("# processed -- %d --" % (i + 1))
return data
def process_embedding(embedding_file, vocab_file, out_embedding_file):
words = set([line.strip() for line in open(vocab_file, encoding='utf-8')])
with open(out_embedding_file, 'w', encoding='utf-8') as f:
for line in open(embedding_file, encoding='utf-8'):
tokens = line.split()
# skip the first line
if len(tokens) == 2:
continue
word = tokens[0].lower()
if word in words:
f.write(word + ' ' + ' '.join(tokens[1:]) + '\n')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
add_arguments(parser)
flags, unparsed = parser.parse_known_args()
if flags.embedding:
process_embedding(flags.data_file, flags.vocab_file, flags.output_file)
else:
if flags.is_trian_file:
if flags.vocab_file is None:
raise ValueError("Must provided a vocab file to save vocab")
data = process_data(flags.output_file, flags.data_file)
create_vocab(data, flags.vocab_file, flags.vocab_size)
else:
process_data(flags.output_file, flags.data_file)