-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiDump_cleaner.py
214 lines (193 loc) · 8.23 KB
/
wikiDump_cleaner.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
# url = "https://github.com/CyberZHG/wiki-dump-reader/blob/master/wiki_dump_reader/cleaner.py"
import re
class Cleaner(object):
def __init__(self):
pass
def clean_text(self, text):
text = self._remove_file_links(text)
text = self._remove_image_links(text)
text = self._remove_external_links(text)
text = self._remove_refs(text)
text = self._remove_emphasises(text)
text = self._remove_comments(text)
text = self._remove_langs(text)
# text = self._remove_titles(text)
text = self._remove_choices(text)
text = self._remove_templates(text)
text = self._remove_htmls(text)
text = self._remove_lists(text)
text = self._remove_indents(text)
text = self._remove_styles(text)
text = self._remove_spaces(text)
text = self._remove_continuous_newlines(text)
return text.strip()
def _remove_file_links(self, text):
"""Remove links like `[[File:*]]`"""
text = self._remove_resource_links(text, 'File')
text = re.sub(r'^File:.*$', '', text, flags=re.MULTILINE)
return text
def _remove_image_links(self, text):
"""Remove links like `[[File:*]]`"""
return self._remove_resource_links(text, 'Image')
def _remove_resource_links(self, text, resource):
"""Remove links likes `[[*:*]]`"""
pattern = '[[' + resource + ':'
pattern_begin = text.find(pattern)
if pattern_begin == -1:
return text
begin, removed = 0, ''
while begin < len(text):
if pattern_begin > begin:
removed += text[begin:pattern_begin]
pattern_end, depth = pattern_begin + 2, 2
while pattern_end < len(text):
ch = text[pattern_end]
pattern_end += 1
if ch == '[':
depth += 1
elif ch == ']':
depth -= 1
if depth == 0:
break
if depth == 0:
begin = pattern_end
else:
removed += text[begin]
begin = max(begin+1, pattern_end)
pattern_begin = text.find(pattern, begin)
if pattern_begin == -1:
break
if len(text) > begin:
removed += text[begin:]
return removed
def _remove_external_links(self, text):
"""Remove links like [*]"""
return re.sub(r'\[h[^ ]+ (.*?)\]', r'\1', text)
def _remove_refs(self, text):
"""Remove patterns like <ref*>*</ref>"""
text = re.sub(r'<ref[^/]*?/>', '', text, flags=re.IGNORECASE | re.DOTALL)
text = re.sub(r'<ref.*?</ref>', '', text, flags=re.IGNORECASE | re.DOTALL)
text = re.sub(r'{{Refbegin.*?Refend}}', '', text, flags=re.IGNORECASE | re.DOTALL)
return text
def _remove_emphasises(self, text):
"""Remove patterns like '''*'''"""
text = re.sub(r"'''(.*?)'''", r'\1', text, flags=re.DOTALL)
text = re.sub(r"''(.*?)''", r'\1', text, flags=re.DOTALL)
return text
def _remove_comments(self, text):
"""Remove patterns like <!--*-->"""
return re.sub(r'<!--.*?-->', '', text, flags=re.DOTALL)
def _remove_langs(self, text):
"""Remove pattenrs like {{lang-*|*}}}"""
return re.sub(r'{{lang(-|\|).*?\|(.*?)}}', r'\2', text, flags=re.IGNORECASE | re.DOTALL)
def _remove_titles(self, text):
"""Remove patterns like ==*=="""
return re.sub(r'(={2,6})\s*(.*?)\s*\1', r'\2', text)
def _remove_choices(self, text):
"""Remove patterns like -{zh-hans:*; zh-hant:*}-"""
text = re.sub(
r'-{.{,100}?zh(-hans|-cn|-hk|):(.{,100}?)(;.{,100}?}-|}-)',
r'\2', text,
flags=re.DOTALL
)
text = re.sub(r'-{.{,100}?:(.{,100}?)(;.{,100}?}-|}-)', r'\1', text, flags=re.DOTALL)
text = re.sub(r'-{(.{,100}?)}-', r'\1', text, flags=re.DOTALL)
return text
def _remove_templates(self, text):
"""Remove patterns like {{*}}"""
begin, removed = 0, ''
while begin < len(text):
pattern_begin = text.find('{{', begin)
if pattern_begin == -1:
if begin == 0:
removed = text
else:
removed += text[begin:]
break
if pattern_begin > begin:
removed += text[begin:pattern_begin]
pattern_end, depth = pattern_begin + 2, 2
while pattern_end < len(text):
ch = text[pattern_end]
pattern_end += 1
if ch == '{':
depth += 1
elif ch == '}':
depth -= 1
if depth == 0:
link = text[pattern_begin + 2:pattern_end - 2]
parts = link.split('|')
template_type = parts[0].split(' ')[0].lower()
if len(parts) == 1:
if all(map(lambda x: x in {'"', "'", ' '}, parts[0][:])):
removed += parts[0].replace(' ', '')
elif len(parts) in [2, 3]:
if template_type in {'le'} or template_type.startswith('link-'):
removed += parts[1]
break
begin = pattern_end
return removed
def _remove_htmls(self, text):
return re.sub(r'<(.*?)>', '', text, flags=re.DOTALL)
def _remove_lists(self, text):
return re.sub(r'^\s*[\*#]\s*', '', text, flags=re.MULTILINE)
def _remove_indents(self, text):
return re.sub(r'^\s*[:;]\s*', '', text, flags=re.MULTILINE)
def _remove_styles(self, text):
return re.sub(r':?{\| (style|class)=.*?\|}', '', text, flags=re.IGNORECASE | re.DOTALL)
def _remove_spaces(self, text):
return text.replace('\u200b', '')
def _remove_continuous_newlines(self, text):
return re.sub(r'\n{2,}', '\n', text)
def build_links(self, text):
begin, removed, links = 0, '', []
while begin < len(text):
pattern_begin = text.find('[[', begin)
if pattern_begin == -1:
if begin == 0:
removed = text
else:
removed += text[begin:]
break
if pattern_begin > begin:
removed += text[begin:pattern_begin]
pattern_end, depth = pattern_begin + 2, 2
while pattern_end < len(text):
ch = text[pattern_end]
pattern_end += 1
if ch == '[':
depth += 1
elif ch == ']':
depth -= 1
if depth == 0:
link = text[pattern_begin + 2:pattern_end - 2]
parts = link.split('|')
if len(parts) == 1:
if ':' in link:
pure = link.split(':')[-1]
links.append({
'begin': len(removed),
'end': len(removed) + len(pure),
'link': link,
'text': pure
})
removed += pure
else:
links.append({
'begin': len(removed),
'end': len(removed) + len(text),
'link': link,
'text': link
})
removed += link
elif len(parts) == 2:
links.append({
'begin': len(removed),
'end': len(removed) + len(parts[1]),
'link': parts[0],
'text': parts[1]
})
removed += parts[1]
break
begin = pattern_end
return removed.strip(), links