<gh_stars>0
#!/usr/bin/env python3
# Copyright    2022  Xiaomi Corp.             (<NAME>)
#
# See ../../../../LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from pathlib import Path

from lhotse import CutSet
from lhotse.recipes.utils import read_manifests_if_cached


def preprocess_aidatatang_200zh():
    src_dir = Path("data/manifests/aidatatang_200zh")
    output_dir = Path("data/fbank/aidatatang_200zh")
    output_dir.mkdir(exist_ok=True, parents=True)

    dataset_parts = (
        "train",
        "test",
        "dev",
    )

    logging.info("Loading manifest")
    manifests = read_manifests_if_cached(
        dataset_parts=dataset_parts,
        output_dir=src_dir,
    )
    assert len(manifests) > 0

    for partition, m in manifests.items():
        logging.info(f"Processing {partition}")
        raw_cuts_path = output_dir / f"cuts_{partition}_raw.jsonl.gz"
        if raw_cuts_path.is_file():
            logging.info(f"{partition} already exists - skipping")
            continue

        for sup in m["supervisions"]:
            sup.custom = {"origin": "aidatatang_200zh"}

        cut_set = CutSet.from_manifests(
            recordings=m["recordings"],
            supervisions=m["supervisions"],
        )

        logging.info(f"Saving to {raw_cuts_path}")
        cut_set.to_file(raw_cuts_path)


def main():
    formatter = (
        "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
    )
    logging.basicConfig(format=formatter, level=logging.INFO)

    preprocess_aidatatang_200zh()


if __name__ == "__main__":
    main()
