-
Notifications
You must be signed in to change notification settings - Fork 1
/
c4-filter.py
412 lines (320 loc) · 12 KB
/
c4-filter.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
import os
import re
import sys
import json
import heapq
import fileinput
import dataclasses
import hashlib
import collections
from typing import Optional, Any
_SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
# Filters
_MIN_WORDS_PER_LINE = 5
_MIN_NUM_SENTENCES = 3
_MAX_WORD_LENGTH = 1000
_END_MARKS = (".", "?", "!", '"', "؟") # FIXME add Arabic
_ELLIPSIS = "..."
_POLICY_SUBSTRINGS = [
"terms of use",
"privacy policy",
"cookie policy",
"uses cookies",
"use of cookies",
"use cookies",
]
_MIN_PARAGRAPHS = 3
_MIN_PARAGRAPH_LEN = 200
_BADWORDS_URL = "https://raw.githubusercontent.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/5faf2ba42d7b1c0977169ec3611df25a3c08eb13/ar"
UNKNOWN_LANGUAGE = "und"
@dataclasses.dataclass
class PageFeatures:
url: str = ""
normalized_url: str = ""
text: str = ""
timestamp: str = ""
content_length: str = ""
content_type: str = ""
word_count: Optional[int] = None
language: Optional[str] = None
discarded: Optional[str] = None
class Filter:
def __init__(self):
pass
def should_pass(self, page):
return True
def __call__(self, page):
if self.should_pass(page):
return page
class Processor:
def __init__(self):
pass
def process(self, page):
pass
def __call__(self, page):
self.process(page)
return page
class Pipeline:
def __init__(self, modules, debug=False):
self.modules = modules
self.debug = debug
def __call__(self, dataset):
for page in dataset:
for module in self.modules:
new_page = module(page)
if new_page is None:
if self.debug:
page.discarded = module.__class__.__name__
yield page
break
page = new_page
else:
yield page
class NormalizeUrlProcessor(Processor):
def __init__(self):
super().__init__()
def process(self, page):
url = page.url
url = re.sub(r"https?:\/\/(www\.)?", "", url)
url = re.sub(r"\?(utm_|ref|feed).*", "", url)
url = url.rstrip("/")
page.normalized_url = url
class WordCountProcessor(Processor):
def __init__(self):
super().__init__()
def process(self, page):
page.word_count = len(page.text.split())
class CleanTextProcessor(Processor):
def __init__(self):
super().__init__()
self.citation_regex = re.compile(r"\[\d*\]|\[edit\]|\[citation needed\]")
self.min_words_per_line = _MIN_WORDS_PER_LINE
self.min_num_sentences = _MIN_NUM_SENTENCES
self.max_word_length = _MAX_WORD_LENGTH
self.line_delimiter = "\n"
@staticmethod
def line_is_copyright(text):
return '©' in text
@staticmethod
def line_is_javascript_code(text):
# Count occurrences of specific characters
count_open_parenthesis = text.count('(')
count_close_parenthesis = text.count(')')
count_dollar_sign = text.count('$')
count_semicolon = text.count(';')
count_equals = text.count('=')
count_equals = text.count('{')
count_equals = text.count('}')
count_equals = text.count('+')
count_equals = text.count('_')
count_equals = text.count("'")
count_equals = text.count('"')
count_equals = text.count('#')
count_equals = text.count('/')
# Calculate the total number of characters
total_characters = len(text)
# Calculate the total count of specific characters
total_count = (count_open_parenthesis + count_close_parenthesis +
count_dollar_sign + count_semicolon + count_equals)
# Calculate the percentage of the total count relative to the total characters
percentage_total_count = (total_count / total_characters) * 100
# Check if the percentage exceeds 8%
if percentage_total_count > 8:
return True
else:
return False
@staticmethod
def contains_arabic(text):
# Regular expression pattern to match Arabic characters
arabic_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]+')
# Check if the pattern matches the text
if arabic_pattern.search(text):
return True
else:
return False
def process(self, page):
text = page.text
lines = text.splitlines()
valid_lines = []
def line_has_too_long_word(line):
for word in line.split():
if len(word) > self.max_word_length:
return True
return False
for line in lines:
line = line.strip()
line = self.citation_regex.sub("", line)
if line_has_too_long_word(line):
continue
if not line.endswith(_END_MARKS) or line.endswith(_ELLIPSIS):
counter_inc_fn("line-filtered:no_endmark")
continue
if len(line.split()) < self.min_words_per_line:
counter_inc_fn("line-filtered:too_short")
continue
if CleanTextProcessor.line_is_javascript_code(line):
continue
if not CleanTextProcessor.contains_arabic(line):
continue
line_lower = line.lower()
# Remove documents which contain lorem ipsum
if "lorem ipsum" in line_lower:
counter_inc_fn("filtered:loremipsum")
return
# Remove "javascript must be enabled" notices
if "javascript" in line_lower:
counter_inc_fn("line-filtered:javascript")
continue
# Remove docs which probably contain javascript code
if "{" in line:
counter_inc_fn("filtered:squigglybracket")
continue
# Remove copyrights
if "©" in line:
counter_inc_fn("filtered:copyright")
continue
# Remove policy lines
if any(p in line_lower for p in _POLICY_SUBSTRINGS):
counter_inc_fn("line-filtered:policy")
continue
# num_sentences += len(_get_sentences(line))
valid_lines.append(line)
page.text = '\n'.join(valid_lines)
class BadUrlFilter(Filter):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.regex = re.compile(r"(porn|video)")
def should_pass(self, page):
if self.regex.search(page.url.lower()):
return False
return True
class LengthFilter(Filter):
def __init__(self, min_len=100, max_len=10000, **kwargs):
super().__init__(**kwargs)
self.min_len = min_len
self.max_len = max_len
def should_pass(self, page):
if self.min_len < len(page.text) < self.max_len:
return True
return False
class BadWordsFilter(Filter):
def __init__(self, badwords, filter_fraction=0.01, **kwargs):
super().__init__(**kwargs)
self.badwords = badwords
self.filter_fraction = filter_fraction
self.filter = get_badwords_filter_fn(badwords=badwords, filter_fraction=filter_fraction)
def should_pass(self, page):
return self.filter(page)
class C4ParagraphFilter(Filter):
def __init__(self, min_paragraphs=3, min_paragraph_len=200, line_delimiter="\n"):
super().__init__()
self.min_paragraphs = min_paragraphs
self.min_paragraph_len = min_paragraph_len
self.line_delimiter = line_delimiter
def should_pass(self, page):
lines = page.text.split(line_delimiter)
if len(lines) < self.min_paragraphs or \
min(heapq.nlargest(3, [len(l) for l in lines])) < self.min_paragraph_len:
return False
return True
line_delimiter = '\n'
counts = {}
def counter_inc_fn(name):
counts[name] = counts.get(name, 0) + 1
def get_counter_inc_fn(counter_name):
return counter_inc_fn
def get_hashed_url_filter_fn(predicate_fn):
def filter_fn(page):
url = page.normalized_url
val = int(
hashlib.md5(tf.compat.as_text(url).encode("utf-8")).hexdigest(), 16
)
return predicate_fn(val)
return filter_fn
def load_badwords():
badwords = collections.defaultdict(set)
with open(os.path.join(_SCRIPT_DIR, 'ar-badwords.txt'), 'rt') as f:
badwords['ar'].update(x.strip() for x in f)
return badwords
def get_badwords_filter_fn(badwords, filter_fraction: float = 1.0):
"""Filters pages at given rate that contain language-specific bad word(s)."""
badwords_regex = {}
for lang, words in badwords.items():
words = [re.escape(w) for w in words]
badwords_regex[lang] = (
# For Japanese, Thai, and Chinese, do not require word separations.
re.compile("|".join(words))
if lang in ("ja", "th", "zh")
# For other languages, match only when flanked by non-word chars.
else re.compile(r"(?:\W|^)({})(?:\W|$)".format("|".join(words)))
)
filter_ratio = float.as_integer_ratio(filter_fraction)
keep_badword_page = get_hashed_url_filter_fn(
lambda x: x % filter_ratio[1] >= filter_ratio[0]
)
def badwords_filter(page):
lang = page.language.split("-")[0] # remove suffix if present
if lang in badwords_regex:
text = page.text
badwords_found = badwords_regex[lang].search(text.lower())
if badwords_found is not None:
if keep_badword_page(page):
get_counter_inc_fn("badwords-filter")("soft-passed")
get_counter_inc_fn("badwords-filter-%s" % lang)("soft-passed")
return True
get_counter_inc_fn("badwords-filter")("filtered")
get_counter_inc_fn("badwords-filter-%s" % lang)("filtered")
return False
get_counter_inc_fn("badwords-filter-%s" % lang)("passed")
get_counter_inc_fn("badwords-filter")("passed")
return True
return badwords_filter
def process(args):
badwords = load_badwords()
pipeline = Pipeline([
NormalizeUrlProcessor(),
BadUrlFilter(),
CleanTextProcessor(),
C4ParagraphFilter(),
BadWordsFilter(badwords=badwords),
], debug=args.debug)
def pages():
for json_line in fileinput.input(files=("-"), encoding="utf-8"):
yield PageFeatures(**json.loads(json_line))
for page in pipeline(pages()):
print(json.dumps(dataclasses.asdict(page, dict_factory=lambda x: {k: v for (k, v) in x if v is not None}), ensure_ascii=False), file=sys.stdout)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
prog="c4-filter",
description="filter and clean using c4 strategy",
)
parser.add_argument('--debug', dest='debug', action='store_true',
help='debug')
parser.add_argument('--debug-url', dest='debug_url', type=str,
help='use if you want to debug a specific url')
parser.add_argument('--debug-clean', dest='debug_clean', action='store_true', default=False,
help='use this flag to display decisions made in cleaning')
parser.add_argument('-o', '--output', dest='output', help='output file')
parser.add_argument('--out-dir', dest='out_dir', type=str,
help='output directory')
parser.add_argument('--clean', dest='clean', action='store_true', default=False,
help='run text cleaning for article')
parser.add_argument('--only-arabic', dest='only_arabic', action='store_true', default=False,
help='keep text only if it contains at least some Arabic characters')
parser.add_argument('--length-filter', dest='length_filter', action='store_true', default=False,
help='filter content too short or too long')
parser.add_argument('--paragraph-filter', dest='paragraph_filter', action='store_true', default=False,
help='run paragraph filter')
parser.add_argument('--min-paragraphs', dest='min_paragraphs', type=int, default=_MIN_PARAGRAPHS,
help='minimal number of paragraphs')
parser.add_argument('--min-paragraph-len', dest='min_paragraph_len', type=int, default=_MIN_PARAGRAPH_LEN,
help='minimal length for a paragraph')
parser.add_argument('--lang-detect', dest='lang_detect', action='store_true', default=False,
help='run language detection')
parser.add_argument('--badwords-filter', dest='badwords_filter', action='store_true', default=False,
help='run badwords filter')
parser.add_argument('--add-word-count', dest='add_word_count', action='store_true', default=False,
help='add word counts in output')
args = parser.parse_args()
process(args)