-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_data.py
More file actions
353 lines (312 loc) · 13.2 KB
/
clean_data.py
File metadata and controls
353 lines (312 loc) · 13.2 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
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
import argparse
import json
import functools
import data_normalizers as DN
import data_filters as DF
from typing import Callable, List, Iterable, Dict, Any, Optional, Tuple, Set
from tqdm import tqdm
from functools import partial
import multiprocessing as mp
def read_jsonl(fn: str) -> Iterable[Dict[Any, Any]]:
with open(fn) as fh:
for line in fh:
try:
yield json.loads(line)
except json.JSONDecodeError:
pass
# The compose function can compose multiple functions but is not used in favour
# of for-loops
def compose(*functions: List[Callable]) -> Callable:
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x)
def my_normalize(jobj: Dict[Any, Any], sub_functions: List[Callable]) -> Dict[Any, Any]:
"""
Given a list of functions and a json-object, this function iterates through
the object's content and applies all normalizer functions to update the
content.
"""
meta = jobj["meta"]
content = jobj["content"][:]
for i, c in enumerate(content):
if c is None:
continue
# content[i] = normalize(content[i])
for n in sub_functions:
content[i] = n(content[i])
return {"meta": meta, "content": content}
def apply_normalizers(fn: str, args: argparse.Namespace) -> None:
"""
This function collects normalizers and applies them to all json-objects in
the given file.
"""
normalizers: List[Callable] = []
if args.unicode_normalize:
normalizers.append(DN.unicode_normalize)
if args.unidecode_normalize:
normalizers.append(DN.unidecode_normalize)
if args.moses_normalize:
normalizers.append(DN.moses_normalize)
if args.common_errors:
normalizers.append(DN.common_errors)
if args.anonymize:
normalizers.append(DN.anonymize)
if args.strip_incomplete_string:
normalizers.append(DN.strip_incomplete_string)
if args.sentence_split:
normalizers.append(DN.sentence_split)
if args.strip_incomplete_sentence:
if not normalizers[-1] == DN.sentence_split:
print("--strip_incomplete also require --sentence_split")
raise Exception("Try again")
normalizers.append(DN.strip_incomplete_sentence)
# normalize = compose(*normalizers)
if normalizers == []:
return
data = (read_jsonl(fn))
with open(fn + ".normalized", "w") as fout:
# return_dict = multi_func(my_normalize, data, args.n_processes, 15)
return_list = multi_pool(my_normalize, data, args.n_processes, args.chunksize, normalizers, args)
for xs in return_list:
for x in xs:
meta = x["meta"]
content = x["content"]
print(json.dumps({"meta": meta, "content": content}), file=fout)
def my_filter(jobj: Dict[Any, Any],
sub_functions: List[Callable],
remove_breaks: bool = False,
) -> Tuple[Dict[Any, Any], Dict[str, List[str]]]:
"""
Given a list of functions and a json-object, this function iterates through
the object's content and applies all filter functions to filter out unwanted
content.
The stricter filters should be applied first, to skip further function calls.
"""
meta = jobj["meta"]
content = jobj["content"][:]
new_content: List[Optional[str]] = []
filtered: Dict[str, List[str]] = {}
for f in sub_functions:
try:
name = f.__name__
except AttributeError:
name = "exact_duplicate"
filtered[name] = []
for c in content:
keep = True
if c is None:
if not remove_breaks:
new_content.append(None)
continue
if type(c) == list: # some docs in SOU are apparently lists
c = " ".join(c)
c = c.replace("\n", " ") # other docs have newlines which some filter does not like
for f in sub_functions:
if not f(c):
keep = False
try:
name = f.__name__
except AttributeError:
name = "exact_duplicate"
filtered[name].append(c)
try:
if new_content[-1] is not None:
if not remove_breaks:
new_content.append(None)
except IndexError:
pass
break
if keep:
new_content.append(c)
return {"meta": meta, "content": new_content}, filtered
def apply_filters(fn: str, args: argparse.Namespace) -> None:
"""
This function collects filters and applies them to all json-objects in
the given file.
"""
filters: List[Callable] = []
if args.filter_by_num_tokens:
filters.append(DF.filter_by_num_tokens)
if args.filter_by_unicode:
filters.append(DF.filter_by_unicode)
if args.filter_tv_tables:
filters.append(DF.filter_tv_tables)
# language filter should be run after normalization
# other filters should be run before to have less data when normalizing
if args.filter_by_language:
filters.append(DF.filter_by_language)
if args.filter_exact_duplicates:
if args.do_parallel:
manager = mp.Manager()
# hashes: List[int] = [] # manager.list()
# hashes: Set[str] = set() # manager.list()
# hashes: Dict[str, int] = manager.dict()
else:
# hashes: Dict[int, int] = {}
hashes: Set[int] = set() # manager.list()
fed = partial(DF.filter_exact_duplicates, hashes=hashes)
filters.append(fed)
if args.filter_exact_duplicates_min_size:
if args.do_parallel:
manager = mp.Manager()
hashes_fedup: Dict[str, int] = manager.dict()
else:
hashes_fedup: Dict[str, int] = {}
fed = partial(DF.filter_exact_duplicates, hashes=hashes_fedup)
def fed_up(x):
if DF.filter_by_num_tokens(x):
return fed(x)
else:
return False
filters.append(fed_up)
if filters == []:
return
data = (read_jsonl(fn))
if args.save_removed:
fout_err = open(fn + ".removed", "w")
with open(fn + ".filtered", "w") as fout:
# return_dict = multi_func(my_filter, data, args.n_processes, None)
my_filter_rfb = partial(my_filter, remove_breaks=args.remove_filter_breaks)
return_list = multi_pool(my_filter_rfb, data, args.n_processes, args.chunksize, filters, args)
if args.save_removed:
removed: Dict[str, List[str]] = {}
# for x, y in return_list:
for xys in return_list:
for x, y in xys:
meta = x["meta"]
content = x["content"]
print(json.dumps({"meta": meta, "content": content}), file=fout)
if args.save_removed:
for k in y.keys():
if k not in removed:
removed[k] = []
removed[k].extend(y[k])
if args.save_removed:
json.dump(removed, fout_err)
if args.save_removed:
fout_err.close()
def multi_pool(my_function: Callable, data: Iterable[Dict[Any, Any]],
n_processes: int, chunk_size: Optional[int],
functions: List[Callable],
args: argparse.Namespace): #List[Any]:
"""
multi_pool is used to apply the normalizers and filters on one file with
multiple processes.
"""
if chunk_size is None:
# chunk_size = max((len(data) // n_processes, 1))
chunk_size = 1 # max((len(data) // n_processes, 1))
my_f = partial(my_function, sub_functions=functions)
return_list = []
if args.do_parallel:
pool = mp.Pool(processes=n_processes)
iterator_thing = pool.imap(my_f, tqdm(data), chunksize=chunk_size)
else:
iterator_thing = map(my_f, tqdm(data))
for res in iterator_thing:
return_list.append(res)
if len(return_list) >= chunk_size:
yield return_list
return_list = []
# return_dict = list(pool.starmap(my_function, tqdm(itertools.product(data, [functions]), total=len(data)), chunksize=chunk_size))
# return return_list
if args.do_parallel:
pool.close()
def json2txt(fn: str) -> None:
with open(fn + ".txt", "w") as fout:
for jobj in read_jsonl(fn):
content = jobj["content"]
for c in content:
if c is None:
continue
if type(c) == str and c != "":
print(c, file=fout)
elif type(c) == list:
# if doc has been split into sentences
if c != []:
not_empty = False
for x in c:
if x != "":
not_empty = True
print(x, file=fout)
if not_empty:
print("", file=fout)
def fuse_paragraphs(fn: str, ignore_breaks: bool = False) -> None:
# ignore_breaks ignores None elements that appear when one or more
# breaks are introduced due to filtering paragraphs
# if there are None elements paragraphs are only fused together to a document
# between None elements, i.e. continuous paragraphs
with open(fn + ".fused", "w") as fout:
for jobj in read_jsonl(fn):
content = jobj["content"]
# if doc has been split into sentences then this would return the
# same format as a doc split into paragraphs, which can be ver
# confusing
# Please don't split into sentences
# returns list with one element that joins the previous list's
# elements
if content:
if ignore_breaks:
jobj["content"] = [" ".join(filter(lambda x: x is not None, content))]
else:
new_content = []
last_content: List[str] = []
for c in content:
if c is None and last_content:
new_content.append(" ".join(last_content))
last_content = []
elif c is not None:
last_content.append(c)
if last_content:
new_content.append(" ".join(last_content))
jobj["content"] = new_content
print(json.dumps(jobj), file=fout)
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
# parser.add_argument("--folder", type=str, default=None)
parser.add_argument("--file", type=str, default=None)
parser.add_argument("--filter_by_num_tokens", action="store_true")
parser.add_argument("--filter_by_language", action="store_true")
parser.add_argument("--filter_by_unicode", action="store_true")
parser.add_argument("--filter_exact_duplicates", action="store_true")
parser.add_argument("--filter_tv_tables", action="store_true")
parser.add_argument("--filter_exact_duplicates_min_size", action="store_true")
parser.add_argument("--remove_filter_breaks", action="store_true")
parser.add_argument("--save_removed", action="store_true")
parser.add_argument("--unicode_normalize", action="store_true")
parser.add_argument("--unidecode_normalize", action="store_true")
parser.add_argument("--moses_normalize", action="store_true")
parser.add_argument("--common_errors", action="store_true")
parser.add_argument("--anonymize", action="store_true")
parser.add_argument("--n_processes", type=int, default=1)
parser.add_argument("--chunksize", type=int, default=1)
parser.add_argument("--strip_incomplete_string", action="store_true")
parser.add_argument("--sentence_split", action="store_true")
parser.add_argument("--strip_incomplete_sentence", action="store_true")
parser.add_argument("--json2txt", action="store_true")
parser.add_argument("--fuse-paragraphs", action="store_true")
parser.add_argument("--ignore-breaks", action="store_true")
parser.add_argument("--do_parallel", action="store_true")
return parser.parse_args()
def main():
args = get_args()
print(args)
filter_args = [args.filter_by_unicode, args.filter_exact_duplicates,
args.filter_by_language, args.filter_by_num_tokens,
args.filter_tv_tables, args.filter_exact_duplicates_min_size]
normalize_args = [args.unicode_normalize, args.unidecode_normalize,
args.moses_normalize, args.anonymize, args.common_errors,
args.strip_incomplete_string,
args.strip_incomplete_sentence, args.sentence_split]
if args.file:
if any(filter_args):
apply_filters(args.file, args)
elif any(normalize_args):
apply_normalizers(args.file, args)
elif args.json2txt:
json2txt(args.file)
elif args.fuse_paragraphs:
fuse_paragraphs(args.file, args.ignore_breaks)
if __name__ == "__main__":
import time
start = time.time()
main()
print(time.time() - start)