|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from collections import defaultdict |
| 4 | +from pathlib import Path |
| 5 | +from typing import Dict, Optional, Union |
| 6 | + |
| 7 | +from lhotse import validate_recordings_and_supervisions |
| 8 | +from lhotse.audio import Recording, RecordingSet |
| 9 | +from lhotse.qa import fix_manifests |
| 10 | +from lhotse.supervision import SupervisionSegment, SupervisionSet |
| 11 | +from lhotse.utils import Pathlike |
| 12 | + |
| 13 | + |
| 14 | +def prepare_reazonspeech( |
| 15 | + corpus_dir: Pathlike, |
| 16 | + output_dir: Optional[Pathlike] = None, |
| 17 | +) -> Dict[str, Dict[str, Union[RecordingSet, SupervisionSet]]]: |
| 18 | + |
| 19 | + corpus_dir = Path(corpus_dir) |
| 20 | + assert corpus_dir.is_dir(), f"No such directory: {corpus_dir}" |
| 21 | + manifests = defaultdict(dict) |
| 22 | + |
| 23 | + if output_dir is not None: |
| 24 | + output_dir = Path(output_dir) |
| 25 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 26 | + |
| 27 | + idx = 0 |
| 28 | + for part in ["train", "valid", "test"]: |
| 29 | + recordings = [] |
| 30 | + supervisions = [] |
| 31 | + with open("%s/%s.json" % (corpus_dir, part)) as fp: |
| 32 | + for line in fp: |
| 33 | + line = line.strip() |
| 34 | + if not line: |
| 35 | + continue |
| 36 | + item = json.loads(line) |
| 37 | + recordings.append( |
| 38 | + Recording.from_file(item['audio_filepath'], recording_id=str(idx)) |
| 39 | + ) |
| 40 | + supervisions.append(SupervisionSegment( |
| 41 | + id=str(idx), |
| 42 | + recording_id=str(idx), |
| 43 | + start=0.0, |
| 44 | + duration=item['duration'], |
| 45 | + channel=0, |
| 46 | + language="Japanese", |
| 47 | + speaker=str(idx), |
| 48 | + text=item['text'] |
| 49 | + )) |
| 50 | + idx += 1 |
| 51 | + |
| 52 | + recording_set = RecordingSet.from_recordings(recordings) |
| 53 | + supervision_set = SupervisionSet.from_segments(supervisions) |
| 54 | + recording_set, supervision_set = fix_manifests(recording_set, supervision_set) |
| 55 | + validate_recordings_and_supervisions(recording_set, supervision_set) |
| 56 | + |
| 57 | + if output_dir is not None: |
| 58 | + supervision_set.to_file( |
| 59 | + output_dir / f"reazonspeech_supervisions_{part}.jsonl.gz" |
| 60 | + ) |
| 61 | + recording_set.to_file(output_dir / f"reazonspeech_recordings_{part}.jsonl.gz") |
| 62 | + |
| 63 | + manifests[part] = {"recordings": recording_set, "supervisions": supervision_set} |
| 64 | + |
| 65 | + return manifests |
0 commit comments