-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipedia.py
169 lines (144 loc) · 4.8 KB
/
wikipedia.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
import os
import re
import multiprocessing as mp
from nltk import tokenize
# Matches HTML tag and their content
RE_1 = re.compile(r'<\s*a[^>]*>(.*?)<\s*/\s*a>')
def _worker_loop(work_queue, result_queue):
while True:
fpath = work_queue.get()
if not fpath:
break
with open(fpath, 'r') as fp:
for line in fp:
line = line.strip()
before = len(line)
re.sub(RE_1, '', line)
after = len(line)
if not line and before != after:
continue
if line.startswith('<doc'):
doc = []
elif line.startswith('</doc>'):
result_queue.put(process_doc(doc))
else:
doc.append(line)
result_queue.put(None)
def process_doc(doc):
# Remove title
doc = doc[1:]
while len(doc) > 0 and not doc[0]:
doc = doc[1:]
if not doc:
return ''
sections_paragraphs_lines = [[[]]]
for line in doc:
if not line:
sections_paragraphs_lines[-1].append([])
continue
if line.startswith('Section::::'):
sections_paragraphs_lines.append([[]])
continue
sections_paragraphs_lines[-1][-1] += tokenize.sent_tokenize(line)
final = ''
for section in sections_paragraphs_lines:
final_section = ''
for paragraph in section:
if not paragraph:
continue
if final_section:
final_section += '\n\n' + '\n'.join(paragraph)
else:
final_section = '\n'.join(paragraph)
if not final_section:
continue
if final:
final += '\n\n\n' + final_section
else:
final = final_section
return final
def get_fpaths(dpath):
fpaths = []
for name in os.listdir(dpath):
path = os.path.join(dpath, name)
if os.path.isdir(path):
fpaths += get_fpaths(path)
else:
fpaths.append(path)
return fpaths
def process_wiki(dpath_in, dpath_out, num_workers, splits):
if not os.path.exists(dpath_out):
os.makedirs(dpath_out)
fpaths = get_fpaths(dpath_in)
num_workers = min(len(fpaths), num_workers)
work_queue, result_queue = mp.Queue(), mp.Queue(num_workers * 10)
processes = []
for _ in range(max(1, num_workers)):
p = mp.Process(target=_worker_loop, args=(work_queue, result_queue))
p.start()
processes.append(p)
for fpath in fpaths:
work_queue.put(fpath)
for _ in range(len(processes)):
work_queue.put(None)
remaining = len(processes)
total_count = 0
for split_size, split_fname in splits:
if not remaining:
break
with open(os.path.join(dpath_out, split_fname), 'w') as fp:
count = 0
first = True
while remaining and (split_size < 0 or count < split_size):
result = result_queue.get()
if result is None:
remaining -= 1
else:
count += 1
total_count += 1
if result:
if first:
first = False
else:
result = '\n\n\n\n' + result
fp.write(result)
if total_count % 10000 == 0:
print('\rProcessed {} articles, {} files remaining.'.format(
total_count, remaining), end='')
for p in processes:
p.join()
work_queue.close()
result_queue.close()
print('\nProcessed {} articles and {} files!'.format(total_count, len(fpaths)))
def main():
import argparse
parser = argparse.ArgumentParser('Extract article texts from wiki dump')
parser.add_argument(
'--input', '-i',
type=str,
metavar='DIR',
help='Path of previously extracted articles.',
required=True
)
parser.add_argument('-o', '--output', type=str, metavar='DIR', required=True,
help='Output directory')
parser.add_argument('-s', '--splits', type=int, nargs='*', default=[],
help='Number of articles of each split.')
parser.add_argument(
'--num-workers',
type=int,
metavar='WORKERS',
help='Number of worker processes. Default: %(default)s.',
default=mp.cpu_count()
)
args = parser.parse_args()
splits = [(split, 'split{}.txt'.format(i + 1)) for i, split in enumerate(args.splits)]
splits.append((-1, 'remainder.txt'))
process_wiki(
dpath_in=args.input,
dpath_out=args.output,
num_workers=args.num_workers,
splits=splits
)
if __name__ == '__main__':
main()