-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_csv_to_mtx_json_r.py
188 lines (166 loc) · 6.15 KB
/
convert_csv_to_mtx_json_r.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
import argparse
import csv
import json
from collections import OrderedDict
from os import makedirs, pardir
from os.path import join as path_join
from os.path import realpath, abspath
def save_to_dir(path, data, language, postfix, offset_64bit=False):
for json_path in data:
json_data = {"Has64BitOffsets": offset_64bit, "Entries": []}
for entry_wrapper in data[json_path]:
entry_wrapper_o = []
for entry in entry_wrapper:
entry_wrapper_o.append(entry[language])
json_data["Entries"].append(entry_wrapper_o)
json_file_path = path_join(
realpath(path), json_path + f"{postfix}.json"
)
makedirs(abspath(path_join(json_file_path, pardir)), exist_ok=True)
with open(json_file_path, "w", encoding="utf-8") as f:
f.write(json.dumps(json_data, ensure_ascii=False, indent=True))
def load_data_from_csv(path, language, debug=False):
data = OrderedDict()
with open(path, newline="", encoding="utf-8") as f:
csv_reader = csv.reader(f)
is_header = True
index = 0
for row in csv_reader:
index = index + 1
if is_header:
is_header = False
continue
key = row[0]
en = row[1].replace("\\n", "\n")
kr = row[2].replace("\\n", "\n")
kr_s = row[3].replace("\\n", "\n")
jp = row[4].replace("\\n", "\n")
if debug:
el = {
"en": f"[{index}]{en}",
"kr": f"[{index}]{kr}",
"kr_s": f"[{index}]{kr_s}",
"jp": f"[{index}]{jp}",
}
else:
el = {
"en": f"{en}",
"kr": f"{kr}",
"kr_s": f"{kr_s}",
"jp": f"{jp}",
}
if not el["en"] and not el[language] :
print(
f"[W] It is not have any english(base) and {language} text. skipped:"
f" {key}"
)
continue
if not el["en"] and el[language]:
print(
f"[W] It is not have any english(base) text but {language} is defined:"
f" {key}"
)
if el["en"] and not el[language]:
raise Exception(
f"[!] It is not have any {language} text:"
f" {key}"
)
skey = key.split("|")
json_path = skey[0]
entry_wrapper_index = skey[1]
entry_index = skey[2]
if json_path not in data:
data[json_path] = {}
if entry_wrapper_index not in data[json_path]:
data[json_path][entry_wrapper_index] = {}
if debug:
data[json_path][entry_wrapper_index][entry_index] = {
"en": f"[{index}]{en}",
"kr": f"[{index}]{kr}",
"kr_s": f"[{index}]{kr_s}",
"jp": f"[{index}]{jp}",
}
else:
data[json_path][entry_wrapper_index][entry_index] = {
"en": f"{en}",
"kr": f"{kr}",
"kr_s": f"{kr_s}",
"jp": f"{jp}",
}
# validation and convert dict to list
for json_path in data:
ewil = len(data[json_path].keys())
ewi_list = []
for entry_wrapper_index in range(ewil):
entry_wrapper_index = str(entry_wrapper_index)
if entry_wrapper_index not in data[json_path]:
raise Exception(
f"entry_wrapper_index {entry_wrapper_index}"
f" is not defined in {json_path}"
)
eil = len(data[json_path][entry_wrapper_index].keys())
ei_list = []
for entry_index in range(eil):
entry_index = str(entry_index)
if entry_index not in data[json_path][entry_wrapper_index]:
raise Exception(
f"entry_index {entry_index} is not defined in"
f" {json_path} on entry_wrapper_index {entry_wrapper_index}"
)
el = data[json_path][entry_wrapper_index][entry_index]
if not el["en"] and el[language]:
print(
f"[W] It is not have any english(base) text but {language} is defined:"
f" {json_path}|{entry_wrapper_index}|{entry_index}"
)
if el["en"] and not el[language]:
raise Exception(
f"[!] It is not have any {language} text:"
f" {json_path}|{entry_wrapper_index}|{entry_index}"
)
ei_list.append(data[json_path][entry_wrapper_index][entry_index])
ewi_list.append(ei_list)
data[json_path] = ewi_list
return data
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert csv file to mtx(json format)"
)
parser.add_argument(
"-d",
"--dir_path",
help="target directory path to convert",
required=True,
)
parser.add_argument(
"-c",
"--csv",
help="file path of csv to load",
required=True,
)
parser.add_argument(
"-l",
"--language",
help="language to convert",
required=True,
)
parser.add_argument(
"-p",
"--postfix",
help="postfix of mtx filename",
required=True,
)
parser.add_argument(
"--offset_64bit",
help="set Has64BitOffsets to true",
action="store_true",
)
parser.add_argument(
"--debug",
help="add debug text",
action="store_true",
)
args = parser.parse_args()
loaded_data = load_data_from_csv(args.csv, args.language, args.debug)
save_to_dir(args.dir_path, loaded_data, args.language, args.postfix,
args.offset_64bit)