-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean_json_duration.py
53 lines (45 loc) · 1.35 KB
/
mean_json_duration.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
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
def get_parser():
parser = argparse.ArgumentParser(
"mean_json_duration.py",
description="Calculate mean duration of utterances in JSON files",
)
parser.add_argument(
"--jsons",
type=str,
help="Relative paths to JSON files",
nargs="+",
required=True,
)
parser.add_argument(
"--data_dir",
type=str,
help="Data directory containing JSON files",
required=True,
)
parser.add_argument(
"--max_duration",
type=float,
help="Filter out utterances longer than this duration, default 20.0",
default=20.0,
)
return parser
def get_abs_paths(json_files, data_dir):
return [str(Path(data_dir) / f) for f in json_files]
def main(args):
abs_paths = get_abs_paths(args.jsons, args.data_dir)
nested_contents = (json.load(open(path)) for path in abs_paths)
flat_contents = (item for each_json in nested_contents for item in each_json)
durations = [
item["original_duration"]
for item in flat_contents
if item["original_duration"] <= args.max_duration
]
print(f"Mean duration: {sum(durations) / len(durations)}")
if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
main(args)