-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
208 lines (176 loc) · 6.29 KB
/
Copy pathpreprocess.py
File metadata and controls
208 lines (176 loc) · 6.29 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
import argparse
import csv
import gzip
import os
import subprocess
import pandas as pd
import regex
from langcodes import Language
from dataset import Dataset
OPUS_REPO_ID = "https://github.com/Helsinki-NLP/OPUS-MT-train"
OPUS_COMMIT_ID = "2805bf49e759a742ac76e2011f1ee8f5eb9b6ab9"
TATOEBA_REPO_ID = "https://github.com/Helsinki-NLP/Tatoeba-Challenge"
TATOEBA_COMMIT_ID = "d34a89ac102fd236503a1911dd1050564bf4e682"
def preprocess_text(text: str) -> str:
replacements = [
(r',', ','),
(r'。 *', '. '),
(r'、', ','),
(r'”', '"'),
(r'“', '"'),
(r'∶', ':'),
(r':', ':'),
(r'?', '?'),
(r'《', '"'),
(r'》', '"'),
(r')', ')'),
(r'!', '!'),
(r'(', '('),
(r';', ';'),
(r'1', '1'),
(r'」', '"'),
(r'「', '"'),
(r'0', '0'),
(r'3', '3'),
(r'2', '2'),
(r'5', '5'),
(r'6', '6'),
(r'9', '9'),
(r'7', '7'),
(r'8', '8'),
(r'4', '4'),
(r'. *', '. '),
(r'~', '~'),
(r'’', '\''),
(r'…', '...'),
(r'━', '-'),
(r'〈', '<'),
(r'〉', '>'),
(r'【', '['),
(r'】', ']'),
(r'%', '%'),
(r' +', ' '),
(r'^ *', ''),
(r' *$', ''),
(r'\p{C}+', ''),
]
for pattern, replacement in replacements:
text = regex.sub(pattern, replacement, text)
return text.strip()
def process_opus(
output_path: str,
input_path: str,
dataset: Dataset
) -> None:
output_path = os.path.join(output_path, f"{dataset.source_language}-{dataset.target_language}")
input_path = os.path.join(input_path, "testsets", f"{dataset.source_language}-{dataset.target_language}")
os.makedirs(output_path, exist_ok=True)
for benchmark in dataset.benchmarks()[:-1]:
l0_path = os.path.join(input_path, f"{benchmark}.{dataset.source_language}.gz")
l1_path = os.path.join(input_path, f"{benchmark}.{dataset.target_language}.gz")
out_path = os.path.join(output_path, f"{benchmark}.{dataset.source_language}.{dataset.target_language}.csv")
with gzip.open(l0_path, "rt", encoding="utf-8") as fd:
l0_lines = [preprocess_text(line) for line in fd]
with gzip.open(l1_path, "rt", encoding="utf-8") as fd:
l1_lines = [preprocess_text(line) for line in fd]
d0 = pd.DataFrame(l0_lines, columns=[dataset.source_language])
d1 = pd.DataFrame(l1_lines, columns=[dataset.target_language])
data = pd.concat([d0, d1], axis=1)
data.to_csv(
out_path,
index=False
)
def process_tatoeba(
output_path: str,
input_path: str,
dataset: Dataset
) -> None:
l0 = Language.get(dataset.source_language).to_alpha3()
l1 = Language.get(dataset.target_language).to_alpha3()
data_path = os.path.join(
input_path,
"data",
"test",
f"{l0}-{l1}",
"test.txt")
data_names = ["l0", "l1", dataset.source_language, dataset.target_language]
if not os.path.exists(data_path):
l0, l1 = l1, l0
data_path = os.path.join(
input_path,
"data",
"test",
f"{l0}-{l1}",
"test.txt")
data_names = ["l1", "l0", dataset.target_language, dataset.source_language]
data = pd.read_csv(
data_path,
sep="\t",
names=data_names,
on_bad_lines="skip",
dtype=str,
quoting=csv.QUOTE_NONE)
for index, row in data.iterrows():
data.loc[index, dataset.source_language] = preprocess_text(row[dataset.source_language])
data.loc[index, dataset.target_language] = preprocess_text(row[dataset.target_language])
benchmark = dataset.benchmarks()[-1]
benchmark_folder = f"{dataset.source_language}-{dataset.target_language}"
benchmark_filename = f"{benchmark}.{dataset.source_language}.{dataset.target_language}.csv"
benchmark_path = os.path.join(output_path, benchmark_folder, benchmark_filename)
data[[dataset.source_language, dataset.target_language]].to_csv(
benchmark_path,
index=False
)
def preprocess(
dataset_path: str,
tatoeba_path: str,
opus_path: str,
source_language: str,
target_language: str,
) -> None:
os.makedirs(dataset_path, exist_ok=True)
os.makedirs(os.path.dirname(tatoeba_path), exist_ok=True)
os.makedirs(os.path.dirname(opus_path), exist_ok=True)
if not os.path.exists(opus_path):
subprocess.check_call(
["git", "clone", "--depth", "1", OPUS_REPO_ID, opus_path])
subprocess.check_call(
["git", "fetch", "--depth", "1", "origin", OPUS_COMMIT_ID],
cwd=opus_path)
subprocess.check_call(
["git", "checkout", OPUS_COMMIT_ID],
cwd=opus_path)
if not os.path.exists(tatoeba_path):
subprocess.check_call(
["git", "clone", "--depth", "1", TATOEBA_REPO_ID, tatoeba_path])
subprocess.check_call(
["git", "fetch", "--depth", "1", "origin", TATOEBA_COMMIT_ID],
cwd=tatoeba_path)
subprocess.check_call(
["git", "checkout", TATOEBA_COMMIT_ID],
cwd=tatoeba_path)
dataset = Dataset.create(None, source_language, target_language)
process_opus(dataset_path, opus_path, dataset)
process_tatoeba(dataset_path, tatoeba_path, dataset)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--dataset-path", type=str, required=True)
parser.add_argument("--tatoeba-path", type=str, required=True)
parser.add_argument("--opus-path", type=str, required=True)
parser.add_argument("--source-language", type=str, required=True)
parser.add_argument("--target-language", type=str, required=True)
args = parser.parse_args()
dataset_path = os.path.abspath(args.dataset_path)
tatoeba_path = os.path.abspath(args.tatoeba_path)
opus_path = os.path.abspath(args.opus_path)
source_language = args.source_language
target_language = args.target_language
preprocess(
dataset_path,
tatoeba_path,
opus_path,
source_language,
target_language
)
if __name__ == "__main__":
main()