-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess_wiki_files.py
130 lines (93 loc) · 3.63 KB
/
process_wiki_files.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
# Copyright (c) 2022 Philip May
# This software is distributed under the terms of the MIT license
# which is available at https://opensource.org/licenses/MIT
from somajo import SoMaJo
import os
import re
from multiprocessing import Pool, cpu_count
# user settings
INPUT_DIR = "data/dewiki-20220201"
OUTPUT_DIR = "data/dewiki-20220201-clean"
LANGUAGE = "de_CMC"
# LANGUAGE = "en_PTB"
tokenizer = SoMaJo(LANGUAGE)
html_tag_patten = re.compile('<[^<>]+>')
# see https://github.com/tsproisl/SoMaJo/issues/17
def detokenize(tokens):
"""Convert SoMaJo tokens to sentence (str)."""
result_list = []
for token in tokens:
if token.original_spelling is not None:
result_list.append(token.original_spelling)
else:
result_list.append(token.text)
if token.space_after:
result_list.append(" ")
result = "".join(result_list)
result = result.strip()
return result
def is_doc_start_line(line):
return line.startswith('<doc id=')
def is_doc_end_line(line):
return line.startswith('</doc>')
def get_data_dirs(root_dir):
return [name for name in os.listdir(root_dir)
if os.path.isdir(os.path.join(root_dir, name))]
def get_data_files(root_dir):
return [name for name in os.listdir(root_dir)
if os.path.isfile(os.path.join(root_dir, name))]
def process_text_line(line):
# remove HTML taks if still there
line = re.sub(html_tag_patten, ' ', line)
sentences = tokenizer.tokenize_text([line])
result = []
for s in sentences:
sentence_string = detokenize(s)
result.append(sentence_string)
return result
def process_directory(map_item):
input_dir, output_file_name = map_item
print("Creating:", output_file_name)
with open(os.path.join(OUTPUT_DIR, output_file_name), 'a') as output_file:
# r_=root, d_=directories, f_=files
data_files = get_data_files(input_dir)
for data_file in data_files:
next_input_file = os.path.join(input_dir, data_file)
print("Reading file:", next_input_file)
with open(next_input_file, "r") as input_file:
skip_next_line = False
for line in input_file:
# drop line with start tag
if is_doc_start_line(line):
skip_next_line = True
continue
# drop line with end tag and append blank line
if is_doc_end_line(line):
output_file.write("\n")
continue
# skip first line to skip headline
if skip_next_line:
skip_next_line = False
continue
# skip empty lines
if len(line) <= 1:
continue
sentences = process_text_line(line)
for sentence in sentences:
# ignore blank lines and make sure that stuff like "\n" is also ignored:
if len(sentence) > 2:
output_file.write(f"{sentence}\n")
if __name__ == '__main__':
# get sub directories with data
data_dirs = get_data_dirs(INPUT_DIR)
# create tasks for parallel execution
task_list = []
for data_dir in data_dirs:
call_item = (os.path.join(INPUT_DIR, data_dir), data_dir + ".txt")
task_list.append(call_item)
pool_size = cpu_count() * 4
print("pool_size:", pool_size)
# execute tasks in parallel
with Pool(pool_size) as p:
p.map(process_directory, task_list)
print("Done!")