-
Notifications
You must be signed in to change notification settings - Fork 2
/
replace.py
152 lines (116 loc) · 3.4 KB
/
replace.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
import numpy as np
from datasets import load_dataset, Dataset
from functools import partial
import glob
import os
import re
import pandas as pd
import csv
from document import Document
import pickle
import json
import argparse
from datasets import disable_caching
disable_caching()
def parse_args():
parser = argparse.ArgumentParser(description="Perform replace on samples")
parser.add_argument(
"--paths_data",
type=str,
required=True
)
parser.add_argument(
"--cache_dir",
type=str,
required=True,
)
parser.add_argument(
"--batch_size",
type=int,
required=False,
default=64,
)
parser.add_argument(
"--format",
type=str,
default="arrow",
required=False
)
parser.add_argument(
"--num_procs",
type=int,
required=False,
default=512,
)
parser.add_argument(
"--decode_base_path",
type=str,
required=False,
default=None,
)
parser.add_argument(
"--translated_save_path",
type=str,
required=True,
)
args = parser.parse_args()
return args
# Define a function for each match
def replace_match(match, replacements):
return replacements.get(match.group(0), match.group(0))
def replace_translated(samples, decode_base_path=None):
translated = []
sub_strs_tlt = []
for i in range(len(samples["doc_id"])):
if not samples["text"][i] or not len(samples["text"][i]):
translated += [str(None)]
sub_strs_tlt += [[str(None)]]
continue
replacements = dict()
sids = json.loads(samples["sids"][i])
sub_tlts = []
for s_idx, sid in enumerate(sids):
sid_tlt_file_path = os.path.join(samples["tlt_folder"][i], sid)
sub_strs = json.loads(samples["sub_strs"][i])
if decode_base_path:
sid_tlt_file_path = os.path.join(decode_base_path, *sid_tlt_file_path.split("/")[-2:])
if not os.path.exists(sid_tlt_file_path):
continue
with open(sid_tlt_file_path, "r") as tlt_f:
tlt_sub = tlt_f.read()
replacements[sub_strs[s_idx]] = tlt_sub
sub_tlts += [tlt_sub]
pattern = re.compile('|'.join(re.escape(key) for key in replacements.keys()))
# Perform the replacements
translated += [
pattern.sub(
partial(replace_match, replacements=replacements),
samples["text"][i],
)
]
sub_strs_tlt += [sub_tlts if len(sub_tlts) else [str(None)]]
return samples | {
"translated": translated,
"substr_tlt": sub_strs_tlt,
}
if __name__ == "__main__":
args = parse_args()
paths_ds = load_dataset(
args.format,
data_files=glob.glob(args.paths_data),
cache_dir=args.cache_dir,
num_proc=args.num_procs,
split="train",
)
replace_ds = paths_ds.select(range(4)).map(
partial(replace_translated, decode_base_path=args.decode_base_path),
batched=True,
batch_size=4,
num_proc=1,
load_from_cache_file=False,
)
os.makedirs(args.translated_save_path, exist_ok=True)
replace_ds.save_to_disk(
args.translated_save_path,
num_proc=args.num_procs,
)