File size: 4,272 Bytes
e6bf0f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21a0491
e6bf0f4
 
 
 
21a0491
e6bf0f4
 
 
 
 
 
029dbdb
21a0491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
029dbdb
 
 
e6bf0f4
 
 
 
19c57bc
e6bf0f4
 
 
 
 
21a0491
 
 
 
e31151e
21a0491
 
 
 
e31151e
e6bf0f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e31151e
e6bf0f4
19c57bc
e6bf0f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19c57bc
e6bf0f4
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from collections import defaultdict
from pathlib import Path
import shutil

import pandas as pd

from project_settings import project_path


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--audio_dir",
        default=(project_path / "data/analysis/es-MX").as_posix(),
        type=str
    )
    parser.add_argument(
        "--output_dir",
        default=(project_path / "data/calling/52").as_posix(),
        type=str
    )
    args = parser.parse_args()
    return args


quantity_to_use = """
bell_and_machine_voice: 6, 6%
bell_and_mute: 2, 2%
bell_and_mute_1_second_then_voicemail: 1, 1%
bell_and_noise: 4, 4%
bell_and_noise_mute: 3, 3%
bell_and_not_connected: 17, 17%
bell_and_voice: 10, 10%
bell_and_voicemail: 19, 19%
busy_and_not_connected: 11, 11%
busy_or_disconnected_or_out_of_coverage_and_not_connected: 1, 1%
disconnected_or_out_of_service_and_not_connected: 1, 1%
early_media_voicemail_and_custom_voicemail: 16, 16%
early_media_voicemail_and_mute_3s_then_voicemail: 5, 5%
early_media_voicemail_and_mute_5s_then_voicemail: 1, 1%
early_media_voicemail_and_not_connected: 11, 11%
early_media_voicemail_and_voicemail: 31, 31%
early_media_voicemail_is_full_and_not_connected: 9, 9%
music_and_voicemail: 2, 2%
mute_and_machine_voice: 4, 4%
mute_and_not_connected: 1, 1%
mute_and_voicemail: 3, 3%
no_early_media_and_mute_3s_then_busy: 1, 1%
no_early_media_and_voice: 2, 2%
out_of_service_and_not_connected: 2, 2%
unavailable_and_not_connected: 1, 1%
unavailable_or_out_of_service_and_not_connected: 2, 2%
"""


def main():
    args = get_args()
    audio_dir = Path(args.audio_dir)
    output_dir = Path(args.output_dir)
    output_dir.mkdir(parents=True, exist_ok=False)
    suffix_count = 0

    call_id_to_wav_file_list = defaultdict(list)
    for filename in audio_dir.glob("**/*.wav"):
        splits = filename.stem.split("_")
        if len(splits) == 4:
            call_id = splits[3]
            # language = splits[4]
            # scene_id = splits[5]
        elif len(splits) == 7:
            call_id = splits[3]
            language = splits[4]
            scene_id = splits[5]
        else:
            raise AssertionError(f"len splits, {len(splits)}, {filename.stem}")
        call_id_to_wav_file_list[call_id].append(filename)
    print(f"count: {len(call_id_to_wav_file_list)}")

    rows = quantity_to_use.strip().split("\n")

    result = list()
    for row in rows:
        splits = row.strip().split(",", maxsplit=1)
        row = splits[0]
        splits = row.strip().split(":", maxsplit=1)

        folder_name = splits[0].strip()
        need_count = splits[1].strip()
        need_count = int(need_count)

        candidate_dir = audio_dir / folder_name
        candidates = list(candidate_dir.glob("active_media_r_*.wav"))

        print(f"candidates count: {len(candidates)}, need count: {need_count}, folder_name: {folder_name}")
        candidates = candidates[:need_count]
        for filename in candidates:
            splits = filename.stem.split("_")
            call_id = splits[3]
            print(call_id)

            tgt_dir = output_dir / f"{suffix_count:03}"
            tgt_dir.mkdir(parents=True, exist_ok=True)
            suffix_count += 1
            wav_file_list = call_id_to_wav_file_list[call_id]

            for wav_file in wav_file_list:

                if wav_file.stem.startswith("active_media_r"):
                    tgt_filename = tgt_dir / f"active_media_{call_id}.wav"
                elif wav_file.stem.startswith("early_media_r"):
                    tgt_filename = tgt_dir / f"early_media_{call_id}.wav"
                elif wav_file.stem.startswith("active_media_w"):
                    continue
                else:
                    raise AssertionError
                shutil.copy(
                    wav_file.as_posix(),
                    tgt_filename.as_posix(),
                )
            result.append({
                "suffix": f"{suffix_count-1:03}",
                "label": folder_name
            })
    result = pd.DataFrame(result)
    result.to_excel(
        output_dir / "readme.xlsx",
        index=False
    )

    return


if __name__ == "__main__":
    main()