HoneyTian's picture
update
e31151e
#!/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()