forked from espnet/espnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatjson.py
executable file
·57 lines (45 loc) · 1.52 KB
/
concatjson.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
#!/usr/bin/env python3
# encoding: utf-8
# Copyright 2017 Johns Hopkins University (Shinji Watanabe)
# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
import argparse
import codecs
import json
import logging
import sys
from espnet.utils.cli_utils import get_commandline_args
is_python2 = sys.version_info[0] == 2
def get_parser():
parser = argparse.ArgumentParser(
description="concatenate json files",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("jsons", type=str, nargs="+", help="json files")
return parser
if __name__ == "__main__":
args = get_parser().parse_args()
# logging info
logfmt = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s"
logging.basicConfig(level=logging.INFO, format=logfmt)
logging.info(get_commandline_args())
# make intersection set for utterance keys
js = {}
for x in args.jsons:
with codecs.open(x, encoding="utf-8") as f:
j = json.load(f)
ks = j["utts"].keys()
logging.debug(x + ": has " + str(len(ks)) + " utterances")
js.update(j["utts"])
logging.info("new json has " + str(len(js.keys())) + " utterances")
# ensure "ensure_ascii=False", which is a bug
jsonstring = json.dumps(
{"utts": js},
indent=4,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
)
sys.stdout = codecs.getwriter("utf-8")(
sys.stdout if is_python2 else sys.stdout.buffer
)
print(jsonstring)